grid.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "tmux.h"
  21. /*
  22. * Grid data. This is the basic data structure that represents what is shown on
  23. * screen.
  24. *
  25. * A grid is a grid of cells (struct grid_cell). Lines are not allocated until
  26. * cells in that line are written to. The grid is split into history and
  27. * viewable data with the history starting at row (line) 0 and extending to
  28. * (hsize - 1); from hsize to hsize + (sy - 1) is the viewable data. All
  29. * functions in this file work on absolute coordinates, grid-view.c has
  30. * functions which work on the screen data.
  31. */
  32. /* Default grid cell data. */
  33. const struct grid_cell grid_default_cell = {
  34. 0, 0, { .fg = 8 }, { .bg = 8 }, { { ' ' }, 0, 1, 1 }
  35. };
  36. const struct grid_cell_entry grid_default_entry = {
  37. 0, { .data = { 0, 8, 8, ' ' } }
  38. };
  39. int grid_check_y(struct grid *, u_int);
  40. void grid_reflow_copy(struct grid_line *, u_int, struct grid_line *l,
  41. u_int, u_int);
  42. void grid_reflow_join(struct grid *, u_int *, struct grid_line *, u_int);
  43. void grid_reflow_split(struct grid *, u_int *, struct grid_line *, u_int,
  44. u_int);
  45. void grid_reflow_move(struct grid *, u_int *, struct grid_line *);
  46. size_t grid_string_cells_fg(const struct grid_cell *, int *);
  47. size_t grid_string_cells_bg(const struct grid_cell *, int *);
  48. void grid_string_cells_code(const struct grid_cell *,
  49. const struct grid_cell *, char *, size_t, int);
  50. /* Copy default into a cell. */
  51. static void
  52. grid_clear_cell(struct grid *gd, u_int px, u_int py)
  53. {
  54. gd->linedata[py].celldata[px] = grid_default_entry;
  55. }
  56. /* Check grid y position. */
  57. int
  58. grid_check_y(struct grid *gd, u_int py)
  59. {
  60. if ((py) >= (gd)->hsize + (gd)->sy) {
  61. log_debug("y out of range: %u", py);
  62. return (-1);
  63. }
  64. return (0);
  65. }
  66. /* Create a new grid. */
  67. struct grid *
  68. grid_create(u_int sx, u_int sy, u_int hlimit)
  69. {
  70. struct grid *gd;
  71. gd = xmalloc(sizeof *gd);
  72. gd->sx = sx;
  73. gd->sy = sy;
  74. gd->flags = GRID_HISTORY;
  75. gd->hsize = 0;
  76. gd->hlimit = hlimit;
  77. gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
  78. return (gd);
  79. }
  80. /* Destroy grid. */
  81. void
  82. grid_destroy(struct grid *gd)
  83. {
  84. struct grid_line *gl;
  85. u_int yy;
  86. for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
  87. gl = &gd->linedata[yy];
  88. free(gl->celldata);
  89. free(gl->extddata);
  90. }
  91. free(gd->linedata);
  92. free(gd);
  93. }
  94. /* Compare grids. */
  95. int
  96. grid_compare(struct grid *ga, struct grid *gb)
  97. {
  98. struct grid_line *gla, *glb;
  99. struct grid_cell gca, gcb;
  100. u_int xx, yy;
  101. if (ga->sx != gb->sx || ga->sy != gb->sy)
  102. return (1);
  103. for (yy = 0; yy < ga->sy; yy++) {
  104. gla = &ga->linedata[yy];
  105. glb = &gb->linedata[yy];
  106. if (gla->cellsize != glb->cellsize)
  107. return (1);
  108. for (xx = 0; xx < gla->cellsize; xx++) {
  109. grid_get_cell(ga, xx, yy, &gca);
  110. grid_get_cell(gb, xx, yy, &gcb);
  111. if (memcmp(&gca, &gcb, sizeof (struct grid_cell)) != 0)
  112. return (1);
  113. }
  114. }
  115. return (0);
  116. }
  117. /*
  118. * Collect lines from the history if at the limit. Free the top (oldest) 10%
  119. * and shift up.
  120. */
  121. void
  122. grid_collect_history(struct grid *gd)
  123. {
  124. u_int yy;
  125. if (gd->hsize < gd->hlimit)
  126. return;
  127. yy = gd->hlimit / 10;
  128. if (yy < 1)
  129. yy = 1;
  130. grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy);
  131. gd->hsize -= yy;
  132. }
  133. /*
  134. * Scroll the entire visible screen, moving one line into the history. Just
  135. * allocate a new line at the bottom and move the history size indicator.
  136. */
  137. void
  138. grid_scroll_history(struct grid *gd)
  139. {
  140. u_int yy;
  141. yy = gd->hsize + gd->sy;
  142. gd->linedata = xreallocarray(gd->linedata, yy + 1,
  143. sizeof *gd->linedata);
  144. memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
  145. gd->hsize++;
  146. }
  147. /* Clear the history. */
  148. void
  149. grid_clear_history(struct grid *gd)
  150. {
  151. grid_clear_lines(gd, 0, gd->hsize);
  152. grid_move_lines(gd, 0, gd->hsize, gd->sy);
  153. gd->hsize = 0;
  154. gd->linedata = xreallocarray(gd->linedata, gd->sy,
  155. sizeof *gd->linedata);
  156. }
  157. /* Scroll a region up, moving the top line into the history. */
  158. void
  159. grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower)
  160. {
  161. struct grid_line *gl_history, *gl_upper, *gl_lower;
  162. u_int yy;
  163. /* Create a space for a new line. */
  164. yy = gd->hsize + gd->sy;
  165. gd->linedata = xreallocarray(gd->linedata, yy + 1,
  166. sizeof *gd->linedata);
  167. /* Move the entire screen down to free a space for this line. */
  168. gl_history = &gd->linedata[gd->hsize];
  169. memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);
  170. /* Adjust the region and find its start and end. */
  171. upper++;
  172. gl_upper = &gd->linedata[upper];
  173. lower++;
  174. gl_lower = &gd->linedata[lower];
  175. /* Move the line into the history. */
  176. memcpy(gl_history, gl_upper, sizeof *gl_history);
  177. /* Then move the region up and clear the bottom line. */
  178. memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
  179. memset(gl_lower, 0, sizeof *gl_lower);
  180. /* Move the history offset down over the line. */
  181. gd->hsize++;
  182. }
  183. /* Expand line to fit to cell. */
  184. void
  185. grid_expand_line(struct grid *gd, u_int py, u_int sx)
  186. {
  187. struct grid_line *gl;
  188. u_int xx;
  189. gl = &gd->linedata[py];
  190. if (sx <= gl->cellsize)
  191. return;
  192. gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata);
  193. for (xx = gl->cellsize; xx < sx; xx++)
  194. grid_clear_cell(gd, xx, py);
  195. gl->cellsize = sx;
  196. }
  197. /* Peek at grid line. */
  198. const struct grid_line *
  199. grid_peek_line(struct grid *gd, u_int py)
  200. {
  201. if (grid_check_y(gd, py) != 0)
  202. return (NULL);
  203. return (&gd->linedata[py]);
  204. }
  205. /* Get cell for reading. */
  206. void
  207. grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
  208. {
  209. struct grid_line *gl;
  210. struct grid_cell_entry *gce;
  211. if (grid_check_y(gd, py) != 0 || px >= gd->linedata[py].cellsize) {
  212. memcpy(gc, &grid_default_cell, sizeof *gc);
  213. return;
  214. }
  215. gl = &gd->linedata[py];
  216. gce = &gl->celldata[px];
  217. if (gce->flags & GRID_FLAG_EXTENDED) {
  218. if (gce->offset >= gl->extdsize)
  219. memcpy(gc, &grid_default_cell, sizeof *gc);
  220. else
  221. memcpy(gc, &gl->extddata[gce->offset], sizeof *gc);
  222. return;
  223. }
  224. gc->flags = gce->flags & ~GRID_FLAG_EXTENDED;
  225. gc->attr = gce->data.attr;
  226. gc->fg = gce->data.fg;
  227. gc->bg = gce->data.bg;
  228. utf8_set(&gc->data, gce->data.data);
  229. }
  230. /* Set cell at relative position. */
  231. void
  232. grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
  233. {
  234. struct grid_line *gl;
  235. struct grid_cell_entry *gce;
  236. struct grid_cell *gcp;
  237. int extended;
  238. if (grid_check_y(gd, py) != 0)
  239. return;
  240. grid_expand_line(gd, py, px + 1);
  241. gl = &gd->linedata[py];
  242. gce = &gl->celldata[px];
  243. extended = (gce->flags & GRID_FLAG_EXTENDED);
  244. if (!extended && (gc->data.size != 1 || gc->data.width != 1))
  245. extended = 1;
  246. if (!extended && (gc->flags & (GRID_FLAG_FGRGB|GRID_FLAG_BGRGB)))
  247. extended = 1;
  248. if (extended) {
  249. if (~gce->flags & GRID_FLAG_EXTENDED) {
  250. gl->extddata = xreallocarray(gl->extddata,
  251. gl->extdsize + 1, sizeof *gl->extddata);
  252. gce->offset = gl->extdsize++;
  253. gce->flags = gc->flags | GRID_FLAG_EXTENDED;
  254. }
  255. if (gce->offset >= gl->extdsize)
  256. fatalx("offset too big");
  257. gcp = &gl->extddata[gce->offset];
  258. memcpy(gcp, gc, sizeof *gcp);
  259. return;
  260. }
  261. gce->flags = gc->flags & ~GRID_FLAG_EXTENDED;
  262. gce->data.attr = gc->attr;
  263. gce->data.fg = gc->fg;
  264. gce->data.bg = gc->bg;
  265. gce->data.data = gc->data.data[0];
  266. }
  267. /* Clear area. */
  268. void
  269. grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
  270. {
  271. u_int xx, yy;
  272. if (nx == 0 || ny == 0)
  273. return;
  274. if (px == 0 && nx == gd->sx) {
  275. grid_clear_lines(gd, py, ny);
  276. return;
  277. }
  278. if (grid_check_y(gd, py) != 0)
  279. return;
  280. if (grid_check_y(gd, py + ny - 1) != 0)
  281. return;
  282. for (yy = py; yy < py + ny; yy++) {
  283. if (px >= gd->linedata[yy].cellsize)
  284. continue;
  285. if (px + nx >= gd->linedata[yy].cellsize) {
  286. gd->linedata[yy].cellsize = px;
  287. continue;
  288. }
  289. for (xx = px; xx < px + nx; xx++) {
  290. if (xx >= gd->linedata[yy].cellsize)
  291. break;
  292. grid_clear_cell(gd, xx, yy);
  293. }
  294. }
  295. }
  296. /* Clear lines. This just frees and truncates the lines. */
  297. void
  298. grid_clear_lines(struct grid *gd, u_int py, u_int ny)
  299. {
  300. struct grid_line *gl;
  301. u_int yy;
  302. if (ny == 0)
  303. return;
  304. if (grid_check_y(gd, py) != 0)
  305. return;
  306. if (grid_check_y(gd, py + ny - 1) != 0)
  307. return;
  308. for (yy = py; yy < py + ny; yy++) {
  309. gl = &gd->linedata[yy];
  310. free(gl->celldata);
  311. free(gl->extddata);
  312. memset(gl, 0, sizeof *gl);
  313. }
  314. }
  315. /* Move a group of lines. */
  316. void
  317. grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
  318. {
  319. u_int yy;
  320. if (ny == 0 || py == dy)
  321. return;
  322. if (grid_check_y(gd, py) != 0)
  323. return;
  324. if (grid_check_y(gd, py + ny - 1) != 0)
  325. return;
  326. if (grid_check_y(gd, dy) != 0)
  327. return;
  328. if (grid_check_y(gd, dy + ny - 1) != 0)
  329. return;
  330. /* Free any lines which are being replaced. */
  331. for (yy = dy; yy < dy + ny; yy++) {
  332. if (yy >= py && yy < py + ny)
  333. continue;
  334. grid_clear_lines(gd, yy, 1);
  335. }
  336. memmove(&gd->linedata[dy], &gd->linedata[py],
  337. ny * (sizeof *gd->linedata));
  338. /* Wipe any lines that have been moved (without freeing them). */
  339. for (yy = py; yy < py + ny; yy++) {
  340. if (yy >= dy && yy < dy + ny)
  341. continue;
  342. memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
  343. }
  344. }
  345. /* Move a group of cells. */
  346. void
  347. grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
  348. {
  349. struct grid_line *gl;
  350. u_int xx;
  351. if (nx == 0 || px == dx)
  352. return;
  353. if (grid_check_y(gd, py) != 0)
  354. return;
  355. gl = &gd->linedata[py];
  356. grid_expand_line(gd, py, px + nx);
  357. grid_expand_line(gd, py, dx + nx);
  358. memmove(&gl->celldata[dx], &gl->celldata[px],
  359. nx * sizeof *gl->celldata);
  360. /* Wipe any cells that have been moved. */
  361. for (xx = px; xx < px + nx; xx++) {
  362. if (xx >= dx && xx < dx + nx)
  363. continue;
  364. grid_clear_cell(gd, xx, py);
  365. }
  366. }
  367. /* Get ANSI foreground sequence. */
  368. size_t
  369. grid_string_cells_fg(const struct grid_cell *gc, int *values)
  370. {
  371. size_t n;
  372. n = 0;
  373. if (gc->flags & GRID_FLAG_FG256) {
  374. values[n++] = 38;
  375. values[n++] = 5;
  376. values[n++] = gc->fg;
  377. } else if (gc->flags & GRID_FLAG_FGRGB) {
  378. values[n++] = 38;
  379. values[n++] = 2;
  380. values[n++] = gc->fg_rgb.r;
  381. values[n++] = gc->fg_rgb.g;
  382. values[n++] = gc->fg_rgb.b;
  383. } else {
  384. switch (gc->fg) {
  385. case 0:
  386. case 1:
  387. case 2:
  388. case 3:
  389. case 4:
  390. case 5:
  391. case 6:
  392. case 7:
  393. values[n++] = gc->fg + 30;
  394. break;
  395. case 8:
  396. values[n++] = 39;
  397. break;
  398. case 90:
  399. case 91:
  400. case 92:
  401. case 93:
  402. case 94:
  403. case 95:
  404. case 96:
  405. case 97:
  406. values[n++] = gc->fg;
  407. break;
  408. }
  409. }
  410. return (n);
  411. }
  412. /* Get ANSI background sequence. */
  413. size_t
  414. grid_string_cells_bg(const struct grid_cell *gc, int *values)
  415. {
  416. size_t n;
  417. n = 0;
  418. if (gc->flags & GRID_FLAG_BG256) {
  419. values[n++] = 48;
  420. values[n++] = 5;
  421. values[n++] = gc->bg;
  422. } else if (gc->flags & GRID_FLAG_BGRGB) {
  423. values[n++] = 48;
  424. values[n++] = 2;
  425. values[n++] = gc->bg_rgb.r;
  426. values[n++] = gc->bg_rgb.g;
  427. values[n++] = gc->bg_rgb.b;
  428. } else {
  429. switch (gc->bg) {
  430. case 0:
  431. case 1:
  432. case 2:
  433. case 3:
  434. case 4:
  435. case 5:
  436. case 6:
  437. case 7:
  438. values[n++] = gc->bg + 40;
  439. break;
  440. case 8:
  441. values[n++] = 49;
  442. break;
  443. case 100:
  444. case 101:
  445. case 102:
  446. case 103:
  447. case 104:
  448. case 105:
  449. case 106:
  450. case 107:
  451. values[n++] = gc->bg - 10;
  452. break;
  453. }
  454. }
  455. return (n);
  456. }
  457. /*
  458. * Returns ANSI code to set particular attributes (colour, bold and so on)
  459. * given a current state. The output buffer must be able to hold at least 57
  460. * bytes.
  461. */
  462. void
  463. grid_string_cells_code(const struct grid_cell *lastgc,
  464. const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
  465. {
  466. int oldc[64], newc[64], s[128];
  467. size_t noldc, nnewc, n, i;
  468. u_int attr = gc->attr;
  469. u_int lastattr = lastgc->attr;
  470. char tmp[64];
  471. struct {
  472. u_int mask;
  473. u_int code;
  474. } attrs[] = {
  475. { GRID_ATTR_BRIGHT, 1 },
  476. { GRID_ATTR_DIM, 2 },
  477. { GRID_ATTR_ITALICS, 3 },
  478. { GRID_ATTR_UNDERSCORE, 4 },
  479. { GRID_ATTR_BLINK, 5 },
  480. { GRID_ATTR_REVERSE, 7 },
  481. { GRID_ATTR_HIDDEN, 8 }
  482. };
  483. n = 0;
  484. /* If any attribute is removed, begin with 0. */
  485. for (i = 0; i < nitems(attrs); i++) {
  486. if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
  487. s[n++] = 0;
  488. lastattr &= GRID_ATTR_CHARSET;
  489. break;
  490. }
  491. }
  492. /* For each attribute that is newly set, add its code. */
  493. for (i = 0; i < nitems(attrs); i++) {
  494. if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
  495. s[n++] = attrs[i].code;
  496. }
  497. /* If the foreground colour changed, append its parameters. */
  498. nnewc = grid_string_cells_fg(gc, newc);
  499. noldc = grid_string_cells_fg(lastgc, oldc);
  500. if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
  501. for (i = 0; i < nnewc; i++)
  502. s[n++] = newc[i];
  503. }
  504. /* If the background colour changed, append its parameters. */
  505. nnewc = grid_string_cells_bg(gc, newc);
  506. noldc = grid_string_cells_bg(lastgc, oldc);
  507. if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
  508. for (i = 0; i < nnewc; i++)
  509. s[n++] = newc[i];
  510. }
  511. /* If there are any parameters, append an SGR code. */
  512. *buf = '\0';
  513. if (n > 0) {
  514. if (escape_c0)
  515. strlcat(buf, "\\033[", len);
  516. else
  517. strlcat(buf, "\033[", len);
  518. for (i = 0; i < n; i++) {
  519. if (i + 1 < n)
  520. xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
  521. else
  522. xsnprintf(tmp, sizeof tmp, "%d", s[i]);
  523. strlcat(buf, tmp, len);
  524. }
  525. strlcat(buf, "m", len);
  526. }
  527. /* Append shift in/shift out if needed. */
  528. if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
  529. if (escape_c0)
  530. strlcat(buf, "\\016", len); /* SO */
  531. else
  532. strlcat(buf, "\016", len); /* SO */
  533. }
  534. if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
  535. if (escape_c0)
  536. strlcat(buf, "\\017", len); /* SI */
  537. else
  538. strlcat(buf, "\017", len); /* SI */
  539. }
  540. }
  541. /* Convert cells into a string. */
  542. char *
  543. grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
  544. struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
  545. {
  546. struct grid_cell gc;
  547. static struct grid_cell lastgc1;
  548. const char *data;
  549. char *buf, code[128];
  550. size_t len, off, size, codelen;
  551. u_int xx;
  552. const struct grid_line *gl;
  553. if (lastgc != NULL && *lastgc == NULL) {
  554. memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
  555. *lastgc = &lastgc1;
  556. }
  557. len = 128;
  558. buf = xmalloc(len);
  559. off = 0;
  560. gl = grid_peek_line(gd, py);
  561. for (xx = px; xx < px + nx; xx++) {
  562. if (gl == NULL || xx >= gl->cellsize)
  563. break;
  564. grid_get_cell(gd, xx, py, &gc);
  565. if (gc.flags & GRID_FLAG_PADDING)
  566. continue;
  567. if (with_codes) {
  568. grid_string_cells_code(*lastgc, &gc, code, sizeof code,
  569. escape_c0);
  570. codelen = strlen(code);
  571. memcpy(*lastgc, &gc, sizeof **lastgc);
  572. } else
  573. codelen = 0;
  574. data = gc.data.data;
  575. size = gc.data.size;
  576. if (escape_c0 && size == 1 && *data == '\\') {
  577. data = "\\\\";
  578. size = 2;
  579. }
  580. while (len < off + size + codelen + 1) {
  581. buf = xreallocarray(buf, 2, len);
  582. len *= 2;
  583. }
  584. if (codelen != 0) {
  585. memcpy(buf + off, code, codelen);
  586. off += codelen;
  587. }
  588. memcpy(buf + off, data, size);
  589. off += size;
  590. }
  591. if (trim) {
  592. while (off > 0 && buf[off - 1] == ' ')
  593. off--;
  594. }
  595. buf[off] = '\0';
  596. return (buf);
  597. }
  598. /*
  599. * Duplicate a set of lines between two grids. If there aren't enough lines in
  600. * either source or destination, the number of lines is limited to the number
  601. * available.
  602. */
  603. void
  604. grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
  605. u_int ny)
  606. {
  607. struct grid_line *dstl, *srcl;
  608. u_int yy;
  609. if (dy + ny > dst->hsize + dst->sy)
  610. ny = dst->hsize + dst->sy - dy;
  611. if (sy + ny > src->hsize + src->sy)
  612. ny = src->hsize + src->sy - sy;
  613. grid_clear_lines(dst, dy, ny);
  614. for (yy = 0; yy < ny; yy++) {
  615. srcl = &src->linedata[sy];
  616. dstl = &dst->linedata[dy];
  617. memcpy(dstl, srcl, sizeof *dstl);
  618. if (srcl->cellsize != 0) {
  619. dstl->celldata = xreallocarray(NULL,
  620. srcl->cellsize, sizeof *dstl->celldata);
  621. memcpy(dstl->celldata, srcl->celldata,
  622. srcl->cellsize * sizeof *dstl->celldata);
  623. } else
  624. dstl->celldata = NULL;
  625. if (srcl->extdsize != 0) {
  626. dstl->extdsize = srcl->extdsize;
  627. dstl->extddata = xreallocarray(NULL, dstl->extdsize,
  628. sizeof *dstl->extddata);
  629. memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
  630. sizeof *dstl->extddata);
  631. }
  632. sy++;
  633. dy++;
  634. }
  635. }
  636. /* Copy a section of a line. */
  637. void
  638. grid_reflow_copy(struct grid_line *dst_gl, u_int to, struct grid_line *src_gl,
  639. u_int from, u_int to_copy)
  640. {
  641. struct grid_cell_entry *gce;
  642. u_int i, was;
  643. memcpy(&dst_gl->celldata[to], &src_gl->celldata[from],
  644. to_copy * sizeof *dst_gl->celldata);
  645. for (i = to; i < to + to_copy; i++) {
  646. gce = &dst_gl->celldata[i];
  647. if (~gce->flags & GRID_FLAG_EXTENDED)
  648. continue;
  649. was = gce->offset;
  650. dst_gl->extddata = xreallocarray(dst_gl->extddata,
  651. dst_gl->extdsize + 1, sizeof *dst_gl->extddata);
  652. gce->offset = dst_gl->extdsize++;
  653. memcpy(&dst_gl->extddata[gce->offset], &src_gl->extddata[was],
  654. sizeof *dst_gl->extddata);
  655. }
  656. }
  657. /* Join line data. */
  658. void
  659. grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
  660. u_int new_x)
  661. {
  662. struct grid_line *dst_gl = &dst->linedata[(*py) - 1];
  663. u_int left, to_copy, ox, nx;
  664. /* How much is left on the old line? */
  665. left = new_x - dst_gl->cellsize;
  666. /* Work out how much to append. */
  667. to_copy = src_gl->cellsize;
  668. if (to_copy > left)
  669. to_copy = left;
  670. ox = dst_gl->cellsize;
  671. nx = ox + to_copy;
  672. /* Resize the destination line. */
  673. dst_gl->celldata = xreallocarray(dst_gl->celldata, nx,
  674. sizeof *dst_gl->celldata);
  675. dst_gl->cellsize = nx;
  676. /* Append as much as possible. */
  677. grid_reflow_copy(dst_gl, ox, src_gl, 0, to_copy);
  678. /* If there is any left in the source, split it. */
  679. if (src_gl->cellsize > to_copy) {
  680. dst_gl->flags |= GRID_LINE_WRAPPED;
  681. src_gl->cellsize -= to_copy;
  682. grid_reflow_split(dst, py, src_gl, new_x, to_copy);
  683. }
  684. }
  685. /* Split line data. */
  686. void
  687. grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
  688. u_int new_x, u_int offset)
  689. {
  690. struct grid_line *dst_gl = NULL;
  691. u_int to_copy;
  692. /* Loop and copy sections of the source line. */
  693. while (src_gl->cellsize > 0) {
  694. /* Create new line. */
  695. if (*py >= dst->hsize + dst->sy)
  696. grid_scroll_history(dst);
  697. dst_gl = &dst->linedata[*py];
  698. (*py)++;
  699. /* How much should we copy? */
  700. to_copy = new_x;
  701. if (to_copy > src_gl->cellsize)
  702. to_copy = src_gl->cellsize;
  703. /* Expand destination line. */
  704. dst_gl->celldata = xreallocarray(NULL, to_copy,
  705. sizeof *dst_gl->celldata);
  706. dst_gl->cellsize = to_copy;
  707. dst_gl->flags |= GRID_LINE_WRAPPED;
  708. /* Copy the data. */
  709. grid_reflow_copy(dst_gl, 0, src_gl, offset, to_copy);
  710. /* Move offset and reduce old line size. */
  711. offset += to_copy;
  712. src_gl->cellsize -= to_copy;
  713. }
  714. /* Last line is not wrapped. */
  715. if (dst_gl != NULL)
  716. dst_gl->flags &= ~GRID_LINE_WRAPPED;
  717. }
  718. /* Move line data. */
  719. void
  720. grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl)
  721. {
  722. struct grid_line *dst_gl;
  723. /* Create new line. */
  724. if (*py >= dst->hsize + dst->sy)
  725. grid_scroll_history(dst);
  726. dst_gl = &dst->linedata[*py];
  727. (*py)++;
  728. /* Copy the old line. */
  729. memcpy(dst_gl, src_gl, sizeof *dst_gl);
  730. dst_gl->flags &= ~GRID_LINE_WRAPPED;
  731. /* Clear old line. */
  732. src_gl->celldata = NULL;
  733. src_gl->extddata = NULL;
  734. }
  735. /*
  736. * Reflow lines from src grid into dst grid of width new_x. Returns number of
  737. * lines fewer in the visible area. The source grid is destroyed.
  738. */
  739. u_int
  740. grid_reflow(struct grid *dst, struct grid *src, u_int new_x)
  741. {
  742. u_int py, sy, line;
  743. int previous_wrapped;
  744. struct grid_line *src_gl;
  745. py = 0;
  746. sy = src->sy;
  747. previous_wrapped = 0;
  748. for (line = 0; line < sy + src->hsize; line++) {
  749. src_gl = src->linedata + line;
  750. if (!previous_wrapped) {
  751. /* Wasn't wrapped. If smaller, move to destination. */
  752. if (src_gl->cellsize <= new_x)
  753. grid_reflow_move(dst, &py, src_gl);
  754. else
  755. grid_reflow_split(dst, &py, src_gl, new_x, 0);
  756. } else {
  757. /* Previous was wrapped. Try to join. */
  758. grid_reflow_join(dst, &py, src_gl, new_x);
  759. }
  760. previous_wrapped = (src_gl->flags & GRID_LINE_WRAPPED);
  761. }
  762. grid_destroy(src);
  763. if (py > sy)
  764. return (0);
  765. return (sy - py);
  766. }