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