4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
26 void screen_write_initctx(struct screen_write_ctx
*, struct tty_ctx
*, int);
27 void screen_write_overwrite(struct screen_write_ctx
*, u_int
);
28 int screen_write_combine(
29 struct screen_write_ctx
*, const struct utf8_data
*);
31 /* Initialise writing with a window. */
34 struct screen_write_ctx
*ctx
, struct window_pane
*wp
, struct screen
*s
)
37 if (wp
!= NULL
&& s
== NULL
)
45 screen_write_stop(unused
struct screen_write_ctx
*ctx
)
50 /* Reset screen state. */
52 screen_write_reset(struct screen_write_ctx
*ctx
)
54 struct screen
*s
= ctx
->s
;
57 screen_write_scrollregion(ctx
, 0, screen_size_y(s
) - 1);
59 s
->mode
&= ~(MODE_INSERT
|MODE_KCURSOR
|MODE_KKEYPAD
|MODE_FOCUSON
);
60 s
->mode
&= ~(ALL_MOUSE_MODES
|MODE_MOUSE_UTF8
|MODE_MOUSE_SGR
);
62 screen_write_clearscreen(ctx
);
63 screen_write_cursormove(ctx
, 0, 0);
66 /* Write character. */
68 screen_write_putc(struct screen_write_ctx
*ctx
, struct grid_cell
*gc
, u_char ch
)
70 grid_cell_one(gc
, ch
);
71 screen_write_cell(ctx
, gc
);
74 /* Calculate string length, with embedded formatting. */
76 screen_write_cstrlen(int utf8flag
, const char *fmt
, ...)
79 char *msg
, *msg2
, *ptr
, *ptr2
;
83 xvasprintf(&msg
, fmt
, ap
);
85 msg2
= xmalloc(strlen(msg
) + 1);
89 while (*ptr
!= '\0') {
90 if (ptr
[0] == '#' && ptr
[1] == '[') {
91 while (*ptr
!= ']' && *ptr
!= '\0')
101 size
= screen_write_strlen(utf8flag
, "%s", msg2
);
109 /* Calculate string length. */
111 screen_write_strlen(int utf8flag
, const char *fmt
, ...)
115 struct utf8_data utf8data
;
117 size_t left
, size
= 0;
120 xvasprintf(&msg
, fmt
, ap
);
124 while (*ptr
!= '\0') {
125 if (utf8flag
&& *ptr
& 0x80 && utf8_open(&utf8data
, (u_char
)*ptr
)) {
129 if (left
< utf8data
.size
- 1)
131 while (utf8_append(&utf8data
, (u_char
)*ptr
))
135 size
+= utf8data
.width
;
146 /* Write simple string (no UTF-8 or maximum length). */
149 struct screen_write_ctx
*ctx
, struct grid_cell
*gc
, const char *fmt
, ...)
154 screen_write_vnputs(ctx
, -1, gc
, 0, fmt
, ap
);
158 /* Write string with length limit (-1 for unlimited). */
160 screen_write_nputs(struct screen_write_ctx
*ctx
,
161 ssize_t maxlen
, struct grid_cell
*gc
, int utf8flag
, const char *fmt
, ...)
166 screen_write_vnputs(ctx
, maxlen
, gc
, utf8flag
, fmt
, ap
);
171 screen_write_vnputs(struct screen_write_ctx
*ctx
, ssize_t maxlen
,
172 struct grid_cell
*gc
, int utf8flag
, const char *fmt
, va_list ap
)
175 struct utf8_data utf8data
;
177 size_t left
, size
= 0;
179 xvasprintf(&msg
, fmt
, ap
);
182 while (*ptr
!= '\0') {
183 if (utf8flag
&& *ptr
& 0x80 && utf8_open(&utf8data
, (u_char
)*ptr
)) {
187 if (left
< utf8data
.size
- 1)
189 while (utf8_append(&utf8data
, (u_char
)*ptr
))
194 size
+ utf8data
.width
> (size_t) maxlen
) {
195 while (size
< (size_t) maxlen
) {
196 screen_write_putc(ctx
, gc
, ' ');
201 size
+= utf8data
.width
;
203 grid_cell_set(gc
, &utf8data
);
204 screen_write_cell(ctx
, gc
);
206 if (maxlen
> 0 && size
+ 1 > (size_t) maxlen
)
210 gc
->attr
^= GRID_ATTR_CHARSET
;
213 screen_write_putc(ctx
, gc
, *ptr
);
222 /* Write string, similar to nputs, but with embedded formatting (#[]). */
224 screen_write_cnputs(struct screen_write_ctx
*ctx
,
225 ssize_t maxlen
, struct grid_cell
*gc
, int utf8flag
, const char *fmt
, ...)
227 struct grid_cell lgc
;
228 struct utf8_data utf8data
;
232 size_t left
, size
= 0;
235 xvasprintf(&msg
, fmt
, ap
);
238 memcpy(&lgc
, gc
, sizeof lgc
);
241 while (*ptr
!= '\0') {
242 if (ptr
[0] == '#' && ptr
[1] == '[') {
244 last
= ptr
+ strcspn(ptr
, "]");
246 /* No ]. Not much point in doing anything. */
251 style_parse(gc
, &lgc
, ptr
);
256 if (utf8flag
&& *ptr
& 0x80 && utf8_open(&utf8data
, (u_char
)*ptr
)) {
260 if (left
< utf8data
.size
- 1)
262 while (utf8_append(&utf8data
, (u_char
)*ptr
))
267 size
+ utf8data
.width
> (size_t) maxlen
) {
268 while (size
< (size_t) maxlen
) {
269 screen_write_putc(ctx
, gc
, ' ');
274 size
+= utf8data
.width
;
276 grid_cell_set(&lgc
, &utf8data
);
277 screen_write_cell(ctx
, &lgc
);
279 if (maxlen
> 0 && size
+ 1 > (size_t) maxlen
)
283 screen_write_putc(ctx
, &lgc
, (u_char
)*ptr
);
291 /* Copy from another screen. */
293 screen_write_copy(struct screen_write_ctx
*ctx
,
294 struct screen
*src
, u_int px
, u_int py
, u_int nx
, u_int ny
)
296 struct screen
*s
= ctx
->s
;
297 struct grid
*gd
= src
->grid
;
298 struct grid_line
*gl
;
299 const struct grid_cell
*gc
;
301 u_int xx
, yy
, cx
, cy
, ax
, bx
;
305 for (yy
= py
; yy
< py
+ ny
; yy
++) {
306 gl
= &gd
->linedata
[yy
];
307 if (yy
< gd
->hsize
+ gd
->sy
) {
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.
314 if (px
> gl
->cellsize
)
318 if (px
+ nx
== gd
->sx
&& px
+ nx
> gl
->cellsize
)
323 for (xx
= ax
; xx
< bx
; xx
++) {
324 if (xx
>= gl
->cellsize
)
325 gc
= &grid_default_cell
;
327 gc
= &gl
->celldata
[xx
];
328 grid_cell_get(gc
, &ud
);
329 screen_write_cell(ctx
, gc
);
331 if (px
+ nx
== gd
->sx
&& px
+ nx
> gl
->cellsize
)
332 screen_write_clearendofline(ctx
);
334 screen_write_clearline(ctx
);
336 screen_write_cursormove(ctx
, cx
, cy
);
340 /* Set up context for TTY command. */
342 screen_write_initctx(
343 struct screen_write_ctx
*ctx
, struct tty_ctx
*ttyctx
, int save_last
)
345 struct screen
*s
= ctx
->s
;
346 struct grid
*gd
= s
->grid
;
347 const struct grid_cell
*gc
;
350 ttyctx
->wp
= ctx
->wp
;
355 ttyctx
->orlower
= s
->rlower
;
356 ttyctx
->orupper
= s
->rupper
;
361 /* Save the last cell on the screen. */
362 gc
= &grid_default_cell
;
363 for (xx
= 1; xx
<= screen_size_x(s
); xx
++) {
364 gc
= grid_view_peek_cell(gd
, screen_size_x(s
) - xx
, s
->cy
);
365 if (!(gc
->flags
& GRID_FLAG_PADDING
))
368 ttyctx
->last_width
= xx
;
369 memcpy(&ttyctx
->last_cell
, gc
, sizeof ttyctx
->last_cell
);
374 screen_write_mode_set(struct screen_write_ctx
*ctx
, int mode
)
376 struct screen
*s
= ctx
->s
;
383 screen_write_mode_clear(struct screen_write_ctx
*ctx
, int mode
)
385 struct screen
*s
= ctx
->s
;
390 /* Cursor up by ny. */
392 screen_write_cursorup(struct screen_write_ctx
*ctx
, u_int ny
)
394 struct screen
*s
= ctx
->s
;
399 if (s
->cy
< s
->rupper
) {
405 if (ny
> s
->cy
- s
->rupper
)
406 ny
= s
->cy
- s
->rupper
;
408 if (s
->cx
== screen_size_x(s
))
416 /* Cursor down by ny. */
418 screen_write_cursordown(struct screen_write_ctx
*ctx
, u_int ny
)
420 struct screen
*s
= ctx
->s
;
425 if (s
->cy
> s
->rlower
) {
427 if (ny
> screen_size_y(s
) - 1 - s
->cy
)
428 ny
= screen_size_y(s
) - 1 - s
->cy
;
431 if (ny
> s
->rlower
- s
->cy
)
432 ny
= s
->rlower
- s
->cy
;
434 if (s
->cx
== screen_size_x(s
))
442 /* Cursor right by nx. */
444 screen_write_cursorright(struct screen_write_ctx
*ctx
, u_int nx
)
446 struct screen
*s
= ctx
->s
;
451 if (nx
> screen_size_x(s
) - 1 - s
->cx
)
452 nx
= screen_size_x(s
) - 1 - s
->cx
;
459 /* Cursor left by nx. */
461 screen_write_cursorleft(struct screen_write_ctx
*ctx
, u_int nx
)
463 struct screen
*s
= ctx
->s
;
476 /* Backspace; cursor left unless at start of wrapped line when can move up. */
478 screen_write_backspace(struct screen_write_ctx
*ctx
)
480 struct screen
*s
= ctx
->s
;
481 struct grid_line
*gl
;
486 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ s
->cy
- 1];
487 if (gl
->flags
& GRID_LINE_WRAPPED
) {
489 s
->cx
= screen_size_x(s
) - 1;
495 /* VT100 alignment test. */
497 screen_write_alignmenttest(struct screen_write_ctx
*ctx
)
499 struct screen
*s
= ctx
->s
;
500 struct tty_ctx ttyctx
;
504 screen_write_initctx(ctx
, &ttyctx
, 0);
506 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
507 grid_cell_one(&gc
, 'E');
509 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
);
519 s
->rlower
= screen_size_y(s
) - 1;
521 tty_write(tty_cmd_alignmenttest
, &ttyctx
);
524 /* Insert nx characters. */
526 screen_write_insertcharacter(struct screen_write_ctx
*ctx
, u_int nx
)
528 struct screen
*s
= ctx
->s
;
529 struct tty_ctx ttyctx
;
534 if (nx
> screen_size_x(s
) - s
->cx
)
535 nx
= screen_size_x(s
) - s
->cx
;
539 screen_write_initctx(ctx
, &ttyctx
, 0);
541 if (s
->cx
<= screen_size_x(s
) - 1)
542 grid_view_insert_cells(s
->grid
, s
->cx
, s
->cy
, nx
);
545 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
548 /* Delete nx characters. */
550 screen_write_deletecharacter(struct screen_write_ctx
*ctx
, u_int nx
)
552 struct screen
*s
= ctx
->s
;
553 struct tty_ctx ttyctx
;
558 if (nx
> screen_size_x(s
) - s
->cx
)
559 nx
= screen_size_x(s
) - s
->cx
;
563 screen_write_initctx(ctx
, &ttyctx
, 0);
565 if (s
->cx
<= screen_size_x(s
) - 1)
566 grid_view_delete_cells(s
->grid
, s
->cx
, s
->cy
, nx
);
569 tty_write(tty_cmd_deletecharacter
, &ttyctx
);
572 /* Clear nx characters. */
574 screen_write_clearcharacter(struct screen_write_ctx
*ctx
, u_int nx
)
576 struct screen
*s
= ctx
->s
;
577 struct tty_ctx ttyctx
;
582 if (nx
> screen_size_x(s
) - s
->cx
)
583 nx
= screen_size_x(s
) - s
->cx
;
587 screen_write_initctx(ctx
, &ttyctx
, 0);
589 if (s
->cx
<= screen_size_x(s
) - 1)
590 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, nx
, 1);
593 tty_write(tty_cmd_clearcharacter
, &ttyctx
);
596 /* Insert ny lines. */
598 screen_write_insertline(struct screen_write_ctx
*ctx
, u_int ny
)
600 struct screen
*s
= ctx
->s
;
601 struct tty_ctx ttyctx
;
606 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
;
612 screen_write_initctx(ctx
, &ttyctx
, 0);
614 grid_view_insert_lines(s
->grid
, s
->cy
, ny
);
617 tty_write(tty_cmd_insertline
, &ttyctx
);
621 if (ny
> s
->rlower
+ 1 - s
->cy
)
622 ny
= s
->rlower
+ 1 - s
->cy
;
626 screen_write_initctx(ctx
, &ttyctx
, 0);
628 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
629 grid_view_insert_lines(s
->grid
, s
->cy
, ny
);
631 grid_view_insert_lines_region(s
->grid
, s
->rlower
, s
->cy
, ny
);
634 tty_write(tty_cmd_insertline
, &ttyctx
);
637 /* Delete ny lines. */
639 screen_write_deleteline(struct screen_write_ctx
*ctx
, u_int ny
)
641 struct screen
*s
= ctx
->s
;
642 struct tty_ctx ttyctx
;
647 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
;
653 screen_write_initctx(ctx
, &ttyctx
, 0);
655 grid_view_delete_lines(s
->grid
, s
->cy
, ny
);
658 tty_write(tty_cmd_deleteline
, &ttyctx
);
662 if (ny
> s
->rlower
+ 1 - s
->cy
)
663 ny
= s
->rlower
+ 1 - s
->cy
;
667 screen_write_initctx(ctx
, &ttyctx
, 0);
669 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
670 grid_view_delete_lines(s
->grid
, s
->cy
, ny
);
672 grid_view_delete_lines_region(s
->grid
, s
->rlower
, s
->cy
, ny
);
675 tty_write(tty_cmd_deleteline
, &ttyctx
);
678 /* Clear line at cursor. */
680 screen_write_clearline(struct screen_write_ctx
*ctx
)
682 struct screen
*s
= ctx
->s
;
683 struct tty_ctx ttyctx
;
685 screen_write_initctx(ctx
, &ttyctx
, 0);
687 grid_view_clear(s
->grid
, 0, s
->cy
, screen_size_x(s
), 1);
689 tty_write(tty_cmd_clearline
, &ttyctx
);
692 /* Clear to end of line from cursor. */
694 screen_write_clearendofline(struct screen_write_ctx
*ctx
)
696 struct screen
*s
= ctx
->s
;
697 struct tty_ctx ttyctx
;
700 screen_write_initctx(ctx
, &ttyctx
, 0);
702 sx
= screen_size_x(s
);
705 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, sx
- s
->cx
, 1);
707 tty_write(tty_cmd_clearendofline
, &ttyctx
);
710 /* Clear to start of line from cursor. */
712 screen_write_clearstartofline(struct screen_write_ctx
*ctx
)
714 struct screen
*s
= ctx
->s
;
715 struct tty_ctx ttyctx
;
718 screen_write_initctx(ctx
, &ttyctx
, 0);
720 sx
= screen_size_x(s
);
723 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1);
725 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1);
727 tty_write(tty_cmd_clearstartofline
, &ttyctx
);
730 /* Move cursor to px,py. */
732 screen_write_cursormove(struct screen_write_ctx
*ctx
, u_int px
, u_int py
)
734 struct screen
*s
= ctx
->s
;
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;
745 /* Reverse index (up with scroll). */
747 screen_write_reverseindex(struct screen_write_ctx
*ctx
)
749 struct screen
*s
= ctx
->s
;
750 struct tty_ctx ttyctx
;
752 screen_write_initctx(ctx
, &ttyctx
, 0);
754 if (s
->cy
== s
->rupper
)
755 grid_view_scroll_region_down(s
->grid
, s
->rupper
, s
->rlower
);
759 tty_write(tty_cmd_reverseindex
, &ttyctx
);
762 /* Set scroll region. */
764 screen_write_scrollregion(
765 struct screen_write_ctx
*ctx
, u_int rupper
, u_int rlower
)
767 struct screen
*s
= ctx
->s
;
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;
773 if (rupper
>= rlower
) /* cannot be one line */
776 /* Cursor moves to top-left. */
786 screen_write_linefeed(struct screen_write_ctx
*ctx
, int wrapped
)
788 struct screen
*s
= ctx
->s
;
789 struct grid_line
*gl
;
790 struct tty_ctx ttyctx
;
792 screen_write_initctx(ctx
, &ttyctx
, 0);
794 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ s
->cy
];
796 gl
->flags
|= GRID_LINE_WRAPPED
;
798 gl
->flags
&= ~GRID_LINE_WRAPPED
;
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)
805 ttyctx
.num
= wrapped
;
806 tty_write(tty_cmd_linefeed
, &ttyctx
);
809 /* Carriage return (cursor to start of line). */
811 screen_write_carriagereturn(struct screen_write_ctx
*ctx
)
813 struct screen
*s
= ctx
->s
;
818 /* Clear to end of screen from cursor. */
820 screen_write_clearendofscreen(struct screen_write_ctx
*ctx
)
822 struct screen
*s
= ctx
->s
;
823 struct tty_ctx ttyctx
;
826 screen_write_initctx(ctx
, &ttyctx
, 0);
828 sx
= screen_size_x(s
);
829 sy
= screen_size_y(s
);
831 /* 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
);
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));
840 tty_write(tty_cmd_clearendofscreen
, &ttyctx
);
843 /* Clear to start of screen. */
845 screen_write_clearstartofscreen(struct screen_write_ctx
*ctx
)
847 struct screen
*s
= ctx
->s
;
848 struct tty_ctx ttyctx
;
851 screen_write_initctx(ctx
, &ttyctx
, 0);
853 sx
= screen_size_x(s
);
856 grid_view_clear(s
->grid
, 0, 0, sx
, s
->cy
);
858 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1);
860 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1);
862 tty_write(tty_cmd_clearstartofscreen
, &ttyctx
);
865 /* Clear entire screen. */
867 screen_write_clearscreen(struct screen_write_ctx
*ctx
)
869 struct screen
*s
= ctx
->s
;
870 struct tty_ctx ttyctx
;
872 screen_write_initctx(ctx
, &ttyctx
, 0);
874 /* Scroll into history if it is enabled. */
875 if (s
->grid
->flags
& GRID_HISTORY
)
876 grid_view_clear_history(s
->grid
);
879 s
->grid
, 0, 0, screen_size_x(s
), screen_size_y(s
));
882 tty_write(tty_cmd_clearscreen
, &ttyctx
);
885 /* Clear entire history. */
887 screen_write_clearhistory(struct screen_write_ctx
*ctx
)
889 struct screen
*s
= ctx
->s
;
890 struct grid
*gd
= s
->grid
;
892 grid_move_lines(gd
, 0, gd
->hsize
, gd
->sy
);
896 /* Write cell data. */
898 screen_write_cell(struct screen_write_ctx
*ctx
, const struct grid_cell
*gc
)
900 struct screen
*s
= ctx
->s
;
901 struct grid
*gd
= s
->grid
;
902 struct tty_ctx ttyctx
;
903 u_int width
, xx
, last
;
904 struct grid_cell tmp_gc
, *tmp_gcp
;
908 /* Ignore padding. */
909 if (gc
->flags
& GRID_FLAG_PADDING
)
911 width
= grid_cell_width(gc
);
914 * If this is a wide character and there is no room on the screen, for
915 * the entire character, don't print it.
917 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
))))
924 * If the width is zero, combine onto the previous character, if
928 grid_cell_get(gc
, &ud
);
929 if (screen_write_combine(ctx
, &ud
) == 0) {
930 screen_write_initctx(ctx
, &ttyctx
, 0);
931 tty_write(tty_cmd_utf8character
, &ttyctx
);
936 /* Initialise the redraw context, saving the last cell. */
937 screen_write_initctx(ctx
, &ttyctx
, 1);
939 /* If in insert mode, make space for the cells. */
940 if ((s
->mode
& MODE_INSERT
) && s
->cx
<= screen_size_x(s
) - width
) {
941 xx
= screen_size_x(s
) - s
->cx
- width
;
942 grid_move_cells(s
->grid
, s
->cx
+ width
, s
->cx
, s
->cy
, xx
);
947 /* Check this will fit on the current line and wrap if not. */
948 if ((s
->mode
& MODE_WRAP
) && s
->cx
> screen_size_x(s
) - width
) {
949 screen_write_linefeed(ctx
, 1);
950 s
->cx
= 0; /* carriage return */
953 /* Sanity check cursor position. */
954 if (s
->cx
> screen_size_x(s
) - width
|| s
->cy
> screen_size_y(s
) - 1)
957 /* Handle overwriting of UTF-8 characters. */
958 screen_write_overwrite(ctx
, width
);
961 * If the new character is UTF-8 wide, fill in padding cells. Have
962 * already ensured there is enough room.
964 for (xx
= s
->cx
+ 1; xx
< s
->cx
+ width
; xx
++) {
965 tmp_gcp
= grid_view_get_cell(gd
, xx
, s
->cy
);
967 tmp_gcp
->flags
|= GRID_FLAG_PADDING
;
971 grid_view_set_cell(gd
, s
->cx
, s
->cy
, gc
);
974 * Move the cursor. If not wrapping, stick at the last character and
977 last
= !(s
->mode
& MODE_WRAP
);
978 if (s
->cx
<= screen_size_x(s
) - last
- width
)
981 s
->cx
= screen_size_x(s
) - last
;
983 /* Draw to the screen if necessary. */
986 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
988 if (screen_check_selection(s
, s
->cx
- width
, s
->cy
)) {
989 memcpy(&tmp_gc
, &s
->sel
.cell
, sizeof tmp_gc
);
990 grid_cell_get(gc
, &ud
);
991 grid_cell_set(&tmp_gc
, &ud
);
992 tmp_gc
.flags
= gc
->flags
& ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
993 tmp_gc
.flags
|= s
->sel
.cell
.flags
&
994 (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
995 ttyctx
.cell
= &tmp_gc
;
996 tty_write(tty_cmd_cell
, &ttyctx
);
999 tty_write(tty_cmd_cell
, &ttyctx
);
1003 /* Combine a UTF-8 zero-width character onto the previous. */
1005 screen_write_combine(struct screen_write_ctx
*ctx
, const struct utf8_data
*ud
)
1007 struct screen
*s
= ctx
->s
;
1008 struct grid
*gd
= s
->grid
;
1009 struct grid_cell
*gc
;
1010 struct utf8_data ud1
;
1012 /* Can't combine if at 0. */
1016 /* Empty data is out. */
1018 fatalx("UTF-8 data empty");
1020 /* Retrieve the previous cell. */
1021 gc
= grid_view_get_cell(gd
, s
->cx
- 1, s
->cy
);
1022 grid_cell_get(gc
, &ud1
);
1024 /* Check there is enough space. */
1025 if (ud1
.size
+ ud
->size
> sizeof ud1
.data
)
1028 /* Append the data and set the cell. */
1029 memcpy(ud1
.data
+ ud1
.size
, ud
->data
, ud
->size
);
1030 ud1
.size
+= ud
->size
;
1031 grid_cell_set(gc
, &ud1
);
1037 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1038 * cell on the screen, so following cells must not be drawn by marking them as
1041 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1042 * character, it is necessary to also overwrite any other cells which covered
1043 * by the same character.
1046 screen_write_overwrite(struct screen_write_ctx
*ctx
, u_int width
)
1048 struct screen
*s
= ctx
->s
;
1049 struct grid
*gd
= s
->grid
;
1050 const struct grid_cell
*gc
;
1053 gc
= grid_view_peek_cell(gd
, s
->cx
, s
->cy
);
1054 if (gc
->flags
& GRID_FLAG_PADDING
) {
1056 * A padding cell, so clear any following and leading padding
1057 * cells back to the character. Don't overwrite the current
1058 * cell as that happens later anyway.
1062 gc
= grid_view_peek_cell(gd
, xx
, s
->cy
);
1063 if (!(gc
->flags
& GRID_FLAG_PADDING
))
1065 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1068 /* Overwrite the character at the start of this padding. */
1069 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1073 * Overwrite any padding cells that belong to a UTF-8 character
1074 * we'll be overwriting with the current character.
1076 xx
= s
->cx
+ width
- 1;
1077 while (++xx
< screen_size_x(s
)) {
1078 gc
= grid_view_peek_cell(gd
, xx
, s
->cy
);
1079 if (!(gc
->flags
& GRID_FLAG_PADDING
))
1081 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1086 screen_write_setselection(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
)
1088 struct tty_ctx ttyctx
;
1090 screen_write_initctx(ctx
, &ttyctx
, 0);
1094 tty_write(tty_cmd_setselection
, &ttyctx
);
1098 screen_write_rawstring(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
)
1100 struct tty_ctx ttyctx
;
1102 screen_write_initctx(ctx
, &ttyctx
, 0);
1106 tty_write(tty_cmd_rawstring
, &ttyctx
);