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