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