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