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>
25 void screen_write_initctx(struct screen_write_ctx
*, struct tty_ctx
*, int);
26 void screen_write_overwrite(struct screen_write_ctx
*, u_int
);
27 int screen_write_combine(
28 struct screen_write_ctx
*, const struct utf8_data
*);
30 /* Initialise writing with a window. */
33 struct screen_write_ctx
*ctx
, struct window_pane
*wp
, struct screen
*s
)
36 if (wp
!= NULL
&& s
== NULL
)
45 screen_write_stop(unused
struct screen_write_ctx
*ctx
)
49 /* Write character. */
52 struct screen_write_ctx
*ctx
, struct grid_cell
*gc
, u_char ch
)
55 screen_write_cell(ctx
, gc
, NULL
);
58 /* Calculate string length, with embedded formatting. */
60 screen_write_cstrlen(int utf8flag
, const char *fmt
, ...)
63 char *msg
, *msg2
, *ptr
, *ptr2
;
67 xvasprintf(&msg
, fmt
, ap
);
69 msg2
= xmalloc(strlen(msg
) + 1);
73 while (*ptr
!= '\0') {
74 if (ptr
[0] == '#' && ptr
[1] == '[') {
75 while (*ptr
!= ']' && *ptr
!= '\0')
85 size
= screen_write_strlen(utf8flag
, "%s", msg2
);
93 /* Calculate string length. */
95 screen_write_strlen(int utf8flag
, const char *fmt
, ...)
99 struct utf8_data utf8data
;
101 size_t left
, size
= 0;
104 xvasprintf(&msg
, fmt
, ap
);
108 while (*ptr
!= '\0') {
109 if (utf8flag
&& *ptr
> 0x7f && utf8_open(&utf8data
, *ptr
)) {
113 if (left
< utf8data
.size
- 1)
115 while (utf8_append(&utf8data
, *ptr
))
119 size
+= utf8data
.width
;
130 /* Write simple string (no UTF-8 or maximum length). */
133 struct screen_write_ctx
*ctx
, struct grid_cell
*gc
, const char *fmt
, ...)
138 screen_write_vnputs(ctx
, -1, gc
, 0, fmt
, ap
);
142 /* Write string with length limit (-1 for unlimited). */
144 screen_write_nputs(struct screen_write_ctx
*ctx
,
145 ssize_t maxlen
, struct grid_cell
*gc
, int utf8flag
, const char *fmt
, ...)
150 screen_write_vnputs(ctx
, maxlen
, gc
, utf8flag
, fmt
, ap
);
155 screen_write_vnputs(struct screen_write_ctx
*ctx
, ssize_t maxlen
,
156 struct grid_cell
*gc
, int utf8flag
, const char *fmt
, va_list ap
)
159 struct utf8_data utf8data
;
161 size_t left
, size
= 0;
163 xvasprintf(&msg
, fmt
, ap
);
166 while (*ptr
!= '\0') {
167 if (utf8flag
&& *ptr
> 0x7f && utf8_open(&utf8data
, *ptr
)) {
171 if (left
< utf8data
.size
- 1)
173 while (utf8_append(&utf8data
, *ptr
))
178 size
+ utf8data
.width
> (size_t) maxlen
) {
179 while (size
< (size_t) maxlen
) {
180 screen_write_putc(ctx
, gc
, ' ');
185 size
+= utf8data
.width
;
187 gc
->flags
|= GRID_FLAG_UTF8
;
188 screen_write_cell(ctx
, gc
, &utf8data
);
189 gc
->flags
&= ~GRID_FLAG_UTF8
;
191 if (maxlen
> 0 && size
+ 1 > (size_t) maxlen
)
195 screen_write_putc(ctx
, gc
, *ptr
);
203 /* Write string, similar to nputs, but with embedded formatting (#[]). */
205 screen_write_cnputs(struct screen_write_ctx
*ctx
,
206 ssize_t maxlen
, struct grid_cell
*gc
, int utf8flag
, const char *fmt
, ...)
208 struct grid_cell lgc
;
209 struct utf8_data utf8data
;
213 size_t left
, size
= 0;
216 xvasprintf(&msg
, fmt
, ap
);
219 memcpy(&lgc
, gc
, sizeof lgc
);
222 while (*ptr
!= '\0') {
223 if (ptr
[0] == '#' && ptr
[1] == '[') {
225 last
= ptr
+ strcspn(ptr
, "]");
227 /* No ]. Not much point in doing anything. */
232 screen_write_parsestyle(gc
, &lgc
, ptr
);
237 if (utf8flag
&& *ptr
> 0x7f && utf8_open(&utf8data
, *ptr
)) {
241 if (left
< utf8data
.size
- 1)
243 while (utf8_append(&utf8data
, *ptr
))
248 size
+ utf8data
.width
> (size_t) maxlen
) {
249 while (size
< (size_t) maxlen
) {
250 screen_write_putc(ctx
, gc
, ' ');
255 size
+= utf8data
.width
;
257 lgc
.flags
|= GRID_FLAG_UTF8
;
258 screen_write_cell(ctx
, &lgc
, &utf8data
);
259 lgc
.flags
&= ~GRID_FLAG_UTF8
;
261 if (maxlen
> 0 && size
+ 1 > (size_t) maxlen
)
265 screen_write_putc(ctx
, &lgc
, *ptr
);
273 /* Parse an embedded style of the form "fg=colour,bg=colour,bright,...". */
275 screen_write_parsestyle(
276 struct grid_cell
*defgc
, struct grid_cell
*gc
, const char *in
)
278 const char delimiters
[] = " ,";
282 u_char fg
, bg
, attr
, flags
;
286 if (strchr(delimiters
, in
[strlen(in
) - 1]) != NULL
)
294 end
= strcspn(in
, delimiters
);
295 if (end
> (sizeof tmp
) - 1)
297 memcpy(tmp
, in
, end
);
300 if (strcasecmp(tmp
, "default") == 0) {
304 } else if (end
> 3 && strncasecmp(tmp
+ 1, "g=", 2) == 0) {
305 if ((val
= colour_fromstring(tmp
+ 3)) == -1)
307 if (*in
== 'f' || *in
== 'F') {
310 flags
|= GRID_FLAG_FG256
;
313 flags
&= ~GRID_FLAG_FG256
;
317 } else if (*in
== 'b' || *in
== 'B') {
320 flags
|= GRID_FLAG_BG256
;
323 flags
&= ~GRID_FLAG_BG256
;
329 } else if (end
> 2 && strncasecmp(tmp
, "no", 2) == 0) {
330 if ((val
= attributes_fromstring(tmp
+ 2)) == -1)
334 if ((val
= attributes_fromstring(tmp
)) == -1)
339 in
+= end
+ strspn(in
+ end
, delimiters
);
340 } while (*in
!= '\0');
347 /* Copy from another screen. */
349 screen_write_copy(struct screen_write_ctx
*ctx
,
350 struct screen
*src
, u_int px
, u_int py
, u_int nx
, u_int ny
)
352 struct screen
*s
= ctx
->s
;
353 struct grid
*gd
= src
->grid
;
354 struct grid_line
*gl
;
355 const struct grid_cell
*gc
;
356 const struct grid_utf8
*gu
;
357 struct utf8_data utf8data
;
358 u_int xx
, yy
, cx
, cy
, ax
, bx
;
362 for (yy
= py
; yy
< py
+ ny
; yy
++) {
363 gl
= &gd
->linedata
[yy
];
364 if (yy
< gd
->hsize
+ gd
->sy
) {
366 * Find start and end position and copy between
367 * them. Limit to the real end of the line then use a
368 * clear EOL only if copying to the end, otherwise
369 * could overwrite whatever is there already.
371 if (px
> gl
->cellsize
)
375 if (px
+ nx
== gd
->sx
&& px
+ nx
> gl
->cellsize
)
380 for (xx
= ax
; xx
< bx
; xx
++) {
381 if (xx
>= gl
->cellsize
)
382 gc
= &grid_default_cell
;
384 gc
= &gl
->celldata
[xx
];
385 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
386 screen_write_cell(ctx
, gc
, NULL
);
389 /* Reinject the UTF-8 sequence. */
390 gu
= &gl
->utf8data
[xx
];
391 utf8data
.size
= grid_utf8_copy(
392 gu
, utf8data
.data
, sizeof utf8data
.data
);
393 utf8data
.width
= gu
->width
;
394 screen_write_cell(ctx
, gc
, &utf8data
);
396 if (px
+ nx
== gd
->sx
&& px
+ nx
> gl
->cellsize
)
397 screen_write_clearendofline(ctx
);
399 screen_write_clearline(ctx
);
401 screen_write_cursormove(ctx
, cx
, cy
);
405 /* Set up context for TTY command. */
407 screen_write_initctx(
408 struct screen_write_ctx
*ctx
, struct tty_ctx
*ttyctx
, int save_last
)
410 struct screen
*s
= ctx
->s
;
411 struct grid
*gd
= s
->grid
;
412 const struct grid_cell
*gc
;
413 const struct grid_utf8
*gu
;
416 ttyctx
->wp
= ctx
->wp
;
421 ttyctx
->orlower
= s
->rlower
;
422 ttyctx
->orupper
= s
->rupper
;
427 /* Save the last cell on the screen. */
428 gc
= &grid_default_cell
;
429 for (xx
= 1; xx
<= screen_size_x(s
); xx
++) {
430 gc
= grid_view_peek_cell(gd
, screen_size_x(s
) - xx
, s
->cy
);
431 if (!(gc
->flags
& GRID_FLAG_PADDING
))
434 ttyctx
->last_width
= xx
;
435 memcpy(&ttyctx
->last_cell
, gc
, sizeof ttyctx
->last_cell
);
436 if (gc
->flags
& GRID_FLAG_UTF8
) {
437 gu
= grid_view_peek_utf8(gd
, screen_size_x(s
) - xx
, s
->cy
);
438 memcpy(&ttyctx
->last_utf8
, gu
, sizeof ttyctx
->last_utf8
);
442 /* Cursor up by ny. */
444 screen_write_cursorup(struct screen_write_ctx
*ctx
, u_int ny
)
446 struct screen
*s
= ctx
->s
;
451 if (s
->cy
< s
->rupper
) {
457 if (ny
> s
->cy
- s
->rupper
)
458 ny
= s
->cy
- s
->rupper
;
466 /* Cursor down by ny. */
468 screen_write_cursordown(struct screen_write_ctx
*ctx
, u_int ny
)
470 struct screen
*s
= ctx
->s
;
475 if (s
->cy
> s
->rlower
) {
477 if (ny
> screen_size_y(s
) - 1 - s
->cy
)
478 ny
= screen_size_y(s
) - 1 - s
->cy
;
481 if (ny
> s
->rlower
- s
->cy
)
482 ny
= s
->rlower
- s
->cy
;
490 /* Cursor right by nx. */
492 screen_write_cursorright(struct screen_write_ctx
*ctx
, u_int nx
)
494 struct screen
*s
= ctx
->s
;
499 if (nx
> screen_size_x(s
) - 1 - s
->cx
)
500 nx
= screen_size_x(s
) - 1 - s
->cx
;
507 /* Cursor left by nx. */
509 screen_write_cursorleft(struct screen_write_ctx
*ctx
, u_int nx
)
511 struct screen
*s
= ctx
->s
;
524 /* Backspace; cursor left unless at start of wrapped line when can move up. */
526 screen_write_backspace(struct screen_write_ctx
*ctx
)
528 struct screen
*s
= ctx
->s
;
529 struct grid_line
*gl
;
534 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ s
->cy
- 1];
535 if (gl
->flags
& GRID_LINE_WRAPPED
) {
537 s
->cx
= screen_size_x(s
) - 1;
543 /* VT100 alignment test. */
545 screen_write_alignmenttest(struct screen_write_ctx
*ctx
)
547 struct screen
*s
= ctx
->s
;
548 struct tty_ctx ttyctx
;
552 screen_write_initctx(ctx
, &ttyctx
, 0);
554 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
557 for (yy
= 0; yy
< screen_size_y(s
); yy
++) {
558 for (xx
= 0; xx
< screen_size_x(s
); xx
++)
559 grid_view_set_cell(s
->grid
, xx
, yy
, &gc
);
567 s
->rlower
= screen_size_y(s
) - 1;
569 tty_write(tty_cmd_alignmenttest
, &ttyctx
);
572 /* Insert nx characters. */
574 screen_write_insertcharacter(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_insert_cells(s
->grid
, s
->cx
, s
->cy
, nx
);
593 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
596 /* Delete nx characters. */
598 screen_write_deletecharacter(struct screen_write_ctx
*ctx
, u_int nx
)
600 struct screen
*s
= ctx
->s
;
601 struct tty_ctx ttyctx
;
606 if (nx
> screen_size_x(s
) - s
->cx
)
607 nx
= screen_size_x(s
) - s
->cx
;
611 screen_write_initctx(ctx
, &ttyctx
, 0);
613 if (s
->cx
<= screen_size_x(s
) - 1)
614 grid_view_delete_cells(s
->grid
, s
->cx
, s
->cy
, nx
);
617 tty_write(tty_cmd_deletecharacter
, &ttyctx
);
620 /* Insert ny lines. */
622 screen_write_insertline(struct screen_write_ctx
*ctx
, u_int ny
)
624 struct screen
*s
= ctx
->s
;
625 struct tty_ctx ttyctx
;
630 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
631 if (ny
> screen_size_y(s
) - s
->cy
)
632 ny
= screen_size_y(s
) - s
->cy
;
636 screen_write_initctx(ctx
, &ttyctx
, 0);
638 grid_view_insert_lines(s
->grid
, s
->cy
, ny
);
641 tty_write(tty_cmd_insertline
, &ttyctx
);
645 if (ny
> s
->rlower
+ 1 - s
->cy
)
646 ny
= s
->rlower
+ 1 - s
->cy
;
650 screen_write_initctx(ctx
, &ttyctx
, 0);
652 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
653 grid_view_insert_lines(s
->grid
, s
->cy
, ny
);
655 grid_view_insert_lines_region(s
->grid
, s
->rlower
, s
->cy
, ny
);
658 tty_write(tty_cmd_insertline
, &ttyctx
);
661 /* Delete ny lines. */
663 screen_write_deleteline(struct screen_write_ctx
*ctx
, u_int ny
)
665 struct screen
*s
= ctx
->s
;
666 struct tty_ctx ttyctx
;
671 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
672 if (ny
> screen_size_y(s
) - s
->cy
)
673 ny
= screen_size_y(s
) - s
->cy
;
677 screen_write_initctx(ctx
, &ttyctx
, 0);
679 grid_view_delete_lines(s
->grid
, s
->cy
, ny
);
682 tty_write(tty_cmd_deleteline
, &ttyctx
);
686 if (ny
> s
->rlower
+ 1 - s
->cy
)
687 ny
= s
->rlower
+ 1 - s
->cy
;
691 screen_write_initctx(ctx
, &ttyctx
, 0);
693 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
694 grid_view_delete_lines(s
->grid
, s
->cy
, ny
);
696 grid_view_delete_lines_region(s
->grid
, s
->rlower
, s
->cy
, ny
);
699 tty_write(tty_cmd_deleteline
, &ttyctx
);
702 /* Clear line at cursor. */
704 screen_write_clearline(struct screen_write_ctx
*ctx
)
706 struct screen
*s
= ctx
->s
;
707 struct tty_ctx ttyctx
;
709 screen_write_initctx(ctx
, &ttyctx
, 0);
711 grid_view_clear(s
->grid
, 0, s
->cy
, screen_size_x(s
), 1);
713 tty_write(tty_cmd_clearline
, &ttyctx
);
716 /* Clear to end of line from cursor. */
718 screen_write_clearendofline(struct screen_write_ctx
*ctx
)
720 struct screen
*s
= ctx
->s
;
721 struct tty_ctx ttyctx
;
724 screen_write_initctx(ctx
, &ttyctx
, 0);
726 sx
= screen_size_x(s
);
729 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, sx
- s
->cx
, 1);
731 tty_write(tty_cmd_clearendofline
, &ttyctx
);
734 /* Clear to start of line from cursor. */
736 screen_write_clearstartofline(struct screen_write_ctx
*ctx
)
738 struct screen
*s
= ctx
->s
;
739 struct tty_ctx ttyctx
;
742 screen_write_initctx(ctx
, &ttyctx
, 0);
744 sx
= screen_size_x(s
);
747 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1);
749 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1);
751 tty_write(tty_cmd_clearstartofline
, &ttyctx
);
754 /* Move cursor to px,py. */
756 screen_write_cursormove(struct screen_write_ctx
*ctx
, u_int px
, u_int py
)
758 struct screen
*s
= ctx
->s
;
760 if (px
> screen_size_x(s
) - 1)
761 px
= screen_size_x(s
) - 1;
762 if (py
> screen_size_y(s
) - 1)
763 py
= screen_size_y(s
) - 1;
769 /* Set cursor mode. */
771 screen_write_cursormode(struct screen_write_ctx
*ctx
, int state
)
773 struct screen
*s
= ctx
->s
;
776 s
->mode
|= MODE_CURSOR
;
778 s
->mode
&= ~MODE_CURSOR
;
781 /* Reverse index (up with scroll). */
783 screen_write_reverseindex(struct screen_write_ctx
*ctx
)
785 struct screen
*s
= ctx
->s
;
786 struct tty_ctx ttyctx
;
788 screen_write_initctx(ctx
, &ttyctx
, 0);
790 if (s
->cy
== s
->rupper
)
791 grid_view_scroll_region_down(s
->grid
, s
->rupper
, s
->rlower
);
795 tty_write(tty_cmd_reverseindex
, &ttyctx
);
798 /* Set scroll region. */
800 screen_write_scrollregion(
801 struct screen_write_ctx
*ctx
, u_int rupper
, u_int rlower
)
803 struct screen
*s
= ctx
->s
;
805 if (rupper
> screen_size_y(s
) - 1)
806 rupper
= screen_size_y(s
) - 1;
807 if (rlower
> screen_size_y(s
) - 1)
808 rlower
= screen_size_y(s
) - 1;
809 if (rupper
>= rlower
) /* cannot be one line */
812 /* Cursor moves to top-left. */
820 /* Set insert mode. */
822 screen_write_insertmode(struct screen_write_ctx
*ctx
, int state
)
824 struct screen
*s
= ctx
->s
;
827 s
->mode
|= MODE_INSERT
;
829 s
->mode
&= ~MODE_INSERT
;
832 /* Set UTF-8 mouse mode. */
834 screen_write_utf8mousemode(struct screen_write_ctx
*ctx
, int state
)
836 struct screen
*s
= ctx
->s
;
839 s
->mode
|= MODE_MOUSE_UTF8
;
841 s
->mode
&= ~MODE_MOUSE_UTF8
;
844 /* Set mouse mode off. */
846 screen_write_mousemode_off(struct screen_write_ctx
*ctx
)
848 struct screen
*s
= ctx
->s
;
850 s
->mode
&= ~ALL_MOUSE_MODES
;
853 /* Set mouse mode on. */
855 screen_write_mousemode_on(struct screen_write_ctx
*ctx
, int mode
)
857 struct screen
*s
= ctx
->s
;
859 s
->mode
&= ~ALL_MOUSE_MODES
;
865 screen_write_linefeed(struct screen_write_ctx
*ctx
, int wrapped
)
867 struct screen
*s
= ctx
->s
;
868 struct grid_line
*gl
;
869 struct tty_ctx ttyctx
;
871 screen_write_initctx(ctx
, &ttyctx
, 0);
873 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ s
->cy
];
875 gl
->flags
|= GRID_LINE_WRAPPED
;
877 gl
->flags
&= ~GRID_LINE_WRAPPED
;
879 if (s
->cy
== s
->rlower
)
880 grid_view_scroll_region_up(s
->grid
, s
->rupper
, s
->rlower
);
881 else if (s
->cy
< screen_size_y(s
) - 1)
884 ttyctx
.num
= wrapped
;
885 tty_write(tty_cmd_linefeed
, &ttyctx
);
888 /* Carriage return (cursor to start of line). */
890 screen_write_carriagereturn(struct screen_write_ctx
*ctx
)
892 struct screen
*s
= ctx
->s
;
897 /* Set keypad cursor keys mode. */
899 screen_write_kcursormode(struct screen_write_ctx
*ctx
, int state
)
901 struct screen
*s
= ctx
->s
;
904 s
->mode
|= MODE_KCURSOR
;
906 s
->mode
&= ~MODE_KCURSOR
;
909 /* Set keypad number keys mode. */
911 screen_write_kkeypadmode(struct screen_write_ctx
*ctx
, int state
)
913 struct screen
*s
= ctx
->s
;
916 s
->mode
|= MODE_KKEYPAD
;
918 s
->mode
&= ~MODE_KKEYPAD
;
921 /* Clear to end of screen from cursor. */
923 screen_write_clearendofscreen(struct screen_write_ctx
*ctx
)
925 struct screen
*s
= ctx
->s
;
926 struct tty_ctx ttyctx
;
929 screen_write_initctx(ctx
, &ttyctx
, 0);
931 sx
= screen_size_x(s
);
932 sy
= screen_size_y(s
);
934 /* Scroll into history if it is enabled and clearing entire screen. */
935 if (s
->cy
== 0 && s
->grid
->flags
& GRID_HISTORY
)
936 grid_view_clear_history(s
->grid
);
939 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, sx
- s
->cx
, 1);
940 grid_view_clear(s
->grid
, 0, s
->cy
+ 1, sx
, sy
- (s
->cy
+ 1));
943 tty_write(tty_cmd_clearendofscreen
, &ttyctx
);
946 /* Clear to start of screen. */
948 screen_write_clearstartofscreen(struct screen_write_ctx
*ctx
)
950 struct screen
*s
= ctx
->s
;
951 struct tty_ctx ttyctx
;
954 screen_write_initctx(ctx
, &ttyctx
, 0);
956 sx
= screen_size_x(s
);
959 grid_view_clear(s
->grid
, 0, 0, sx
, s
->cy
);
961 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1);
963 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1);
965 tty_write(tty_cmd_clearstartofscreen
, &ttyctx
);
968 /* Clear entire screen. */
970 screen_write_clearscreen(struct screen_write_ctx
*ctx
)
972 struct screen
*s
= ctx
->s
;
973 struct tty_ctx ttyctx
;
975 screen_write_initctx(ctx
, &ttyctx
, 0);
977 /* Scroll into history if it is enabled. */
978 if (s
->grid
->flags
& GRID_HISTORY
)
979 grid_view_clear_history(s
->grid
);
982 s
->grid
, 0, 0, screen_size_x(s
), screen_size_y(s
));
985 tty_write(tty_cmd_clearscreen
, &ttyctx
);
988 /* Write cell data. */
990 screen_write_cell(struct screen_write_ctx
*ctx
,
991 const struct grid_cell
*gc
, const struct utf8_data
*utf8data
)
993 struct screen
*s
= ctx
->s
;
994 struct grid
*gd
= s
->grid
;
995 struct tty_ctx ttyctx
;
998 struct grid_cell tmp_gc
, *tmp_gcp
;
1001 /* Ignore padding. */
1002 if (gc
->flags
& GRID_FLAG_PADDING
)
1005 /* Find character width. */
1006 if (gc
->flags
& GRID_FLAG_UTF8
)
1007 width
= utf8data
->width
;
1012 * If this is a wide character and there is no room on the screen, for
1013 * the entire character, don't print it.
1015 if (!(s
->mode
& MODE_WRAP
)
1016 && (width
> 1 && (width
> screen_size_x(s
) ||
1017 (s
->cx
!= screen_size_x(s
)
1018 && s
->cx
> screen_size_x(s
) - width
))))
1022 * If the width is zero, combine onto the previous character, if
1026 if (screen_write_combine(ctx
, utf8data
) == 0) {
1027 screen_write_initctx(ctx
, &ttyctx
, 0);
1028 tty_write(tty_cmd_utf8character
, &ttyctx
);
1033 /* Initialise the redraw context, saving the last cell. */
1034 screen_write_initctx(ctx
, &ttyctx
, 1);
1036 /* If in insert mode, make space for the cells. */
1037 if (s
->mode
& MODE_INSERT
&& s
->cx
<= screen_size_x(s
) - width
) {
1038 xx
= screen_size_x(s
) - s
->cx
- width
;
1039 grid_move_cells(s
->grid
, s
->cx
+ width
, s
->cx
, s
->cy
, xx
);
1043 /* Check this will fit on the current line and wrap if not. */
1044 if ((s
->mode
& MODE_WRAP
) && s
->cx
> screen_size_x(s
) - width
) {
1045 screen_write_linefeed(ctx
, 1);
1046 s
->cx
= 0; /* carriage return */
1049 /* Sanity checks. */
1050 if (((s
->mode
& MODE_WRAP
) && s
->cx
> screen_size_x(s
) - width
)
1051 || s
->cy
> screen_size_y(s
) - 1)
1054 /* Handle overwriting of UTF-8 characters. */
1055 screen_write_overwrite(ctx
, width
);
1058 * If the new character is UTF-8 wide, fill in padding cells. Have
1059 * already ensured there is enough room.
1061 for (xx
= s
->cx
+ 1; xx
< s
->cx
+ width
; xx
++) {
1062 tmp_gcp
= grid_view_get_cell(gd
, xx
, s
->cy
);
1063 if (tmp_gcp
!= NULL
)
1064 tmp_gcp
->flags
|= GRID_FLAG_PADDING
;
1068 grid_view_set_cell(gd
, s
->cx
, s
->cy
, gc
);
1069 if (gc
->flags
& GRID_FLAG_UTF8
) {
1070 /* Construct UTF-8 and write it. */
1071 grid_utf8_set(&gu
, utf8data
);
1072 grid_view_set_utf8(gd
, s
->cx
, s
->cy
, &gu
);
1075 /* Move the cursor. */
1078 /* Draw to the screen if necessary. */
1081 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
1084 if (screen_check_selection(s
, s
->cx
- width
, s
->cy
)) {
1085 memcpy(&tmp_gc
, &s
->sel
.cell
, sizeof tmp_gc
);
1086 tmp_gc
.data
= gc
->data
;
1087 tmp_gc
.flags
= gc
->flags
&
1088 ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
1089 tmp_gc
.flags
|= s
->sel
.cell
.flags
&
1090 (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
1091 ttyctx
.cell
= &tmp_gc
;
1092 tty_write(tty_cmd_cell
, &ttyctx
);
1095 tty_write(tty_cmd_cell
, &ttyctx
);
1099 /* Combine a UTF-8 zero-width character onto the previous. */
1101 screen_write_combine(
1102 struct screen_write_ctx
*ctx
, const struct utf8_data
*utf8data
)
1104 struct screen
*s
= ctx
->s
;
1105 struct grid
*gd
= s
->grid
;
1106 struct grid_cell
*gc
;
1107 struct grid_utf8
*gu
, tmp_gu
;
1110 /* Can't combine if at 0. */
1114 /* Empty utf8data is out. */
1115 if (utf8data
->size
== 0)
1116 fatalx("UTF-8 data empty");
1118 /* Retrieve the previous cell and convert to UTF-8 if not already. */
1119 gc
= grid_view_get_cell(gd
, s
->cx
- 1, s
->cy
);
1120 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
1121 tmp_gu
.data
[0] = gc
->data
;
1122 tmp_gu
.data
[1] = 0xff;
1125 grid_view_set_utf8(gd
, s
->cx
- 1, s
->cy
, &tmp_gu
);
1126 gc
->flags
|= GRID_FLAG_UTF8
;
1129 /* Append the current cell. */
1130 gu
= grid_view_get_utf8(gd
, s
->cx
- 1, s
->cy
);
1131 if (grid_utf8_append(gu
, utf8data
) != 0) {
1132 /* Failed: scrap this character and replace with underscores. */
1133 if (gu
->width
== 1) {
1135 gc
->flags
&= ~GRID_FLAG_UTF8
;
1137 for (i
= 0; i
< gu
->width
&& i
!= sizeof gu
->data
; i
++)
1139 if (i
!= sizeof gu
->data
)
1149 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1150 * cell on the screen, so following cells must not be drawn by marking them as
1153 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1154 * character, it is necessary to also overwrite any other cells which covered
1155 * by the same character.
1158 screen_write_overwrite(struct screen_write_ctx
*ctx
, u_int width
)
1160 struct screen
*s
= ctx
->s
;
1161 struct grid
*gd
= s
->grid
;
1162 const struct grid_cell
*gc
;
1165 gc
= grid_view_peek_cell(gd
, s
->cx
, s
->cy
);
1166 if (gc
->flags
& GRID_FLAG_PADDING
) {
1168 * A padding cell, so clear any following and leading padding
1169 * cells back to the character. Don't overwrite the current
1170 * cell as that happens later anyway.
1174 gc
= grid_view_peek_cell(gd
, xx
, s
->cy
);
1175 if (!(gc
->flags
& GRID_FLAG_PADDING
))
1177 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1180 /* Overwrite the character at the start of this padding. */
1181 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1185 * Overwrite any padding cells that belong to a UTF-8 character
1186 * we'll be overwriting with the current character.
1188 xx
= s
->cx
+ width
- 1;
1189 while (++xx
< screen_size_x(s
)) {
1190 gc
= grid_view_peek_cell(gd
, xx
, s
->cy
);
1191 if (!(gc
->flags
& GRID_FLAG_PADDING
))
1193 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1198 screen_write_setselection(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
)
1200 struct tty_ctx ttyctx
;
1202 screen_write_initctx(ctx
, &ttyctx
, 0);
1206 tty_write(tty_cmd_setselection
, &ttyctx
);
1210 screen_write_rawstring(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
)
1212 struct tty_ctx ttyctx
;
1214 screen_write_initctx(ctx
, &ttyctx
, 0);
1218 tty_write(tty_cmd_rawstring
, &ttyctx
);