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