Coverage Report

Created: 2026-06-12 06:51

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