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