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