Annotation of src/usr.bin/tmux/grid.c, Revision 1.114
1.114 ! nicm 1: /* $OpenBSD: grid.c,v 1.113 2020/05/26 08:56:48 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.111 nicm 499: utf8_to_data(gee->data, &gc->data);
1.110 nicm 500: }
1.48 nicm 501: return;
502: }
1.1 nicm 503:
1.53 nicm 504: gc->flags = gce->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
1.48 nicm 505: gc->attr = gce->data.attr;
506: gc->fg = gce->data.fg;
1.53 nicm 507: if (gce->flags & GRID_FLAG_FG256)
508: gc->fg |= COLOUR_FLAG_256;
1.48 nicm 509: gc->bg = gce->data.bg;
1.53 nicm 510: if (gce->flags & GRID_FLAG_BG256)
511: gc->bg |= COLOUR_FLAG_256;
1.97 nicm 512: gc->us = 0;
1.48 nicm 513: utf8_set(&gc->data, gce->data.data);
1.1 nicm 514: }
515:
1.79 nicm 516: /* Get cell for reading. */
517: void
518: grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
519: {
1.81 nicm 520: if (grid_check_y(gd, __func__, py) != 0 ||
1.90 nicm 521: px >= gd->linedata[py].cellsize)
1.79 nicm 522: memcpy(gc, &grid_default_cell, sizeof *gc);
1.90 nicm 523: else
524: grid_get_cell1(&gd->linedata[py], px, gc);
1.79 nicm 525: }
526:
1.1 nicm 527: /* Set cell at relative position. */
528: void
1.31 nicm 529: grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
1.1 nicm 530: {
1.48 nicm 531: struct grid_line *gl;
532: struct grid_cell_entry *gce;
533:
1.81 nicm 534: if (grid_check_y(gd, __func__, py) != 0)
1.1 nicm 535: return;
536:
1.58 nicm 537: grid_expand_line(gd, py, px + 1, 8);
1.48 nicm 538:
539: gl = &gd->linedata[py];
1.58 nicm 540: if (px + 1 > gl->cellused)
541: gl->cellused = px + 1;
542:
1.63 nicm 543: gce = &gl->celldata[px];
1.65 nicm 544: if (grid_need_extended_cell(gce, gc))
1.59 nicm 545: grid_extended_cell(gl, gce, gc);
1.63 nicm 546: else
547: grid_store_cell(gce, gc, gc->data.data[0]);
1.64 nicm 548: }
549:
550: /* Set cells at relative position. */
551: void
552: grid_set_cells(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc,
553: const char *s, size_t slen)
554: {
555: struct grid_line *gl;
556: struct grid_cell_entry *gce;
1.110 nicm 557: struct grid_extd_entry *gee;
1.64 nicm 558: u_int i;
559:
1.81 nicm 560: if (grid_check_y(gd, __func__, py) != 0)
1.64 nicm 561: return;
562:
563: grid_expand_line(gd, py, px + slen, 8);
564:
565: gl = &gd->linedata[py];
566: if (px + slen > gl->cellused)
567: gl->cellused = px + slen;
568:
569: for (i = 0; i < slen; i++) {
570: gce = &gl->celldata[px + i];
1.65 nicm 571: if (grid_need_extended_cell(gce, gc)) {
1.110 nicm 572: gee = grid_extended_cell(gl, gce, gc);
1.111 nicm 573: gee->data = utf8_build_one(s[i], 1);
1.64 nicm 574: } else
575: grid_store_cell(gce, gc, s[i]);
576: }
1.1 nicm 577: }
578:
1.14 nicm 579: /* Clear area. */
1.1 nicm 580: void
1.58 nicm 581: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny, u_int bg)
1.1 nicm 582: {
1.88 nicm 583: struct grid_line *gl;
1.98 nicm 584: u_int xx, yy, ox, sx;
1.1 nicm 585:
586: if (nx == 0 || ny == 0)
587: return;
588:
589: if (px == 0 && nx == gd->sx) {
1.58 nicm 590: grid_clear_lines(gd, py, ny, bg);
1.1 nicm 591: return;
592: }
593:
1.81 nicm 594: if (grid_check_y(gd, __func__, py) != 0)
1.1 nicm 595: return;
1.81 nicm 596: if (grid_check_y(gd, __func__, py + ny - 1) != 0)
1.1 nicm 597: return;
598:
599: for (yy = py; yy < py + ny; yy++) {
1.88 nicm 600: gl = &gd->linedata[yy];
1.98 nicm 601:
602: sx = gd->sx;
603: if (sx > gl->cellsize)
604: sx = gl->cellsize;
605: ox = nx;
606: if (COLOUR_DEFAULT(bg)) {
607: if (px > sx)
608: continue;
609: if (px + nx > sx)
610: ox = sx - px;
1.14 nicm 611: }
1.98 nicm 612:
613: grid_expand_line(gd, yy, px + ox, 8); /* default bg first */
614: for (xx = px; xx < px + ox; xx++)
1.58 nicm 615: grid_clear_cell(gd, xx, yy, bg);
1.1 nicm 616: }
617: }
618:
619: /* Clear lines. This just frees and truncates the lines. */
620: void
1.58 nicm 621: grid_clear_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
1.1 nicm 622: {
1.75 nicm 623: u_int yy;
1.1 nicm 624:
625: if (ny == 0)
626: return;
627:
1.81 nicm 628: if (grid_check_y(gd, __func__, py) != 0)
1.1 nicm 629: return;
1.81 nicm 630: if (grid_check_y(gd, __func__, py + ny - 1) != 0)
1.1 nicm 631: return;
632:
633: for (yy = py; yy < py + ny; yy++) {
1.75 nicm 634: grid_free_line(gd, yy);
1.58 nicm 635: grid_empty_line(gd, yy, bg);
1.1 nicm 636: }
637: }
638:
639: /* Move a group of lines. */
640: void
1.58 nicm 641: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny, u_int bg)
1.1 nicm 642: {
643: u_int yy;
644:
645: if (ny == 0 || py == dy)
646: return;
647:
1.81 nicm 648: if (grid_check_y(gd, __func__, py) != 0)
1.1 nicm 649: return;
1.81 nicm 650: if (grid_check_y(gd, __func__, py + ny - 1) != 0)
1.1 nicm 651: return;
1.81 nicm 652: if (grid_check_y(gd, __func__, dy) != 0)
1.1 nicm 653: return;
1.81 nicm 654: if (grid_check_y(gd, __func__, dy + ny - 1) != 0)
1.1 nicm 655: return;
656:
657: /* Free any lines which are being replaced. */
658: for (yy = dy; yy < dy + ny; yy++) {
659: if (yy >= py && yy < py + ny)
660: continue;
1.75 nicm 661: grid_free_line(gd, yy);
1.1 nicm 662: }
663:
1.46 nicm 664: memmove(&gd->linedata[dy], &gd->linedata[py],
665: ny * (sizeof *gd->linedata));
1.1 nicm 666:
1.75 nicm 667: /*
668: * Wipe any lines that have been moved (without freeing them - they are
669: * still present).
670: */
1.1 nicm 671: for (yy = py; yy < py + ny; yy++) {
1.58 nicm 672: if (yy < dy || yy >= dy + ny)
673: grid_empty_line(gd, yy, bg);
1.1 nicm 674: }
675: }
676:
677: /* Move a group of cells. */
678: void
1.58 nicm 679: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx,
680: u_int bg)
1.1 nicm 681: {
1.10 nicm 682: struct grid_line *gl;
683: u_int xx;
1.1 nicm 684:
685: if (nx == 0 || px == dx)
686: return;
687:
1.81 nicm 688: if (grid_check_y(gd, __func__, py) != 0)
1.1 nicm 689: return;
1.10 nicm 690: gl = &gd->linedata[py];
1.1 nicm 691:
1.58 nicm 692: grid_expand_line(gd, py, px + nx, 8);
693: grid_expand_line(gd, py, dx + nx, 8);
1.46 nicm 694: memmove(&gl->celldata[dx], &gl->celldata[px],
695: nx * sizeof *gl->celldata);
1.67 nicm 696: if (dx + nx > gl->cellused)
697: gl->cellused = dx + nx;
1.1 nicm 698:
699: /* Wipe any cells that have been moved. */
700: for (xx = px; xx < px + nx; xx++) {
701: if (xx >= dx && xx < dx + nx)
702: continue;
1.58 nicm 703: grid_clear_cell(gd, xx, py, bg);
1.1 nicm 704: }
1.3 nicm 705: }
706:
1.24 nicm 707: /* Get ANSI foreground sequence. */
1.56 nicm 708: static size_t
1.24 nicm 709: grid_string_cells_fg(const struct grid_cell *gc, int *values)
710: {
711: size_t n;
1.53 nicm 712: u_char r, g, b;
1.24 nicm 713:
714: n = 0;
1.53 nicm 715: if (gc->fg & COLOUR_FLAG_256) {
1.24 nicm 716: values[n++] = 38;
717: values[n++] = 5;
1.53 nicm 718: values[n++] = gc->fg & 0xff;
719: } else if (gc->fg & COLOUR_FLAG_RGB) {
1.52 nicm 720: values[n++] = 38;
721: values[n++] = 2;
1.53 nicm 722: colour_split_rgb(gc->fg, &r, &g, &b);
723: values[n++] = r;
724: values[n++] = g;
725: values[n++] = b;
1.24 nicm 726: } else {
727: switch (gc->fg) {
1.45 nicm 728: case 0:
729: case 1:
730: case 2:
731: case 3:
732: case 4:
733: case 5:
734: case 6:
735: case 7:
736: values[n++] = gc->fg + 30;
737: break;
738: case 8:
739: values[n++] = 39;
740: break;
741: case 90:
742: case 91:
743: case 92:
744: case 93:
745: case 94:
746: case 95:
747: case 96:
748: case 97:
749: values[n++] = gc->fg;
750: break;
1.24 nicm 751: }
752: }
753: return (n);
754: }
755:
756: /* Get ANSI background sequence. */
1.56 nicm 757: static size_t
1.24 nicm 758: grid_string_cells_bg(const struct grid_cell *gc, int *values)
759: {
760: size_t n;
1.53 nicm 761: u_char r, g, b;
1.24 nicm 762:
763: n = 0;
1.53 nicm 764: if (gc->bg & COLOUR_FLAG_256) {
1.24 nicm 765: values[n++] = 48;
766: values[n++] = 5;
1.53 nicm 767: values[n++] = gc->bg & 0xff;
768: } else if (gc->bg & COLOUR_FLAG_RGB) {
1.52 nicm 769: values[n++] = 48;
770: values[n++] = 2;
1.53 nicm 771: colour_split_rgb(gc->bg, &r, &g, &b);
772: values[n++] = r;
773: values[n++] = g;
774: values[n++] = b;
1.24 nicm 775: } else {
776: switch (gc->bg) {
777: case 0:
778: case 1:
779: case 2:
780: case 3:
781: case 4:
782: case 5:
783: case 6:
784: case 7:
785: values[n++] = gc->bg + 40;
786: break;
787: case 8:
788: values[n++] = 49;
789: break;
1.103 nicm 790: case 90:
791: case 91:
792: case 92:
793: case 93:
794: case 94:
795: case 95:
796: case 96:
797: case 97:
798: values[n++] = gc->bg + 10;
1.24 nicm 799: break;
800: }
801: }
802: return (n);
803: }
804:
805: /*
806: * Returns ANSI code to set particular attributes (colour, bold and so on)
1.74 nicm 807: * given a current state.
1.24 nicm 808: */
1.56 nicm 809: static void
1.24 nicm 810: grid_string_cells_code(const struct grid_cell *lastgc,
1.26 nicm 811: const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
1.24 nicm 812: {
1.52 nicm 813: int oldc[64], newc[64], s[128];
1.24 nicm 814: size_t noldc, nnewc, n, i;
1.70 nicm 815: u_int attr = gc->attr, lastattr = lastgc->attr;
1.24 nicm 816: char tmp[64];
817:
818: struct {
819: u_int mask;
820: u_int code;
821: } attrs[] = {
822: { GRID_ATTR_BRIGHT, 1 },
823: { GRID_ATTR_DIM, 2 },
824: { GRID_ATTR_ITALICS, 3 },
825: { GRID_ATTR_UNDERSCORE, 4 },
826: { GRID_ATTR_BLINK, 5 },
827: { GRID_ATTR_REVERSE, 7 },
1.68 nicm 828: { GRID_ATTR_HIDDEN, 8 },
1.87 nicm 829: { GRID_ATTR_STRIKETHROUGH, 9 },
830: { GRID_ATTR_UNDERSCORE_2, 42 },
831: { GRID_ATTR_UNDERSCORE_3, 43 },
832: { GRID_ATTR_UNDERSCORE_4, 44 },
833: { GRID_ATTR_UNDERSCORE_5, 45 },
1.94 nicm 834: { GRID_ATTR_OVERLINE, 53 },
1.24 nicm 835: };
836: n = 0;
837:
838: /* If any attribute is removed, begin with 0. */
839: for (i = 0; i < nitems(attrs); i++) {
840: if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
841: s[n++] = 0;
1.25 nicm 842: lastattr &= GRID_ATTR_CHARSET;
1.24 nicm 843: break;
844: }
845: }
846: /* For each attribute that is newly set, add its code. */
847: for (i = 0; i < nitems(attrs); i++) {
848: if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
849: s[n++] = attrs[i].code;
850: }
851:
1.70 nicm 852: /* Write the attributes. */
853: *buf = '\0';
854: if (n > 0) {
855: if (escape_c0)
856: strlcat(buf, "\\033[", len);
857: else
858: strlcat(buf, "\033[", len);
859: for (i = 0; i < n; i++) {
1.87 nicm 860: if (s[i] < 10)
1.70 nicm 861: xsnprintf(tmp, sizeof tmp, "%d", s[i]);
1.87 nicm 862: else {
863: xsnprintf(tmp, sizeof tmp, "%d:%d", s[i] / 10,
864: s[i] % 10);
865: }
1.70 nicm 866: strlcat(buf, tmp, len);
1.87 nicm 867: if (i + 1 < n)
868: strlcat(buf, ";", len);
1.70 nicm 869: }
870: strlcat(buf, "m", len);
871: }
872:
873: /* If the foreground colour changed, write its parameters. */
1.24 nicm 874: nnewc = grid_string_cells_fg(gc, newc);
875: noldc = grid_string_cells_fg(lastgc, oldc);
1.70 nicm 876: if (nnewc != noldc ||
877: memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 ||
878: (n != 0 && s[0] == 0)) {
879: if (escape_c0)
880: strlcat(buf, "\\033[", len);
881: else
882: strlcat(buf, "\033[", len);
883: for (i = 0; i < nnewc; i++) {
884: if (i + 1 < nnewc)
885: xsnprintf(tmp, sizeof tmp, "%d;", newc[i]);
886: else
887: xsnprintf(tmp, sizeof tmp, "%d", newc[i]);
888: strlcat(buf, tmp, len);
889: }
890: strlcat(buf, "m", len);
1.24 nicm 891: }
892:
1.39 nicm 893: /* If the background colour changed, append its parameters. */
1.24 nicm 894: nnewc = grid_string_cells_bg(gc, newc);
895: noldc = grid_string_cells_bg(lastgc, oldc);
1.70 nicm 896: if (nnewc != noldc ||
897: memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 ||
898: (n != 0 && s[0] == 0)) {
1.26 nicm 899: if (escape_c0)
900: strlcat(buf, "\\033[", len);
901: else
902: strlcat(buf, "\033[", len);
1.70 nicm 903: for (i = 0; i < nnewc; i++) {
904: if (i + 1 < nnewc)
905: xsnprintf(tmp, sizeof tmp, "%d;", newc[i]);
1.24 nicm 906: else
1.70 nicm 907: xsnprintf(tmp, sizeof tmp, "%d", newc[i]);
1.24 nicm 908: strlcat(buf, tmp, len);
909: }
910: strlcat(buf, "m", len);
911: }
912:
913: /* Append shift in/shift out if needed. */
1.26 nicm 914: if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
915: if (escape_c0)
1.70 nicm 916: strlcat(buf, "\\016", len); /* SO */
1.26 nicm 917: else
918: strlcat(buf, "\016", len); /* SO */
919: }
920: if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
921: if (escape_c0)
1.70 nicm 922: strlcat(buf, "\\017", len); /* SI */
1.26 nicm 923: else
924: strlcat(buf, "\017", len); /* SI */
925: }
1.24 nicm 926: }
927:
1.3 nicm 928: /* Convert cells into a string. */
929: char *
1.24 nicm 930: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
1.28 nicm 931: struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
1.3 nicm 932: {
1.48 nicm 933: struct grid_cell gc;
1.24 nicm 934: static struct grid_cell lastgc1;
1.38 nicm 935: const char *data;
1.24 nicm 936: char *buf, code[128];
1.26 nicm 937: size_t len, off, size, codelen;
1.16 nicm 938: u_int xx;
1.30 nicm 939: const struct grid_line *gl;
1.3 nicm 940:
1.29 nicm 941: if (lastgc != NULL && *lastgc == NULL) {
1.24 nicm 942: memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
943: *lastgc = &lastgc1;
944: }
945:
1.3 nicm 946: len = 128;
947: buf = xmalloc(len);
948: off = 0;
949:
1.30 nicm 950: gl = grid_peek_line(gd, py);
1.3 nicm 951: for (xx = px; xx < px + nx; xx++) {
1.30 nicm 952: if (gl == NULL || xx >= gl->cellsize)
953: break;
1.48 nicm 954: grid_get_cell(gd, xx, py, &gc);
955: if (gc.flags & GRID_FLAG_PADDING)
1.3 nicm 956: continue;
957:
1.24 nicm 958: if (with_codes) {
1.48 nicm 959: grid_string_cells_code(*lastgc, &gc, code, sizeof code,
1.26 nicm 960: escape_c0);
1.24 nicm 961: codelen = strlen(code);
1.48 nicm 962: memcpy(*lastgc, &gc, sizeof **lastgc);
1.24 nicm 963: } else
964: codelen = 0;
965:
1.48 nicm 966: data = gc.data.data;
967: size = gc.data.size;
1.26 nicm 968: if (escape_c0 && size == 1 && *data == '\\') {
1.27 nicm 969: data = "\\\\";
1.26 nicm 970: size = 2;
971: }
972:
973: while (len < off + size + codelen + 1) {
1.41 nicm 974: buf = xreallocarray(buf, 2, len);
1.21 nicm 975: len *= 2;
976: }
1.3 nicm 977:
1.24 nicm 978: if (codelen != 0) {
979: memcpy(buf + off, code, codelen);
980: off += codelen;
981: }
1.26 nicm 982: memcpy(buf + off, data, size);
983: off += size;
1.3 nicm 984: }
1.17 nicm 985:
1.37 nicm 986: if (trim) {
1.28 nicm 987: while (off > 0 && buf[off - 1] == ' ')
988: off--;
1.32 nicm 989: }
1.3 nicm 990: buf[off] = '\0';
1.26 nicm 991:
1.3 nicm 992: return (buf);
1.7 nicm 993: }
994:
1.17 nicm 995: /*
1.75 nicm 996: * Duplicate a set of lines between two grids. Both source and destination
997: * should be big enough.
1.7 nicm 998: */
999: void
1.31 nicm 1000: grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
1001: u_int ny)
1.7 nicm 1002: {
1.10 nicm 1003: struct grid_line *dstl, *srcl;
1004: u_int yy;
1.7 nicm 1005:
1006: if (dy + ny > dst->hsize + dst->sy)
1007: ny = dst->hsize + dst->sy - dy;
1008: if (sy + ny > src->hsize + src->sy)
1009: ny = src->hsize + src->sy - sy;
1.75 nicm 1010: grid_free_lines(dst, dy, ny);
1.7 nicm 1011:
1012: for (yy = 0; yy < ny; yy++) {
1.11 nicm 1013: srcl = &src->linedata[sy];
1014: dstl = &dst->linedata[dy];
1.10 nicm 1015:
1016: memcpy(dstl, srcl, sizeof *dstl);
1017: if (srcl->cellsize != 0) {
1.42 deraadt 1018: dstl->celldata = xreallocarray(NULL,
1.10 nicm 1019: srcl->cellsize, sizeof *dstl->celldata);
1020: memcpy(dstl->celldata, srcl->celldata,
1021: srcl->cellsize * sizeof *dstl->celldata);
1.44 nicm 1022: } else
1023: dstl->celldata = NULL;
1.7 nicm 1024:
1.48 nicm 1025: if (srcl->extdsize != 0) {
1026: dstl->extdsize = srcl->extdsize;
1027: dstl->extddata = xreallocarray(NULL, dstl->extdsize,
1028: sizeof *dstl->extddata);
1029: memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
1030: sizeof *dstl->extddata);
1031: }
1032:
1.10 nicm 1033: sy++;
1034: dy++;
1.7 nicm 1035: }
1.22 nicm 1036: }
1037:
1.80 nicm 1038: /* Mark line as dead. */
1039: static void
1040: grid_reflow_dead(struct grid_line *gl)
1041: {
1042: memset(gl, 0, sizeof *gl);
1043: gl->flags = GRID_LINE_DEAD;
1044: }
1045:
1046: /* Add lines, return the first new one. */
1047: static struct grid_line *
1048: grid_reflow_add(struct grid *gd, u_int n)
1049: {
1050: struct grid_line *gl;
1051: u_int sy = gd->sy + n;
1052:
1053: gd->linedata = xreallocarray(gd->linedata, sy, sizeof *gd->linedata);
1054: gl = &gd->linedata[gd->sy];
1055: memset(gl, 0, n * (sizeof *gl));
1056: gd->sy = sy;
1057: return (gl);
1058: }
1059:
1060: /* Move a line across. */
1061: static struct grid_line *
1062: grid_reflow_move(struct grid *gd, struct grid_line *from)
1063: {
1064: struct grid_line *to;
1065:
1066: to = grid_reflow_add(gd, 1);
1067: memcpy(to, from, sizeof *to);
1068: grid_reflow_dead(from);
1069: return (to);
1070: }
1071:
1.79 nicm 1072: /* Join line below onto this one. */
1.56 nicm 1073: static void
1.80 nicm 1074: grid_reflow_join(struct grid *target, struct grid *gd, u_int sx, u_int yy,
1.91 nicm 1075: u_int width, int already)
1.48 nicm 1076: {
1.83 nicm 1077: struct grid_line *gl, *from = NULL;
1.79 nicm 1078: struct grid_cell gc;
1.83 nicm 1079: u_int lines, left, i, to, line, want = 0;
1.80 nicm 1080: u_int at;
1.79 nicm 1081: int wrapped = 1;
1.48 nicm 1082:
1.80 nicm 1083: /*
1084: * Add a new target line.
1085: */
1086: if (!already) {
1087: to = target->sy;
1088: gl = grid_reflow_move(target, &gd->linedata[yy]);
1089: } else {
1090: to = target->sy - 1;
1091: gl = &target->linedata[to];
1092: }
1093: at = gl->cellused;
1094:
1095: /*
1096: * Loop until no more to consume or the target line is full.
1097: */
1.79 nicm 1098: lines = 0;
1099: for (;;) {
1100: /*
1101: * If this is now the last line, there is nothing more to be
1102: * done.
1103: */
1.82 nicm 1104: if (yy + 1 + lines == gd->hsize + gd->sy)
1.79 nicm 1105: break;
1106: line = yy + 1 + lines;
1.48 nicm 1107:
1.79 nicm 1108: /* If the next line is empty, skip it. */
1109: if (~gd->linedata[line].flags & GRID_LINE_WRAPPED)
1110: wrapped = 0;
1111: if (gd->linedata[line].cellused == 0) {
1112: if (!wrapped)
1113: break;
1.82 nicm 1114: lines++;
1.48 nicm 1115: continue;
1.79 nicm 1116: }
1117:
1118: /*
1119: * Is the destination line now full? Copy the first character
1120: * separately because we need to leave "from" set to the last
1121: * line if this line is full.
1122: */
1123: grid_get_cell1(&gd->linedata[line], 0, &gc);
1124: if (width + gc.data.width > sx)
1125: break;
1126: width += gc.data.width;
1.80 nicm 1127: grid_set_cell(target, at, to, &gc);
1.79 nicm 1128: at++;
1129:
1130: /* Join as much more as possible onto the current line. */
1131: from = &gd->linedata[line];
1132: for (want = 1; want < from->cellused; want++) {
1133: grid_get_cell1(from, want, &gc);
1134: if (width + gc.data.width > sx)
1135: break;
1136: width += gc.data.width;
1137:
1.80 nicm 1138: grid_set_cell(target, at, to, &gc);
1.79 nicm 1139: at++;
1140: }
1141: lines++;
1142:
1143: /*
1144: * If this line wasn't wrapped or we didn't consume the entire
1145: * line, don't try to join any further lines.
1146: */
1147: if (!wrapped || want != from->cellused || width == sx)
1148: break;
1149: }
1150: if (lines == 0)
1151: return;
1.48 nicm 1152:
1.79 nicm 1153: /*
1154: * If we didn't consume the entire final line, then remove what we did
1155: * consume. If we consumed the entire line and it wasn't wrapped,
1156: * remove the wrap flag from this line.
1157: */
1158: left = from->cellused - want;
1159: if (left != 0) {
1160: grid_move_cells(gd, 0, want, yy + lines, left, 8);
1161: from->cellsize = from->cellused = left;
1162: lines--;
1163: } else if (!wrapped)
1164: gl->flags &= ~GRID_LINE_WRAPPED;
1165:
1166: /* Remove the lines that were completely consumed. */
1.80 nicm 1167: for (i = yy + 1; i < yy + 1 + lines; i++) {
1168: free(gd->linedata[i].celldata);
1169: free(gd->linedata[i].extddata);
1170: grid_reflow_dead(&gd->linedata[i]);
1.48 nicm 1171: }
1.79 nicm 1172:
1.91 nicm 1173: /* Adjust scroll position. */
1.80 nicm 1174: if (gd->hscrolled > to + lines)
1.79 nicm 1175: gd->hscrolled -= lines;
1.80 nicm 1176: else if (gd->hscrolled > to)
1177: gd->hscrolled = to;
1.48 nicm 1178: }
1179:
1.79 nicm 1180: /* Split this line into several new ones */
1.56 nicm 1181: static void
1.80 nicm 1182: grid_reflow_split(struct grid *target, struct grid *gd, u_int sx, u_int yy,
1.91 nicm 1183: u_int at)
1.23 nicm 1184: {
1.80 nicm 1185: struct grid_line *gl = &gd->linedata[yy], *first;
1.79 nicm 1186: struct grid_cell gc;
1.80 nicm 1187: u_int line, lines, width, i, xx;
1188: u_int used = gl->cellused;
1.79 nicm 1189: int flags = gl->flags;
1.23 nicm 1190:
1.80 nicm 1191: /* How many lines do we need to insert? We know we need at least two. */
1.79 nicm 1192: if (~gl->flags & GRID_LINE_EXTENDED)
1.80 nicm 1193: lines = 1 + (gl->cellused - 1) / sx;
1.79 nicm 1194: else {
1.80 nicm 1195: lines = 2;
1.79 nicm 1196: width = 0;
1197: for (i = at; i < used; i++) {
1198: grid_get_cell1(gl, i, &gc);
1199: if (width + gc.data.width > sx) {
1200: lines++;
1201: width = 0;
1202: }
1203: width += gc.data.width;
1204: }
1205: }
1.23 nicm 1206:
1.79 nicm 1207: /* Insert new lines. */
1.80 nicm 1208: line = target->sy + 1;
1209: first = grid_reflow_add(target, lines);
1.79 nicm 1210:
1211: /* Copy sections from the original line. */
1212: width = 0;
1213: xx = 0;
1214: for (i = at; i < used; i++) {
1215: grid_get_cell1(gl, i, &gc);
1216: if (width + gc.data.width > sx) {
1.80 nicm 1217: target->linedata[line].flags |= GRID_LINE_WRAPPED;
1.79 nicm 1218:
1219: line++;
1220: width = 0;
1221: xx = 0;
1222: }
1223: width += gc.data.width;
1.80 nicm 1224: grid_set_cell(target, xx, line, &gc);
1.79 nicm 1225: xx++;
1226: }
1227: if (flags & GRID_LINE_WRAPPED)
1.80 nicm 1228: target->linedata[line].flags |= GRID_LINE_WRAPPED;
1229:
1230: /* Move the remainder of the original line. */
1231: gl->cellsize = gl->cellused = at;
1232: gl->flags |= GRID_LINE_WRAPPED;
1233: memcpy(first, gl, sizeof *first);
1234: grid_reflow_dead(gl);
1.79 nicm 1235:
1.91 nicm 1236: /* Adjust the scroll position. */
1.79 nicm 1237: if (yy <= gd->hscrolled)
1.80 nicm 1238: gd->hscrolled += lines - 1;
1.23 nicm 1239:
1.79 nicm 1240: /*
1241: * If the original line had the wrapped flag and there is still space
1242: * in the last new line, try to join with the next lines.
1243: */
1244: if (width < sx && (flags & GRID_LINE_WRAPPED))
1.91 nicm 1245: grid_reflow_join(target, gd, sx, yy, width, 1);
1.23 nicm 1246: }
1247:
1.79 nicm 1248: /* Reflow lines on grid to new width. */
1249: void
1.91 nicm 1250: grid_reflow(struct grid *gd, u_int sx)
1.23 nicm 1251: {
1.80 nicm 1252: struct grid *target;
1.79 nicm 1253: struct grid_line *gl;
1254: struct grid_cell gc;
1.91 nicm 1255: u_int yy, width, i, at, first;
1.23 nicm 1256:
1.79 nicm 1257: /*
1.80 nicm 1258: * Create a destination grid. This is just used as a container for the
1259: * line data and may not be fully valid.
1260: */
1261: target = grid_create(gd->sx, 0, 0);
1262:
1263: /*
1264: * Loop over each source line.
1.79 nicm 1265: */
1266: for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
1267: gl = &gd->linedata[yy];
1.80 nicm 1268: if (gl->flags & GRID_LINE_DEAD)
1269: continue;
1.23 nicm 1270:
1.80 nicm 1271: /*
1272: * Work out the width of this line. first is the width of the
1273: * first character, at is the point at which the available
1274: * width is hit, and width is the full line width.
1275: */
1.79 nicm 1276: first = at = width = 0;
1277: if (~gl->flags & GRID_LINE_EXTENDED) {
1278: first = 1;
1279: width = gl->cellused;
1280: if (width > sx)
1281: at = sx;
1282: else
1283: at = width;
1284: } else {
1285: for (i = 0; i < gl->cellused; i++) {
1286: grid_get_cell1(gl, i, &gc);
1287: if (i == 0)
1288: first = gc.data.width;
1289: if (at == 0 && width + gc.data.width > sx)
1290: at = i;
1291: width += gc.data.width;
1292: }
1293: }
1.23 nicm 1294:
1.79 nicm 1295: /*
1.80 nicm 1296: * If the line is exactly right or the first character is wider
1.108 nicm 1297: * than the target width, just move it across unchanged.
1.79 nicm 1298: */
1.80 nicm 1299: if (width == sx || first > sx) {
1300: grid_reflow_move(target, gl);
1.79 nicm 1301: continue;
1.80 nicm 1302: }
1.23 nicm 1303:
1.79 nicm 1304: /*
1305: * If the line is too big, it needs to be split, whether or not
1306: * it was previously wrapped.
1307: */
1308: if (width > sx) {
1.91 nicm 1309: grid_reflow_split(target, gd, sx, yy, at);
1.79 nicm 1310: continue;
1311: }
1.23 nicm 1312:
1.79 nicm 1313: /*
1314: * If the line was previously wrapped, join as much as possible
1315: * of the next line.
1316: */
1317: if (gl->flags & GRID_LINE_WRAPPED)
1.91 nicm 1318: grid_reflow_join(target, gd, sx, yy, width, 0);
1.80 nicm 1319: else
1320: grid_reflow_move(target, gl);
1321: }
1.55 nicm 1322:
1.80 nicm 1323: /*
1324: * Replace the old grid with the new.
1325: */
1326: if (target->sy < gd->sy)
1327: grid_reflow_add(target, gd->sy - target->sy);
1328: gd->hsize = target->sy - gd->sy;
1.92 nicm 1329: if (gd->hscrolled > gd->hsize)
1330: gd->hscrolled = gd->hsize;
1.80 nicm 1331: free(gd->linedata);
1332: gd->linedata = target->linedata;
1333: free(target);
1.91 nicm 1334: }
1.22 nicm 1335:
1.93 nicm 1336: /* Convert to position based on wrapped lines. */
1337: void
1338: grid_wrap_position(struct grid *gd, u_int px, u_int py, u_int *wx, u_int *wy)
1.91 nicm 1339: {
1.93 nicm 1340: u_int ax = 0, ay = 0, yy;
1.91 nicm 1341:
1.93 nicm 1342: for (yy = 0; yy < py; yy++) {
1343: if (gd->linedata[yy].flags & GRID_LINE_WRAPPED)
1344: ax += gd->linedata[yy].cellused;
1345: else {
1346: ax = 0;
1347: ay++;
1348: }
1.91 nicm 1349: }
1.93 nicm 1350: if (px >= gd->linedata[yy].cellused)
1351: ax = UINT_MAX;
1352: else
1353: ax += px;
1354: *wx = ax;
1355: *wy = ay;
1.91 nicm 1356: }
1357:
1.93 nicm 1358: /* Convert position based on wrapped lines back. */
1.91 nicm 1359: void
1.93 nicm 1360: grid_unwrap_position(struct grid *gd, u_int *px, u_int *py, u_int wx, u_int wy)
1.91 nicm 1361: {
1.105 nicm 1362: u_int yy, ay = 0;
1.91 nicm 1363:
1364: for (yy = 0; yy < gd->hsize + gd->sy - 1; yy++) {
1.93 nicm 1365: if (ay == wy)
1.91 nicm 1366: break;
1.105 nicm 1367: if (~gd->linedata[yy].flags & GRID_LINE_WRAPPED)
1.93 nicm 1368: ay++;
1.91 nicm 1369: }
1.93 nicm 1370:
1371: /*
1372: * yy is now 0 on the unwrapped line which contains wx. Walk forwards
1373: * until we find the end or the line now containing wx.
1374: */
1375: if (wx == UINT_MAX) {
1376: while (gd->linedata[yy].flags & GRID_LINE_WRAPPED)
1377: yy++;
1378: wx = gd->linedata[yy].cellused;
1379: } else {
1380: while (gd->linedata[yy].flags & GRID_LINE_WRAPPED) {
1381: if (wx < gd->linedata[yy].cellused)
1382: break;
1383: wx -= gd->linedata[yy].cellused;
1384: yy++;
1385: }
1386: }
1387: *px = wx;
1.91 nicm 1388: *py = yy;
1.95 nicm 1389: }
1390:
1391: /* Get length of line. */
1392: u_int
1393: grid_line_length(struct grid *gd, u_int py)
1394: {
1395: struct grid_cell gc;
1396: u_int px;
1397:
1398: px = grid_get_line(gd, py)->cellsize;
1399: if (px > gd->sx)
1400: px = gd->sx;
1401: while (px > 0) {
1402: grid_get_cell(gd, px - 1, py, &gc);
1.109 nicm 1403: if ((gc.flags & GRID_FLAG_PADDING) ||
1404: gc.data.size != 1 ||
1405: *gc.data.data != ' ')
1.95 nicm 1406: break;
1407: px--;
1408: }
1409: return (px);
1.1 nicm 1410: }