blob: f80048b68fa4b617d74dd8ded979581a16b8fc7c [file] [log] [blame] [raw]
Nicholas Marriott35876ea2009-06-01 22:58:49 +00001/* $OpenBSD$ */
2
3/*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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
Nicholas Marriottdf912e32012-07-10 11:53:01 +000021#include <stdlib.h>
Nicholas Marriott35876ea2009-06-01 22:58:49 +000022#include <string.h>
23
24#include "tmux.h"
25
Nicholas Marriott43d62c12009-10-17 08:24:46 +000026void screen_write_initctx(struct screen_write_ctx *, struct tty_ctx *, int);
Nicholas Marriott5afb8202010-06-21 00:11:12 +000027void screen_write_overwrite(struct screen_write_ctx *, u_int);
nicm0a1a88d2014-10-20 23:57:13 +000028int screen_write_combine(struct screen_write_ctx *,
29 const struct utf8_data *);
Nicholas Marriott35876ea2009-06-01 22:58:49 +000030
31/* Initialise writing with a window. */
32void
nicm0a1a88d2014-10-20 23:57:13 +000033screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp,
34 struct screen *s)
Nicholas Marriott35876ea2009-06-01 22:58:49 +000035{
36 ctx->wp = wp;
37 if (wp != NULL && s == NULL)
38 ctx->s = wp->screen;
39 else
40 ctx->s = s;
41}
42
43/* Finish writing. */
44void
45screen_write_stop(unused struct screen_write_ctx *ctx)
46{
47}
48
Nicholas Marriott66f04512012-01-21 08:10:21 +000049/* Reset screen state. */
50void
51screen_write_reset(struct screen_write_ctx *ctx)
52{
Nicholas Marriott10682b92013-03-21 18:47:56 +000053 struct screen *s = ctx->s;
Nicholas Marriott66f04512012-01-21 08:10:21 +000054
Nicholas Marriott10682b92013-03-21 18:47:56 +000055 screen_reset_tabs(s);
56 screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
Nicholas Marriott67b4d5b2013-03-22 10:33:50 +000057
Nicholas Marriott772d61f2013-05-15 15:32:14 +000058 s->mode &= ~(MODE_INSERT|MODE_KCURSOR|MODE_KKEYPAD|MODE_FOCUSON);
Nicholas Marriott67b4d5b2013-03-22 10:33:50 +000059 s->mode &= ~(ALL_MOUSE_MODES|MODE_MOUSE_UTF8|MODE_MOUSE_SGR);
Nicholas Marriott66f04512012-01-21 08:10:21 +000060
61 screen_write_clearscreen(ctx);
62 screen_write_cursormove(ctx, 0, 0);
63}
64
Nicholas Marriott35876ea2009-06-01 22:58:49 +000065/* Write character. */
66void
nicma5d4b7f2014-04-17 14:45:49 +000067screen_write_putc(struct screen_write_ctx *ctx, struct grid_cell *gc,
68 u_char ch)
Nicholas Marriott35876ea2009-06-01 22:58:49 +000069{
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +000070 grid_cell_one(gc, ch);
71 screen_write_cell(ctx, gc);
Nicholas Marriott35876ea2009-06-01 22:58:49 +000072}
73
Nicholas Marriotte97006b2009-09-07 10:49:32 +000074/* Calculate string length, with embedded formatting. */
nicm0a1a88d2014-10-20 23:57:13 +000075size_t
Nicholas Marriotte97006b2009-09-07 10:49:32 +000076screen_write_cstrlen(int utf8flag, const char *fmt, ...)
77{
78 va_list ap;
79 char *msg, *msg2, *ptr, *ptr2;
80 size_t size;
81
82 va_start(ap, fmt);
83 xvasprintf(&msg, fmt, ap);
84 va_end(ap);
85 msg2 = xmalloc(strlen(msg) + 1);
86
87 ptr = msg;
88 ptr2 = msg2;
89 while (*ptr != '\0') {
90 if (ptr[0] == '#' && ptr[1] == '[') {
91 while (*ptr != ']' && *ptr != '\0')
92 ptr++;
93 if (*ptr == ']')
94 ptr++;
95 continue;
96 }
97 *ptr2++ = *ptr++;
98 }
99 *ptr2 = '\0';
100
101 size = screen_write_strlen(utf8flag, "%s", msg2);
102
Nicholas Marriottdf912e32012-07-10 11:53:01 +0000103 free(msg);
104 free(msg2);
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000105
106 return (size);
107}
108
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000109/* Calculate string length. */
nicm0a1a88d2014-10-20 23:57:13 +0000110size_t
Nicholas Marriott65214272009-06-03 16:54:26 +0000111screen_write_strlen(int utf8flag, const char *fmt, ...)
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000112{
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000113 va_list ap;
114 char *msg;
115 struct utf8_data utf8data;
116 u_char *ptr;
117 size_t left, size = 0;
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000118
119 va_start(ap, fmt);
120 xvasprintf(&msg, fmt, ap);
121 va_end(ap);
122
123 ptr = msg;
124 while (*ptr != '\0') {
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000125 if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) {
126 ptr++;
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000127
128 left = strlen(ptr);
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000129 if (left < utf8data.size - 1)
130 break;
131 while (utf8_append(&utf8data, *ptr))
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000132 ptr++;
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000133 ptr++;
134
135 size += utf8data.width;
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000136 } else {
137 size++;
138 ptr++;
139 }
Ray Lai74749e82009-06-05 03:13:16 +0000140 }
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000141
Nicholas Marriottdf912e32012-07-10 11:53:01 +0000142 free(msg);
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000143 return (size);
144}
145
Nicholas Marriott65214272009-06-03 16:54:26 +0000146/* Write simple string (no UTF-8 or maximum length). */
nicm0a1a88d2014-10-20 23:57:13 +0000147void
148screen_write_puts(struct screen_write_ctx *ctx, struct grid_cell *gc,
149 const char *fmt, ...)
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000150{
151 va_list ap;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000152
153 va_start(ap, fmt);
Nicholas Marriott65214272009-06-03 16:54:26 +0000154 screen_write_vnputs(ctx, -1, gc, 0, fmt, ap);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000155 va_end(ap);
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000156}
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000157
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000158/* Write string with length limit (-1 for unlimited). */
nicm0a1a88d2014-10-20 23:57:13 +0000159void
160screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
161 struct grid_cell *gc, int utf8flag, const char *fmt, ...)
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000162{
163 va_list ap;
164
165 va_start(ap, fmt);
Nicholas Marriott65214272009-06-03 16:54:26 +0000166 screen_write_vnputs(ctx, maxlen, gc, utf8flag, fmt, ap);
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000167 va_end(ap);
168}
169
170void
Nicholas Marriott65214272009-06-03 16:54:26 +0000171screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
172 struct grid_cell *gc, int utf8flag, const char *fmt, va_list ap)
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000173{
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000174 char *msg;
175 struct utf8_data utf8data;
176 u_char *ptr;
177 size_t left, size = 0;
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000178
179 xvasprintf(&msg, fmt, ap);
180
181 ptr = msg;
182 while (*ptr != '\0') {
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000183 if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) {
184 ptr++;
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000185
186 left = strlen(ptr);
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000187 if (left < utf8data.size - 1)
188 break;
189 while (utf8_append(&utf8data, *ptr))
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000190 ptr++;
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000191 ptr++;
Ray Lai74749e82009-06-05 03:13:16 +0000192
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000193 if (maxlen > 0 &&
194 size + utf8data.width > (size_t) maxlen) {
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000195 while (size < (size_t) maxlen) {
196 screen_write_putc(ctx, gc, ' ');
197 size++;
198 }
199 break;
200 }
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000201 size += utf8data.width;
Nicholas Marriott15a64b82009-12-03 22:50:09 +0000202
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000203 grid_cell_set(gc, &utf8data);
204 screen_write_cell(ctx, gc);
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000205 } else {
Nicholas Marriott1ec736a2009-06-26 15:13:39 +0000206 if (maxlen > 0 && size + 1 > (size_t) maxlen)
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000207 break;
208
Nicholas Marriotteb0ad182012-09-24 12:53:55 +0000209 if (*ptr == '\001')
210 gc->attr ^= GRID_ATTR_CHARSET;
211 else {
212 size++;
213 screen_write_putc(ctx, gc, *ptr);
214 }
Nicholas Marriott7d45e292009-06-03 16:05:46 +0000215 ptr++;
216 }
217 }
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000218
Nicholas Marriottdf912e32012-07-10 11:53:01 +0000219 free(msg);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000220}
221
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000222/* Write string, similar to nputs, but with embedded formatting (#[]). */
nicm0a1a88d2014-10-20 23:57:13 +0000223void
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000224screen_write_cnputs(struct screen_write_ctx *ctx,
225 ssize_t maxlen, struct grid_cell *gc, int utf8flag, const char *fmt, ...)
226{
227 struct grid_cell lgc;
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000228 struct utf8_data utf8data;
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000229 va_list ap;
230 char *msg;
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000231 u_char *ptr, *last;
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000232 size_t left, size = 0;
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000233
234 va_start(ap, fmt);
235 xvasprintf(&msg, fmt, ap);
236 va_end(ap);
237
238 memcpy(&lgc, gc, sizeof lgc);
239
240 ptr = msg;
241 while (*ptr != '\0') {
242 if (ptr[0] == '#' && ptr[1] == '[') {
243 ptr += 2;
244 last = ptr + strcspn(ptr, "]");
245 if (*last == '\0') {
246 /* No ]. Not much point in doing anything. */
247 break;
248 }
249 *last = '\0';
250
nicm945339b2014-01-28 23:07:09 +0000251 style_parse(gc, &lgc, ptr);
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000252 ptr = last + 1;
253 continue;
254 }
255
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000256 if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) {
257 ptr++;
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000258
259 left = strlen(ptr);
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000260 if (left < utf8data.size - 1)
261 break;
262 while (utf8_append(&utf8data, *ptr))
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000263 ptr++;
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000264 ptr++;
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000265
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000266 if (maxlen > 0 &&
267 size + utf8data.width > (size_t) maxlen) {
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000268 while (size < (size_t) maxlen) {
269 screen_write_putc(ctx, gc, ' ');
270 size++;
271 }
272 break;
273 }
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000274 size += utf8data.width;
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000275
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000276 grid_cell_set(&lgc, &utf8data);
277 screen_write_cell(ctx, &lgc);
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000278 } else {
279 if (maxlen > 0 && size + 1 > (size_t) maxlen)
280 break;
281
282 size++;
283 screen_write_putc(ctx, &lgc, *ptr);
284 ptr++;
285 }
286 }
287
Nicholas Marriottdf912e32012-07-10 11:53:01 +0000288 free(msg);
Nicholas Marriotte97006b2009-09-07 10:49:32 +0000289}
290
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000291/* Copy from another screen. */
292void
293screen_write_copy(struct screen_write_ctx *ctx,
294 struct screen *src, u_int px, u_int py, u_int nx, u_int ny)
295{
296 struct screen *s = ctx->s;
297 struct grid *gd = src->grid;
Nicholas Marriottb02e4292009-08-13 16:24:33 +0000298 struct grid_line *gl;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000299 const struct grid_cell *gc;
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000300 struct utf8_data ud;
Nicholas Marriotta78cc982009-11-18 17:02:17 +0000301 u_int xx, yy, cx, cy, ax, bx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000302
303 cx = s->cx;
304 cy = s->cy;
305 for (yy = py; yy < py + ny; yy++) {
Nicholas Marriottb02e4292009-08-13 16:24:33 +0000306 gl = &gd->linedata[yy];
Nicholas Marriott42781992009-09-15 15:14:09 +0000307 if (yy < gd->hsize + gd->sy) {
308 /*
309 * Find start and end position and copy between
310 * them. Limit to the real end of the line then use a
311 * clear EOL only if copying to the end, otherwise
312 * could overwrite whatever is there already.
313 */
314 if (px > gl->cellsize)
315 ax = gl->cellsize;
316 else
317 ax = px;
318 if (px + nx == gd->sx && px + nx > gl->cellsize)
319 bx = gl->cellsize;
320 else
321 bx = px + nx;
Nicholas Marriott15a64b82009-12-03 22:50:09 +0000322
Nicholas Marriott42781992009-09-15 15:14:09 +0000323 for (xx = ax; xx < bx; xx++) {
Nicholas Marriott42781992009-09-15 15:14:09 +0000324 if (xx >= gl->cellsize)
325 gc = &grid_default_cell;
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000326 else
Nicholas Marriott42781992009-09-15 15:14:09 +0000327 gc = &gl->celldata[xx];
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000328 grid_cell_get(gc, &ud);
329 screen_write_cell(ctx, gc);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000330 }
Nicholas Marriott42781992009-09-15 15:14:09 +0000331 if (px + nx == gd->sx && px + nx > gl->cellsize)
332 screen_write_clearendofline(ctx);
333 } else
Nicholas Marriott1af09d62009-10-20 19:18:28 +0000334 screen_write_clearline(ctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000335 cy++;
336 screen_write_cursormove(ctx, cx, cy);
337 }
338}
339
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000340/* Set up context for TTY command. */
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000341void
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000342screen_write_initctx(
343 struct screen_write_ctx *ctx, struct tty_ctx *ttyctx, int save_last)
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000344{
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000345 struct screen *s = ctx->s;
346 struct grid *gd = s->grid;
347 const struct grid_cell *gc;
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000348 u_int xx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000349
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000350 ttyctx->wp = ctx->wp;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000351
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000352 ttyctx->ocx = s->cx;
353 ttyctx->ocy = s->cy;
354
355 ttyctx->orlower = s->rlower;
356 ttyctx->orupper = s->rupper;
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000357
358 if (!save_last)
359 return;
360
361 /* Save the last cell on the screen. */
Nicholas Marriott5d7bff42009-11-16 13:40:45 +0000362 gc = &grid_default_cell;
363 for (xx = 1; xx <= screen_size_x(s); xx++) {
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000364 gc = grid_view_peek_cell(gd, screen_size_x(s) - xx, s->cy);
365 if (!(gc->flags & GRID_FLAG_PADDING))
366 break;
367 }
368 ttyctx->last_width = xx;
369 memcpy(&ttyctx->last_cell, gc, sizeof ttyctx->last_cell);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000370}
371
Nicholas Marriott10682b92013-03-21 18:47:56 +0000372/* Set a mode. */
373void
374screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
375{
376 struct screen *s = ctx->s;
377
378 s->mode |= mode;
379}
380
381/* Clear a mode. */
382void
383screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
384{
385 struct screen *s = ctx->s;
386
387 s->mode &= ~mode;
388}
389
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000390/* Cursor up by ny. */
391void
392screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
393{
394 struct screen *s = ctx->s;
395
396 if (ny == 0)
397 ny = 1;
398
Nicholas Marriott5f2f07e2009-07-09 17:57:11 +0000399 if (s->cy < s->rupper) {
400 /* Above region. */
401 if (ny > s->cy)
402 ny = s->cy;
403 } else {
404 /* Below region. */
405 if (ny > s->cy - s->rupper)
406 ny = s->cy - s->rupper;
407 }
Nicholas Marriott5e4d9a32013-03-27 11:19:19 +0000408 if (s->cx == screen_size_x(s))
409 s->cx--;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000410 if (ny == 0)
411 return;
412
413 s->cy -= ny;
414}
415
416/* Cursor down by ny. */
417void
418screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
419{
420 struct screen *s = ctx->s;
421
422 if (ny == 0)
423 ny = 1;
424
Nicholas Marriott5f2f07e2009-07-09 17:57:11 +0000425 if (s->cy > s->rlower) {
426 /* Below region. */
427 if (ny > screen_size_y(s) - 1 - s->cy)
428 ny = screen_size_y(s) - 1 - s->cy;
429 } else {
430 /* Above region. */
431 if (ny > s->rlower - s->cy)
432 ny = s->rlower - s->cy;
433 }
Nicholas Marriott5e4d9a32013-03-27 11:19:19 +0000434 if (s->cx == screen_size_x(s))
435 s->cx--;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000436 if (ny == 0)
437 return;
438
439 s->cy += ny;
440}
441
442/* Cursor right by nx. */
443void
444screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
445{
446 struct screen *s = ctx->s;
447
448 if (nx == 0)
449 nx = 1;
450
451 if (nx > screen_size_x(s) - 1 - s->cx)
452 nx = screen_size_x(s) - 1 - s->cx;
453 if (nx == 0)
454 return;
455
456 s->cx += nx;
457}
458
459/* Cursor left by nx. */
460void
461screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
462{
463 struct screen *s = ctx->s;
464
465 if (nx == 0)
466 nx = 1;
467
468 if (nx > s->cx)
469 nx = s->cx;
470 if (nx == 0)
471 return;
472
473 s->cx -= nx;
474}
475
Nicholas Marriott8608c692009-10-12 16:59:55 +0000476/* Backspace; cursor left unless at start of wrapped line when can move up. */
477void
478screen_write_backspace(struct screen_write_ctx *ctx)
479{
480 struct screen *s = ctx->s;
481 struct grid_line *gl;
482
483 if (s->cx == 0) {
484 if (s->cy == 0)
485 return;
486 gl = &s->grid->linedata[s->grid->hsize + s->cy - 1];
487 if (gl->flags & GRID_LINE_WRAPPED) {
488 s->cy--;
489 s->cx = screen_size_x(s) - 1;
490 }
491 } else
492 s->cx--;
493}
494
Nicholas Marriott52ec9b92009-06-03 23:30:40 +0000495/* VT100 alignment test. */
496void
497screen_write_alignmenttest(struct screen_write_ctx *ctx)
498{
499 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000500 struct tty_ctx ttyctx;
Nicholas Marriott52ec9b92009-06-03 23:30:40 +0000501 struct grid_cell gc;
502 u_int xx, yy;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000503
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000504 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott52ec9b92009-06-03 23:30:40 +0000505
506 memcpy(&gc, &grid_default_cell, sizeof gc);
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000507 grid_cell_one(&gc, 'E');
Ray Lai74749e82009-06-05 03:13:16 +0000508
Nicholas Marriott52ec9b92009-06-03 23:30:40 +0000509 for (yy = 0; yy < screen_size_y(s); yy++) {
510 for (xx = 0; xx < screen_size_x(s); xx++)
511 grid_view_set_cell(s->grid, xx, yy, &gc);
512 }
Ray Lai74749e82009-06-05 03:13:16 +0000513
Nicholas Marriott52ec9b92009-06-03 23:30:40 +0000514 s->cx = 0;
515 s->cy = 0;
516
517 s->rupper = 0;
Nicholas Marriott8608c692009-10-12 16:59:55 +0000518
Nicholas Marriott52ec9b92009-06-03 23:30:40 +0000519 s->rlower = screen_size_y(s) - 1;
520
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000521 tty_write(tty_cmd_alignmenttest, &ttyctx);
Nicholas Marriott52ec9b92009-06-03 23:30:40 +0000522}
523
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000524/* Insert nx characters. */
525void
526screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx)
527{
528 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000529 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000530
531 if (nx == 0)
532 nx = 1;
533
Nicholas Marriott68e06fb2009-06-29 21:30:50 +0000534 if (nx > screen_size_x(s) - s->cx)
535 nx = screen_size_x(s) - s->cx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000536 if (nx == 0)
537 return;
538
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000539 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000540
541 if (s->cx <= screen_size_x(s) - 1)
542 grid_view_insert_cells(s->grid, s->cx, s->cy, nx);
543
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000544 ttyctx.num = nx;
545 tty_write(tty_cmd_insertcharacter, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000546}
547
548/* Delete nx characters. */
549void
550screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx)
551{
552 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000553 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000554
555 if (nx == 0)
556 nx = 1;
557
Nicholas Marriott68e06fb2009-06-29 21:30:50 +0000558 if (nx > screen_size_x(s) - s->cx)
559 nx = screen_size_x(s) - s->cx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000560 if (nx == 0)
561 return;
562
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000563 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000564
565 if (s->cx <= screen_size_x(s) - 1)
566 grid_view_delete_cells(s->grid, s->cx, s->cy, nx);
567
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000568 ttyctx.num = nx;
569 tty_write(tty_cmd_deletecharacter, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000570}
571
Nicholas Marriott44f8e1c2013-01-15 23:18:55 +0000572/* Clear nx characters. */
573void
574screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx)
575{
576 struct screen *s = ctx->s;
577 struct tty_ctx ttyctx;
578
579 if (nx == 0)
580 nx = 1;
581
582 if (nx > screen_size_x(s) - s->cx)
583 nx = screen_size_x(s) - s->cx;
584 if (nx == 0)
585 return;
586
587 screen_write_initctx(ctx, &ttyctx, 0);
588
589 if (s->cx <= screen_size_x(s) - 1)
590 grid_view_clear(s->grid, s->cx, s->cy, nx, 1);
591
592 ttyctx.num = nx;
593 tty_write(tty_cmd_clearcharacter, &ttyctx);
594}
595
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000596/* Insert ny lines. */
597void
598screen_write_insertline(struct screen_write_ctx *ctx, u_int ny)
599{
600 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000601 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000602
603 if (ny == 0)
604 ny = 1;
605
Nicholas Marriott86c93c62009-07-09 07:58:14 +0000606 if (s->cy < s->rupper || s->cy > s->rlower) {
607 if (ny > screen_size_y(s) - s->cy)
608 ny = screen_size_y(s) - s->cy;
609 if (ny == 0)
610 return;
611
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000612 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott86c93c62009-07-09 07:58:14 +0000613
614 grid_view_insert_lines(s->grid, s->cy, ny);
615
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000616 ttyctx.num = ny;
617 tty_write(tty_cmd_insertline, &ttyctx);
Nicholas Marriott86c93c62009-07-09 07:58:14 +0000618 return;
619 }
620
621 if (ny > s->rlower + 1 - s->cy)
622 ny = s->rlower + 1 - s->cy;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000623 if (ny == 0)
624 return;
Nicholas Marriott15a64b82009-12-03 22:50:09 +0000625
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000626 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000627
628 if (s->cy < s->rupper || s->cy > s->rlower)
629 grid_view_insert_lines(s->grid, s->cy, ny);
Nicholas Marriott643c2192009-07-09 00:29:32 +0000630 else
631 grid_view_insert_lines_region(s->grid, s->rlower, s->cy, ny);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000632
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000633 ttyctx.num = ny;
634 tty_write(tty_cmd_insertline, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000635}
636
637/* Delete ny lines. */
638void
639screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny)
640{
641 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000642 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000643
644 if (ny == 0)
645 ny = 1;
646
Nicholas Marriott86c93c62009-07-09 07:58:14 +0000647 if (s->cy < s->rupper || s->cy > s->rlower) {
648 if (ny > screen_size_y(s) - s->cy)
649 ny = screen_size_y(s) - s->cy;
650 if (ny == 0)
651 return;
652
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000653 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott86c93c62009-07-09 07:58:14 +0000654
655 grid_view_delete_lines(s->grid, s->cy, ny);
656
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000657 ttyctx.num = ny;
658 tty_write(tty_cmd_deleteline, &ttyctx);
Nicholas Marriott86c93c62009-07-09 07:58:14 +0000659 return;
660 }
Nicholas Marriott15a64b82009-12-03 22:50:09 +0000661
Nicholas Marriott86c93c62009-07-09 07:58:14 +0000662 if (ny > s->rlower + 1 - s->cy)
663 ny = s->rlower + 1 - s->cy;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000664 if (ny == 0)
665 return;
666
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000667 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000668
669 if (s->cy < s->rupper || s->cy > s->rlower)
670 grid_view_delete_lines(s->grid, s->cy, ny);
Nicholas Marriott643c2192009-07-09 00:29:32 +0000671 else
672 grid_view_delete_lines_region(s->grid, s->rlower, s->cy, ny);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000673
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000674 ttyctx.num = ny;
675 tty_write(tty_cmd_deleteline, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000676}
677
678/* Clear line at cursor. */
679void
680screen_write_clearline(struct screen_write_ctx *ctx)
681{
682 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000683 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000684
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000685 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000686
687 grid_view_clear(s->grid, 0, s->cy, screen_size_x(s), 1);
688
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000689 tty_write(tty_cmd_clearline, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000690}
691
692/* Clear to end of line from cursor. */
693void
694screen_write_clearendofline(struct screen_write_ctx *ctx)
695{
696 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000697 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000698 u_int sx;
699
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000700 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000701
702 sx = screen_size_x(s);
703
704 if (s->cx <= sx - 1)
705 grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
706
Nicholas Marriott15a64b82009-12-03 22:50:09 +0000707 tty_write(tty_cmd_clearendofline, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000708}
709
710/* Clear to start of line from cursor. */
711void
712screen_write_clearstartofline(struct screen_write_ctx *ctx)
713{
714 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000715 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000716 u_int sx;
717
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000718 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000719
720 sx = screen_size_x(s);
721
722 if (s->cx > sx - 1)
723 grid_view_clear(s->grid, 0, s->cy, sx, 1);
724 else
725 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
726
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000727 tty_write(tty_cmd_clearstartofline, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000728}
729
730/* Move cursor to px,py. */
731void
732screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
733{
734 struct screen *s = ctx->s;
735
736 if (px > screen_size_x(s) - 1)
737 px = screen_size_x(s) - 1;
738 if (py > screen_size_y(s) - 1)
739 py = screen_size_y(s) - 1;
740
741 s->cx = px;
742 s->cy = py;
743}
744
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000745/* Reverse index (up with scroll). */
746void
747screen_write_reverseindex(struct screen_write_ctx *ctx)
748{
749 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000750 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000751
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000752 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000753
754 if (s->cy == s->rupper)
755 grid_view_scroll_region_down(s->grid, s->rupper, s->rlower);
756 else if (s->cy > 0)
757 s->cy--;
758
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000759 tty_write(tty_cmd_reverseindex, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000760}
761
762/* Set scroll region. */
763void
764screen_write_scrollregion(
765 struct screen_write_ctx *ctx, u_int rupper, u_int rlower)
766{
767 struct screen *s = ctx->s;
768
769 if (rupper > screen_size_y(s) - 1)
770 rupper = screen_size_y(s) - 1;
771 if (rlower > screen_size_y(s) - 1)
772 rlower = screen_size_y(s) - 1;
Nicholas Marriott975f5162009-07-14 14:47:32 +0000773 if (rupper >= rlower) /* cannot be one line */
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000774 return;
775
776 /* Cursor moves to top-left. */
777 s->cx = 0;
778 s->cy = 0;
779
780 s->rupper = rupper;
781 s->rlower = rlower;
782}
783
Nicholas Marriott387f4d42009-10-20 16:32:23 +0000784/* Line feed. */
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000785void
Nicholas Marriott387f4d42009-10-20 16:32:23 +0000786screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped)
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000787{
Nicholas Marriott06ddd3d2009-08-08 15:57:49 +0000788 struct screen *s = ctx->s;
789 struct grid_line *gl;
Nicholas Marriott387f4d42009-10-20 16:32:23 +0000790 struct tty_ctx ttyctx;
791
792 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott06ddd3d2009-08-08 15:57:49 +0000793
794 gl = &s->grid->linedata[s->grid->hsize + s->cy];
795 if (wrapped)
796 gl->flags |= GRID_LINE_WRAPPED;
nicmcc768d72015-07-13 13:28:50 +0000797 else
798 gl->flags &= ~GRID_LINE_WRAPPED;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000799
800 if (s->cy == s->rlower)
801 grid_view_scroll_region_up(s->grid, s->rupper, s->rlower);
802 else if (s->cy < screen_size_y(s) - 1)
803 s->cy++;
Nicholas Marriottd7626cd2009-10-12 17:19:47 +0000804
Nicholas Marriott387f4d42009-10-20 16:32:23 +0000805 ttyctx.num = wrapped;
Nicholas Marriott15a64b82009-12-03 22:50:09 +0000806 tty_write(tty_cmd_linefeed, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000807}
808
809/* Carriage return (cursor to start of line). */
810void
811screen_write_carriagereturn(struct screen_write_ctx *ctx)
812{
813 struct screen *s = ctx->s;
814
815 s->cx = 0;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000816}
817
818/* Clear to end of screen from cursor. */
819void
820screen_write_clearendofscreen(struct screen_write_ctx *ctx)
821{
822 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000823 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000824 u_int sx, sy;
825
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000826 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000827
828 sx = screen_size_x(s);
829 sy = screen_size_y(s);
830
Nicholas Marriottecc22c52011-01-25 23:40:26 +0000831 /* Scroll into history if it is enabled and clearing entire screen. */
832 if (s->cy == 0 && s->grid->flags & GRID_HISTORY)
833 grid_view_clear_history(s->grid);
834 else {
835 if (s->cx <= sx - 1)
836 grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
837 grid_view_clear(s->grid, 0, s->cy + 1, sx, sy - (s->cy + 1));
838 }
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000839
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000840 tty_write(tty_cmd_clearendofscreen, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000841}
842
843/* Clear to start of screen. */
844void
845screen_write_clearstartofscreen(struct screen_write_ctx *ctx)
846{
847 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000848 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000849 u_int sx;
850
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000851 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000852
853 sx = screen_size_x(s);
854
855 if (s->cy > 0)
Nicholas Marriottebe07c22009-06-03 23:26:56 +0000856 grid_view_clear(s->grid, 0, 0, sx, s->cy);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000857 if (s->cx > sx - 1)
858 grid_view_clear(s->grid, 0, s->cy, sx, 1);
859 else
Nicholas Marriottebe07c22009-06-03 23:26:56 +0000860 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000861
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000862 tty_write(tty_cmd_clearstartofscreen, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000863}
864
865/* Clear entire screen. */
866void
867screen_write_clearscreen(struct screen_write_ctx *ctx)
868{
869 struct screen *s = ctx->s;
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000870 struct tty_ctx ttyctx;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000871
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000872 screen_write_initctx(ctx, &ttyctx, 0);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000873
Nicholas Marriottecc22c52011-01-25 23:40:26 +0000874 /* Scroll into history if it is enabled. */
875 if (s->grid->flags & GRID_HISTORY)
876 grid_view_clear_history(s->grid);
877 else {
878 grid_view_clear(
879 s->grid, 0, 0, screen_size_x(s), screen_size_y(s));
880 }
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000881
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000882 tty_write(tty_cmd_clearscreen, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000883}
884
Nicholas Marriottf0aad682011-10-23 10:16:14 +0000885/* Clear entire history. */
886void
887screen_write_clearhistory(struct screen_write_ctx *ctx)
888{
889 struct screen *s = ctx->s;
890 struct grid *gd = s->grid;
891
892 grid_move_lines(gd, 0, gd->hsize, gd->sy);
893 gd->hsize = 0;
894}
895
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000896/* Write cell data. */
897void
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000898screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000899{
900 struct screen *s = ctx->s;
901 struct grid *gd = s->grid;
Nicholas Marriott6a309c52009-07-22 16:45:31 +0000902 struct tty_ctx ttyctx;
Nicholas Marriottad5df9b2013-03-22 10:36:53 +0000903 u_int width, xx, last;
Nicholas Marriottdaa26072009-10-17 08:35:38 +0000904 struct grid_cell tmp_gc, *tmp_gcp;
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000905 struct utf8_data ud;
Nicholas Marriottad5df9b2013-03-22 10:36:53 +0000906 int insert;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000907
908 /* Ignore padding. */
909 if (gc->flags & GRID_FLAG_PADDING)
910 return;
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000911 width = grid_cell_width(gc);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000912
Nicholas Marriottfe26b5d2009-10-17 08:32:18 +0000913 /*
914 * If this is a wide character and there is no room on the screen, for
915 * the entire character, don't print it.
916 */
Nicholas Marriottd74e5bf2011-03-26 19:07:33 +0000917 if (!(s->mode & MODE_WRAP)
918 && (width > 1 && (width > screen_size_x(s) ||
919 (s->cx != screen_size_x(s)
920 && s->cx > screen_size_x(s) - width))))
Nicholas Marriottfe26b5d2009-10-17 08:32:18 +0000921 return;
922
Nicholas Marriott62f234c2009-10-20 17:33:33 +0000923 /*
924 * If the width is zero, combine onto the previous character, if
Nicholas Marriott15a64b82009-12-03 22:50:09 +0000925 * there is space.
Nicholas Marriott62f234c2009-10-20 17:33:33 +0000926 */
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000927 if (width == 0) {
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000928 grid_cell_get(gc, &ud);
929 if (screen_write_combine(ctx, &ud) == 0) {
Nicholas Marriott62f234c2009-10-20 17:33:33 +0000930 screen_write_initctx(ctx, &ttyctx, 0);
931 tty_write(tty_cmd_utf8character, &ttyctx);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000932 }
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000933 return;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000934 }
Nicholas Marriott43d62c12009-10-17 08:24:46 +0000935
Nicholas Marriott04892132012-03-17 17:36:03 +0000936 /* Initialise the redraw context, saving the last cell. */
937 screen_write_initctx(ctx, &ttyctx, 1);
938
Nicholas Marriottc1a4c012009-06-03 23:37:30 +0000939 /* If in insert mode, make space for the cells. */
Nicholas Marriott04892132012-03-17 17:36:03 +0000940 if ((s->mode & MODE_INSERT) && s->cx <= screen_size_x(s) - width) {
Nicholas Marriottc1a4c012009-06-03 23:37:30 +0000941 xx = screen_size_x(s) - s->cx - width;
942 grid_move_cells(s->grid, s->cx + width, s->cx, s->cy, xx);
943 insert = 1;
Nicholas Marriottad5df9b2013-03-22 10:36:53 +0000944 } else
945 insert = 0;
Nicholas Marriottc1a4c012009-06-03 23:37:30 +0000946
Nicholas Marriott06ddd3d2009-08-08 15:57:49 +0000947 /* Check this will fit on the current line and wrap if not. */
Nicholas Marriottac9daf92010-04-06 21:35:44 +0000948 if ((s->mode & MODE_WRAP) && s->cx > screen_size_x(s) - width) {
Nicholas Marriott387f4d42009-10-20 16:32:23 +0000949 screen_write_linefeed(ctx, 1);
Nicholas Marriottd7626cd2009-10-12 17:19:47 +0000950 s->cx = 0; /* carriage return */
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000951 }
Nicholas Marriott71df37c2012-03-14 23:29:07 +0000952
Nicholas Marriottad5df9b2013-03-22 10:36:53 +0000953 /* Sanity check cursor position. */
954 if (s->cx > screen_size_x(s) - width || s->cy > screen_size_y(s) - 1)
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000955 return;
956
957 /* Handle overwriting of UTF-8 characters. */
Nicholas Marriott5afb8202010-06-21 00:11:12 +0000958 screen_write_overwrite(ctx, width);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000959
960 /*
961 * If the new character is UTF-8 wide, fill in padding cells. Have
962 * already ensured there is enough room.
963 */
964 for (xx = s->cx + 1; xx < s->cx + width; xx++) {
Nicholas Marriottfc65da12009-07-27 11:33:21 +0000965 tmp_gcp = grid_view_get_cell(gd, xx, s->cy);
966 if (tmp_gcp != NULL)
967 tmp_gcp->flags |= GRID_FLAG_PADDING;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000968 }
969
970 /* Set the cell. */
971 grid_view_set_cell(gd, s->cx, s->cy, gc);
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000972
Nicholas Marriottad5df9b2013-03-22 10:36:53 +0000973 /*
974 * Move the cursor. If not wrapping, stick at the last character and
975 * replace it.
976 */
Nicholas Marriottf1983652013-03-22 10:41:01 +0000977 last = !(s->mode & MODE_WRAP);
Nicholas Marriottad5df9b2013-03-22 10:36:53 +0000978 if (s->cx <= screen_size_x(s) - last - width)
979 s->cx += width;
980 else
981 s->cx = screen_size_x(s) - last;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000982
983 /* Draw to the screen if necessary. */
Nicholas Marriottddad0be2009-07-22 20:53:38 +0000984 if (insert) {
985 ttyctx.num = width;
986 tty_write(tty_cmd_insertcharacter, &ttyctx);
987 }
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000988 if (screen_check_selection(s, s->cx - width, s->cy)) {
Nicholas Marriottdaa26072009-10-17 08:35:38 +0000989 memcpy(&tmp_gc, &s->sel.cell, sizeof tmp_gc);
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000990 grid_cell_get(gc, &ud);
991 grid_cell_set(&tmp_gc, &ud);
nicm2f19df02014-10-02 08:36:26 +0000992 tmp_gc.attr = tmp_gc.attr & ~GRID_ATTR_CHARSET;
993 tmp_gc.attr |= gc->attr & GRID_ATTR_CHARSET;
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +0000994 tmp_gc.flags = gc->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
Nicholas Marriottdaa26072009-10-17 08:35:38 +0000995 tmp_gc.flags |= s->sel.cell.flags &
Nicholas Marriott56157442009-10-12 16:33:39 +0000996 (GRID_FLAG_FG256|GRID_FLAG_BG256);
Nicholas Marriottdaa26072009-10-17 08:35:38 +0000997 ttyctx.cell = &tmp_gc;
Nicholas Marriott2ec28372009-07-22 18:02:23 +0000998 tty_write(tty_cmd_cell, &ttyctx);
Nicholas Marriott6a309c52009-07-22 16:45:31 +0000999 } else {
1000 ttyctx.cell = gc;
Nicholas Marriott2ec28372009-07-22 18:02:23 +00001001 tty_write(tty_cmd_cell, &ttyctx);
Nicholas Marriott6a309c52009-07-22 16:45:31 +00001002 }
Nicholas Marriott35876ea2009-06-01 22:58:49 +00001003}
1004
Nicholas Marriott62f234c2009-10-20 17:33:33 +00001005/* Combine a UTF-8 zero-width character onto the previous. */
1006int
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +00001007screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud)
Nicholas Marriott62f234c2009-10-20 17:33:33 +00001008{
1009 struct screen *s = ctx->s;
1010 struct grid *gd = s->grid;
1011 struct grid_cell *gc;
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +00001012 struct utf8_data ud1;
Nicholas Marriott62f234c2009-10-20 17:33:33 +00001013
1014 /* Can't combine if at 0. */
1015 if (s->cx == 0)
1016 return (-1);
1017
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +00001018 /* Empty data is out. */
1019 if (ud->size == 0)
Nicholas Marriott9df4e752009-11-11 18:53:21 +00001020 fatalx("UTF-8 data empty");
1021
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +00001022 /* Retrieve the previous cell. */
Nicholas Marriott62f234c2009-10-20 17:33:33 +00001023 gc = grid_view_get_cell(gd, s->cx - 1, s->cy);
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +00001024 grid_cell_get(gc, &ud1);
Nicholas Marriott62f234c2009-10-20 17:33:33 +00001025
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +00001026 /* Check there is enough space. */
1027 if (ud1.size + ud->size > sizeof ud1.data)
1028 return (-1);
Nicholas Marriott62f234c2009-10-20 17:33:33 +00001029
Nicholas Marriottfdbfc7e2013-01-18 02:16:21 +00001030 /* Append the data and set the cell. */
1031 memcpy(ud1.data + ud1.size, ud->data, ud->size);
1032 ud1.size += ud->size;
1033 grid_cell_set(gc, &ud1);
Nicholas Marriott62f234c2009-10-20 17:33:33 +00001034
Nicholas Marriott62f234c2009-10-20 17:33:33 +00001035 return (0);
1036}
1037
Nicholas Marriott35876ea2009-06-01 22:58:49 +00001038/*
1039 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1040 * cell on the screen, so following cells must not be drawn by marking them as
1041 * padding.
1042 *
1043 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1044 * character, it is necessary to also overwrite any other cells which covered
1045 * by the same character.
1046 */
1047void
Nicholas Marriott5afb8202010-06-21 00:11:12 +00001048screen_write_overwrite(struct screen_write_ctx *ctx, u_int width)
Nicholas Marriott35876ea2009-06-01 22:58:49 +00001049{
1050 struct screen *s = ctx->s;
1051 struct grid *gd = s->grid;
1052 const struct grid_cell *gc;
Nicholas Marriott35876ea2009-06-01 22:58:49 +00001053 u_int xx;
1054
1055 gc = grid_view_peek_cell(gd, s->cx, s->cy);
Nicholas Marriott35876ea2009-06-01 22:58:49 +00001056 if (gc->flags & GRID_FLAG_PADDING) {
1057 /*
1058 * A padding cell, so clear any following and leading padding
1059 * cells back to the character. Don't overwrite the current
1060 * cell as that happens later anyway.
1061 */
1062 xx = s->cx + 1;
1063 while (--xx > 0) {
1064 gc = grid_view_peek_cell(gd, xx, s->cy);
1065 if (!(gc->flags & GRID_FLAG_PADDING))
1066 break;
1067 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1068 }
1069
1070 /* Overwrite the character at the start of this padding. */
1071 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
Nicholas Marriott5afb8202010-06-21 00:11:12 +00001072 }
Nicholas Marriott35876ea2009-06-01 22:58:49 +00001073
Nicholas Marriott5afb8202010-06-21 00:11:12 +00001074 /*
1075 * Overwrite any padding cells that belong to a UTF-8 character
1076 * we'll be overwriting with the current character.
1077 */
1078 xx = s->cx + width - 1;
1079 while (++xx < screen_size_x(s)) {
1080 gc = grid_view_peek_cell(gd, xx, s->cy);
1081 if (!(gc->flags & GRID_FLAG_PADDING))
1082 break;
1083 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
Nicholas Marriott35876ea2009-06-01 22:58:49 +00001084 }
1085}
Nicholas Marriott79e30da2011-03-07 23:46:27 +00001086
1087void
Nicholas Marriott96e7f332011-05-18 20:24:29 +00001088screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
1089{
1090 struct tty_ctx ttyctx;
1091
1092 screen_write_initctx(ctx, &ttyctx, 0);
1093 ttyctx.ptr = str;
1094 ttyctx.num = len;
1095
1096 tty_write(tty_cmd_setselection, &ttyctx);
1097}
1098
1099void
Nicholas Marriott79e30da2011-03-07 23:46:27 +00001100screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
1101{
1102 struct tty_ctx ttyctx;
1103
1104 screen_write_initctx(ctx, &ttyctx, 0);
1105 ttyctx.ptr = str;
1106 ttyctx.num = len;
1107
1108 tty_write(tty_cmd_rawstring, &ttyctx);
1109}