Coverage Report

Created: 2025-10-10 06:33

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