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
)
50 /* Reset screen state. */
52 screen_write_reset(struct screen_write_ctx
*ctx
)
54 screen_reset_tabs(ctx
->s
);
56 screen_write_scrollregion(ctx
, 0, screen_size_y(ctx
->s
) - 1);
58 screen_write_insertmode(ctx
, 0);
59 screen_write_kcursormode(ctx
, 0);
60 screen_write_kkeypadmode(ctx
, 0);
61 screen_write_mousemode_off(ctx
);
63 screen_write_clearscreen(ctx
);
64 screen_write_cursormove(ctx
, 0, 0);
67 /* Write character. */
70 struct screen_write_ctx
*ctx
, struct grid_cell
*gc
, u_char ch
)
73 screen_write_cell(ctx
, gc
, NULL
);
76 /* Calculate string length, with embedded formatting. */
78 screen_write_cstrlen(int utf8flag
, const char *fmt
, ...)
81 char *msg
, *msg2
, *ptr
, *ptr2
;
85 xvasprintf(&msg
, fmt
, ap
);
87 msg2
= xmalloc(strlen(msg
) + 1);
91 while (*ptr
!= '\0') {
92 if (ptr
[0] == '#' && ptr
[1] == '[') {
93 while (*ptr
!= ']' && *ptr
!= '\0')
103 size
= screen_write_strlen(utf8flag
, "%s", msg2
);
111 /* Calculate string length. */
113 screen_write_strlen(int utf8flag
, const char *fmt
, ...)
117 struct utf8_data utf8data
;
119 size_t left
, size
= 0;
122 xvasprintf(&msg
, fmt
, ap
);
126 while (*ptr
!= '\0') {
127 if (utf8flag
&& *ptr
> 0x7f && utf8_open(&utf8data
, *ptr
)) {
131 if (left
< utf8data
.size
- 1)
133 while (utf8_append(&utf8data
, *ptr
))
137 size
+= utf8data
.width
;
148 /* Write simple string (no UTF-8 or maximum length). */
151 struct screen_write_ctx
*ctx
, struct grid_cell
*gc
, const char *fmt
, ...)
156 screen_write_vnputs(ctx
, -1, gc
, 0, fmt
, ap
);
160 /* Write string with length limit (-1 for unlimited). */
162 screen_write_nputs(struct screen_write_ctx
*ctx
,
163 ssize_t maxlen
, struct grid_cell
*gc
, int utf8flag
, const char *fmt
, ...)
168 screen_write_vnputs(ctx
, maxlen
, gc
, utf8flag
, fmt
, ap
);
173 screen_write_vnputs(struct screen_write_ctx
*ctx
, ssize_t maxlen
,
174 struct grid_cell
*gc
, int utf8flag
, const char *fmt
, va_list ap
)
177 struct utf8_data utf8data
;
179 size_t left
, size
= 0;
181 xvasprintf(&msg
, fmt
, ap
);
184 while (*ptr
!= '\0') {
185 if (utf8flag
&& *ptr
> 0x7f && utf8_open(&utf8data
, *ptr
)) {
189 if (left
< utf8data
.size
- 1)
191 while (utf8_append(&utf8data
, *ptr
))
196 size
+ utf8data
.width
> (size_t) maxlen
) {
197 while (size
< (size_t) maxlen
) {
198 screen_write_putc(ctx
, gc
, ' ');
203 size
+= utf8data
.width
;
205 gc
->flags
|= GRID_FLAG_UTF8
;
206 screen_write_cell(ctx
, gc
, &utf8data
);
207 gc
->flags
&= ~GRID_FLAG_UTF8
;
209 if (maxlen
> 0 && size
+ 1 > (size_t) maxlen
)
213 screen_write_putc(ctx
, gc
, *ptr
);
221 /* Write string, similar to nputs, but with embedded formatting (#[]). */
223 screen_write_cnputs(struct screen_write_ctx
*ctx
,
224 ssize_t maxlen
, struct grid_cell
*gc
, int utf8flag
, const char *fmt
, ...)
226 struct grid_cell lgc
;
227 struct utf8_data utf8data
;
231 size_t left
, size
= 0;
234 xvasprintf(&msg
, fmt
, ap
);
237 memcpy(&lgc
, gc
, sizeof lgc
);
240 while (*ptr
!= '\0') {
241 if (ptr
[0] == '#' && ptr
[1] == '[') {
243 last
= ptr
+ strcspn(ptr
, "]");
245 /* No ]. Not much point in doing anything. */
250 screen_write_parsestyle(gc
, &lgc
, ptr
);
255 if (utf8flag
&& *ptr
> 0x7f && utf8_open(&utf8data
, *ptr
)) {
259 if (left
< utf8data
.size
- 1)
261 while (utf8_append(&utf8data
, *ptr
))
266 size
+ utf8data
.width
> (size_t) maxlen
) {
267 while (size
< (size_t) maxlen
) {
268 screen_write_putc(ctx
, gc
, ' ');
273 size
+= utf8data
.width
;
275 lgc
.flags
|= GRID_FLAG_UTF8
;
276 screen_write_cell(ctx
, &lgc
, &utf8data
);
277 lgc
.flags
&= ~GRID_FLAG_UTF8
;
279 if (maxlen
> 0 && size
+ 1 > (size_t) maxlen
)
283 screen_write_putc(ctx
, &lgc
, *ptr
);
291 /* Parse an embedded style of the form "fg=colour,bg=colour,bright,...". */
293 screen_write_parsestyle(
294 struct grid_cell
*defgc
, struct grid_cell
*gc
, const char *in
)
296 const char delimiters
[] = " ,";
300 u_char fg
, bg
, attr
, flags
;
304 if (strchr(delimiters
, in
[strlen(in
) - 1]) != NULL
)
312 end
= strcspn(in
, delimiters
);
313 if (end
> (sizeof tmp
) - 1)
315 memcpy(tmp
, in
, end
);
318 if (strcasecmp(tmp
, "default") == 0) {
322 } else if (end
> 3 && strncasecmp(tmp
+ 1, "g=", 2) == 0) {
323 if ((val
= colour_fromstring(tmp
+ 3)) == -1)
325 if (*in
== 'f' || *in
== 'F') {
328 flags
|= GRID_FLAG_FG256
;
331 flags
&= ~GRID_FLAG_FG256
;
335 } else if (*in
== 'b' || *in
== 'B') {
338 flags
|= GRID_FLAG_BG256
;
341 flags
&= ~GRID_FLAG_BG256
;
347 } else if (end
> 2 && strncasecmp(tmp
, "no", 2) == 0) {
348 if ((val
= attributes_fromstring(tmp
+ 2)) == -1)
352 if ((val
= attributes_fromstring(tmp
)) == -1)
357 in
+= end
+ strspn(in
+ end
, delimiters
);
358 } while (*in
!= '\0');
365 /* Copy from another screen. */
367 screen_write_copy(struct screen_write_ctx
*ctx
,
368 struct screen
*src
, u_int px
, u_int py
, u_int nx
, u_int ny
)
370 struct screen
*s
= ctx
->s
;
371 struct grid
*gd
= src
->grid
;
372 struct grid_line
*gl
;
373 const struct grid_cell
*gc
;
374 const struct grid_utf8
*gu
;
375 struct utf8_data utf8data
;
376 u_int xx
, yy
, cx
, cy
, ax
, bx
;
380 for (yy
= py
; yy
< py
+ ny
; yy
++) {
381 gl
= &gd
->linedata
[yy
];
382 if (yy
< gd
->hsize
+ gd
->sy
) {
384 * Find start and end position and copy between
385 * them. Limit to the real end of the line then use a
386 * clear EOL only if copying to the end, otherwise
387 * could overwrite whatever is there already.
389 if (px
> gl
->cellsize
)
393 if (px
+ nx
== gd
->sx
&& px
+ nx
> gl
->cellsize
)
398 for (xx
= ax
; xx
< bx
; xx
++) {
399 if (xx
>= gl
->cellsize
)
400 gc
= &grid_default_cell
;
402 gc
= &gl
->celldata
[xx
];
403 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
404 screen_write_cell(ctx
, gc
, NULL
);
407 /* Reinject the UTF-8 sequence. */
408 gu
= &gl
->utf8data
[xx
];
409 utf8data
.size
= grid_utf8_copy(
410 gu
, utf8data
.data
, sizeof utf8data
.data
);
411 utf8data
.width
= gu
->width
;
412 screen_write_cell(ctx
, gc
, &utf8data
);
414 if (px
+ nx
== gd
->sx
&& px
+ nx
> gl
->cellsize
)
415 screen_write_clearendofline(ctx
);
417 screen_write_clearline(ctx
);
419 screen_write_cursormove(ctx
, cx
, cy
);
423 /* Set up context for TTY command. */
425 screen_write_initctx(
426 struct screen_write_ctx
*ctx
, struct tty_ctx
*ttyctx
, int save_last
)
428 struct screen
*s
= ctx
->s
;
429 struct grid
*gd
= s
->grid
;
430 const struct grid_cell
*gc
;
431 const struct grid_utf8
*gu
;
434 ttyctx
->wp
= ctx
->wp
;
439 ttyctx
->orlower
= s
->rlower
;
440 ttyctx
->orupper
= s
->rupper
;
445 /* Save the last cell on the screen. */
446 gc
= &grid_default_cell
;
447 for (xx
= 1; xx
<= screen_size_x(s
); xx
++) {
448 gc
= grid_view_peek_cell(gd
, screen_size_x(s
) - xx
, s
->cy
);
449 if (!(gc
->flags
& GRID_FLAG_PADDING
))
452 ttyctx
->last_width
= xx
;
453 memcpy(&ttyctx
->last_cell
, gc
, sizeof ttyctx
->last_cell
);
454 if (gc
->flags
& GRID_FLAG_UTF8
) {
455 gu
= grid_view_peek_utf8(gd
, screen_size_x(s
) - xx
, s
->cy
);
456 memcpy(&ttyctx
->last_utf8
, gu
, sizeof ttyctx
->last_utf8
);
460 /* Cursor up by ny. */
462 screen_write_cursorup(struct screen_write_ctx
*ctx
, u_int ny
)
464 struct screen
*s
= ctx
->s
;
469 if (s
->cy
< s
->rupper
) {
475 if (ny
> s
->cy
- s
->rupper
)
476 ny
= s
->cy
- s
->rupper
;
484 /* Cursor down by ny. */
486 screen_write_cursordown(struct screen_write_ctx
*ctx
, u_int ny
)
488 struct screen
*s
= ctx
->s
;
493 if (s
->cy
> s
->rlower
) {
495 if (ny
> screen_size_y(s
) - 1 - s
->cy
)
496 ny
= screen_size_y(s
) - 1 - s
->cy
;
499 if (ny
> s
->rlower
- s
->cy
)
500 ny
= s
->rlower
- s
->cy
;
508 /* Cursor right by nx. */
510 screen_write_cursorright(struct screen_write_ctx
*ctx
, u_int nx
)
512 struct screen
*s
= ctx
->s
;
517 if (nx
> screen_size_x(s
) - 1 - s
->cx
)
518 nx
= screen_size_x(s
) - 1 - s
->cx
;
525 /* Cursor left by nx. */
527 screen_write_cursorleft(struct screen_write_ctx
*ctx
, u_int nx
)
529 struct screen
*s
= ctx
->s
;
542 /* Backspace; cursor left unless at start of wrapped line when can move up. */
544 screen_write_backspace(struct screen_write_ctx
*ctx
)
546 struct screen
*s
= ctx
->s
;
547 struct grid_line
*gl
;
552 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ s
->cy
- 1];
553 if (gl
->flags
& GRID_LINE_WRAPPED
) {
555 s
->cx
= screen_size_x(s
) - 1;
561 /* VT100 alignment test. */
563 screen_write_alignmenttest(struct screen_write_ctx
*ctx
)
565 struct screen
*s
= ctx
->s
;
566 struct tty_ctx ttyctx
;
570 screen_write_initctx(ctx
, &ttyctx
, 0);
572 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
575 for (yy
= 0; yy
< screen_size_y(s
); yy
++) {
576 for (xx
= 0; xx
< screen_size_x(s
); xx
++)
577 grid_view_set_cell(s
->grid
, xx
, yy
, &gc
);
585 s
->rlower
= screen_size_y(s
) - 1;
587 tty_write(tty_cmd_alignmenttest
, &ttyctx
);
590 /* Insert nx characters. */
592 screen_write_insertcharacter(struct screen_write_ctx
*ctx
, u_int nx
)
594 struct screen
*s
= ctx
->s
;
595 struct tty_ctx ttyctx
;
600 if (nx
> screen_size_x(s
) - s
->cx
)
601 nx
= screen_size_x(s
) - s
->cx
;
605 screen_write_initctx(ctx
, &ttyctx
, 0);
607 if (s
->cx
<= screen_size_x(s
) - 1)
608 grid_view_insert_cells(s
->grid
, s
->cx
, s
->cy
, nx
);
611 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
614 /* Delete nx characters. */
616 screen_write_deletecharacter(struct screen_write_ctx
*ctx
, u_int nx
)
618 struct screen
*s
= ctx
->s
;
619 struct tty_ctx ttyctx
;
624 if (nx
> screen_size_x(s
) - s
->cx
)
625 nx
= screen_size_x(s
) - s
->cx
;
629 screen_write_initctx(ctx
, &ttyctx
, 0);
631 if (s
->cx
<= screen_size_x(s
) - 1)
632 grid_view_delete_cells(s
->grid
, s
->cx
, s
->cy
, nx
);
635 tty_write(tty_cmd_deletecharacter
, &ttyctx
);
638 /* Insert ny lines. */
640 screen_write_insertline(struct screen_write_ctx
*ctx
, u_int ny
)
642 struct screen
*s
= ctx
->s
;
643 struct tty_ctx ttyctx
;
648 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
649 if (ny
> screen_size_y(s
) - s
->cy
)
650 ny
= screen_size_y(s
) - s
->cy
;
654 screen_write_initctx(ctx
, &ttyctx
, 0);
656 grid_view_insert_lines(s
->grid
, s
->cy
, ny
);
659 tty_write(tty_cmd_insertline
, &ttyctx
);
663 if (ny
> s
->rlower
+ 1 - s
->cy
)
664 ny
= s
->rlower
+ 1 - s
->cy
;
668 screen_write_initctx(ctx
, &ttyctx
, 0);
670 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
671 grid_view_insert_lines(s
->grid
, s
->cy
, ny
);
673 grid_view_insert_lines_region(s
->grid
, s
->rlower
, s
->cy
, ny
);
676 tty_write(tty_cmd_insertline
, &ttyctx
);
679 /* Delete ny lines. */
681 screen_write_deleteline(struct screen_write_ctx
*ctx
, u_int ny
)
683 struct screen
*s
= ctx
->s
;
684 struct tty_ctx ttyctx
;
689 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
690 if (ny
> screen_size_y(s
) - s
->cy
)
691 ny
= screen_size_y(s
) - s
->cy
;
695 screen_write_initctx(ctx
, &ttyctx
, 0);
697 grid_view_delete_lines(s
->grid
, s
->cy
, ny
);
700 tty_write(tty_cmd_deleteline
, &ttyctx
);
704 if (ny
> s
->rlower
+ 1 - s
->cy
)
705 ny
= s
->rlower
+ 1 - s
->cy
;
709 screen_write_initctx(ctx
, &ttyctx
, 0);
711 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
712 grid_view_delete_lines(s
->grid
, s
->cy
, ny
);
714 grid_view_delete_lines_region(s
->grid
, s
->rlower
, s
->cy
, ny
);
717 tty_write(tty_cmd_deleteline
, &ttyctx
);
720 /* Clear line at cursor. */
722 screen_write_clearline(struct screen_write_ctx
*ctx
)
724 struct screen
*s
= ctx
->s
;
725 struct tty_ctx ttyctx
;
727 screen_write_initctx(ctx
, &ttyctx
, 0);
729 grid_view_clear(s
->grid
, 0, s
->cy
, screen_size_x(s
), 1);
731 tty_write(tty_cmd_clearline
, &ttyctx
);
734 /* Clear to end of line from cursor. */
736 screen_write_clearendofline(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
, s
->cx
, s
->cy
, sx
- s
->cx
, 1);
749 tty_write(tty_cmd_clearendofline
, &ttyctx
);
752 /* Clear to start of line from cursor. */
754 screen_write_clearstartofline(struct screen_write_ctx
*ctx
)
756 struct screen
*s
= ctx
->s
;
757 struct tty_ctx ttyctx
;
760 screen_write_initctx(ctx
, &ttyctx
, 0);
762 sx
= screen_size_x(s
);
765 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1);
767 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1);
769 tty_write(tty_cmd_clearstartofline
, &ttyctx
);
772 /* Move cursor to px,py. */
774 screen_write_cursormove(struct screen_write_ctx
*ctx
, u_int px
, u_int py
)
776 struct screen
*s
= ctx
->s
;
778 if (px
> screen_size_x(s
) - 1)
779 px
= screen_size_x(s
) - 1;
780 if (py
> screen_size_y(s
) - 1)
781 py
= screen_size_y(s
) - 1;
787 /* Set cursor mode. */
789 screen_write_cursormode(struct screen_write_ctx
*ctx
, int state
)
791 struct screen
*s
= ctx
->s
;
794 s
->mode
|= MODE_CURSOR
;
796 s
->mode
&= ~MODE_CURSOR
;
799 /* Reverse index (up with scroll). */
801 screen_write_reverseindex(struct screen_write_ctx
*ctx
)
803 struct screen
*s
= ctx
->s
;
804 struct tty_ctx ttyctx
;
806 screen_write_initctx(ctx
, &ttyctx
, 0);
808 if (s
->cy
== s
->rupper
)
809 grid_view_scroll_region_down(s
->grid
, s
->rupper
, s
->rlower
);
813 tty_write(tty_cmd_reverseindex
, &ttyctx
);
816 /* Set scroll region. */
818 screen_write_scrollregion(
819 struct screen_write_ctx
*ctx
, u_int rupper
, u_int rlower
)
821 struct screen
*s
= ctx
->s
;
823 if (rupper
> screen_size_y(s
) - 1)
824 rupper
= screen_size_y(s
) - 1;
825 if (rlower
> screen_size_y(s
) - 1)
826 rlower
= screen_size_y(s
) - 1;
827 if (rupper
>= rlower
) /* cannot be one line */
830 /* Cursor moves to top-left. */
838 /* Set insert mode. */
840 screen_write_insertmode(struct screen_write_ctx
*ctx
, int state
)
842 struct screen
*s
= ctx
->s
;
845 s
->mode
|= MODE_INSERT
;
847 s
->mode
&= ~MODE_INSERT
;
850 /* Set UTF-8 mouse mode. */
852 screen_write_utf8mousemode(struct screen_write_ctx
*ctx
, int state
)
854 struct screen
*s
= ctx
->s
;
857 s
->mode
|= MODE_MOUSE_UTF8
;
859 s
->mode
&= ~MODE_MOUSE_UTF8
;
862 /* Set mouse mode off. */
864 screen_write_mousemode_off(struct screen_write_ctx
*ctx
)
866 struct screen
*s
= ctx
->s
;
868 s
->mode
&= ~ALL_MOUSE_MODES
;
871 /* Set mouse mode on. */
873 screen_write_mousemode_on(struct screen_write_ctx
*ctx
, int mode
)
875 struct screen
*s
= ctx
->s
;
877 s
->mode
&= ~ALL_MOUSE_MODES
;
883 screen_write_linefeed(struct screen_write_ctx
*ctx
, int wrapped
)
885 struct screen
*s
= ctx
->s
;
886 struct grid_line
*gl
;
887 struct tty_ctx ttyctx
;
889 screen_write_initctx(ctx
, &ttyctx
, 0);
891 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ s
->cy
];
893 gl
->flags
|= GRID_LINE_WRAPPED
;
895 gl
->flags
&= ~GRID_LINE_WRAPPED
;
897 if (s
->cy
== s
->rlower
)
898 grid_view_scroll_region_up(s
->grid
, s
->rupper
, s
->rlower
);
899 else if (s
->cy
< screen_size_y(s
) - 1)
902 ttyctx
.num
= wrapped
;
903 tty_write(tty_cmd_linefeed
, &ttyctx
);
906 /* Carriage return (cursor to start of line). */
908 screen_write_carriagereturn(struct screen_write_ctx
*ctx
)
910 struct screen
*s
= ctx
->s
;
915 /* Set keypad cursor keys mode. */
917 screen_write_kcursormode(struct screen_write_ctx
*ctx
, int state
)
919 struct screen
*s
= ctx
->s
;
922 s
->mode
|= MODE_KCURSOR
;
924 s
->mode
&= ~MODE_KCURSOR
;
927 /* Set keypad number keys mode. */
929 screen_write_kkeypadmode(struct screen_write_ctx
*ctx
, int state
)
931 struct screen
*s
= ctx
->s
;
934 s
->mode
|= MODE_KKEYPAD
;
936 s
->mode
&= ~MODE_KKEYPAD
;
939 /* Clear to end of screen from cursor. */
941 screen_write_clearendofscreen(struct screen_write_ctx
*ctx
)
943 struct screen
*s
= ctx
->s
;
944 struct tty_ctx ttyctx
;
947 screen_write_initctx(ctx
, &ttyctx
, 0);
949 sx
= screen_size_x(s
);
950 sy
= screen_size_y(s
);
952 /* Scroll into history if it is enabled and clearing entire screen. */
953 if (s
->cy
== 0 && s
->grid
->flags
& GRID_HISTORY
)
954 grid_view_clear_history(s
->grid
);
957 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, sx
- s
->cx
, 1);
958 grid_view_clear(s
->grid
, 0, s
->cy
+ 1, sx
, sy
- (s
->cy
+ 1));
961 tty_write(tty_cmd_clearendofscreen
, &ttyctx
);
964 /* Clear to start of screen. */
966 screen_write_clearstartofscreen(struct screen_write_ctx
*ctx
)
968 struct screen
*s
= ctx
->s
;
969 struct tty_ctx ttyctx
;
972 screen_write_initctx(ctx
, &ttyctx
, 0);
974 sx
= screen_size_x(s
);
977 grid_view_clear(s
->grid
, 0, 0, sx
, s
->cy
);
979 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1);
981 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1);
983 tty_write(tty_cmd_clearstartofscreen
, &ttyctx
);
986 /* Clear entire screen. */
988 screen_write_clearscreen(struct screen_write_ctx
*ctx
)
990 struct screen
*s
= ctx
->s
;
991 struct tty_ctx ttyctx
;
993 screen_write_initctx(ctx
, &ttyctx
, 0);
995 /* Scroll into history if it is enabled. */
996 if (s
->grid
->flags
& GRID_HISTORY
)
997 grid_view_clear_history(s
->grid
);
1000 s
->grid
, 0, 0, screen_size_x(s
), screen_size_y(s
));
1003 tty_write(tty_cmd_clearscreen
, &ttyctx
);
1006 /* Clear entire history. */
1008 screen_write_clearhistory(struct screen_write_ctx
*ctx
)
1010 struct screen
*s
= ctx
->s
;
1011 struct grid
*gd
= s
->grid
;
1013 grid_move_lines(gd
, 0, gd
->hsize
, gd
->sy
);
1017 /* Write cell data. */
1019 screen_write_cell(struct screen_write_ctx
*ctx
,
1020 const struct grid_cell
*gc
, const struct utf8_data
*utf8data
)
1022 struct screen
*s
= ctx
->s
;
1023 struct grid
*gd
= s
->grid
;
1024 struct tty_ctx ttyctx
;
1025 struct grid_utf8 gu
;
1027 struct grid_cell tmp_gc
, *tmp_gcp
;
1030 /* Ignore padding. */
1031 if (gc
->flags
& GRID_FLAG_PADDING
)
1034 /* Find character width. */
1035 if (gc
->flags
& GRID_FLAG_UTF8
)
1036 width
= utf8data
->width
;
1041 * If this is a wide character and there is no room on the screen, for
1042 * the entire character, don't print it.
1044 if (!(s
->mode
& MODE_WRAP
)
1045 && (width
> 1 && (width
> screen_size_x(s
) ||
1046 (s
->cx
!= screen_size_x(s
)
1047 && s
->cx
> screen_size_x(s
) - width
))))
1051 * If the width is zero, combine onto the previous character, if
1055 if (screen_write_combine(ctx
, utf8data
) == 0) {
1056 screen_write_initctx(ctx
, &ttyctx
, 0);
1057 tty_write(tty_cmd_utf8character
, &ttyctx
);
1062 /* Initialise the redraw context, saving the last cell. */
1063 screen_write_initctx(ctx
, &ttyctx
, 1);
1065 /* If in insert mode, make space for the cells. */
1066 if (s
->mode
& MODE_INSERT
&& s
->cx
<= screen_size_x(s
) - width
) {
1067 xx
= screen_size_x(s
) - s
->cx
- width
;
1068 grid_move_cells(s
->grid
, s
->cx
+ width
, s
->cx
, s
->cy
, xx
);
1072 /* Check this will fit on the current line and wrap if not. */
1073 if ((s
->mode
& MODE_WRAP
) && s
->cx
> screen_size_x(s
) - width
) {
1074 screen_write_linefeed(ctx
, 1);
1075 s
->cx
= 0; /* carriage return */
1078 /* Sanity checks. */
1079 if (((s
->mode
& MODE_WRAP
) && s
->cx
> screen_size_x(s
) - width
)
1080 || s
->cy
> screen_size_y(s
) - 1)
1083 /* Handle overwriting of UTF-8 characters. */
1084 screen_write_overwrite(ctx
, width
);
1087 * If the new character is UTF-8 wide, fill in padding cells. Have
1088 * already ensured there is enough room.
1090 for (xx
= s
->cx
+ 1; xx
< s
->cx
+ width
; xx
++) {
1091 tmp_gcp
= grid_view_get_cell(gd
, xx
, s
->cy
);
1092 if (tmp_gcp
!= NULL
)
1093 tmp_gcp
->flags
|= GRID_FLAG_PADDING
;
1097 grid_view_set_cell(gd
, s
->cx
, s
->cy
, gc
);
1098 if (gc
->flags
& GRID_FLAG_UTF8
) {
1099 /* Construct UTF-8 and write it. */
1100 grid_utf8_set(&gu
, utf8data
);
1101 grid_view_set_utf8(gd
, s
->cx
, s
->cy
, &gu
);
1104 /* Move the cursor. */
1107 /* Draw to the screen if necessary. */
1110 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
1113 if (screen_check_selection(s
, s
->cx
- width
, s
->cy
)) {
1114 memcpy(&tmp_gc
, &s
->sel
.cell
, sizeof tmp_gc
);
1115 tmp_gc
.data
= gc
->data
;
1116 tmp_gc
.flags
= gc
->flags
&
1117 ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
1118 tmp_gc
.flags
|= s
->sel
.cell
.flags
&
1119 (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
1120 ttyctx
.cell
= &tmp_gc
;
1121 tty_write(tty_cmd_cell
, &ttyctx
);
1124 tty_write(tty_cmd_cell
, &ttyctx
);
1128 /* Combine a UTF-8 zero-width character onto the previous. */
1130 screen_write_combine(
1131 struct screen_write_ctx
*ctx
, const struct utf8_data
*utf8data
)
1133 struct screen
*s
= ctx
->s
;
1134 struct grid
*gd
= s
->grid
;
1135 struct grid_cell
*gc
;
1136 struct grid_utf8
*gu
, tmp_gu
;
1139 /* Can't combine if at 0. */
1143 /* Empty utf8data is out. */
1144 if (utf8data
->size
== 0)
1145 fatalx("UTF-8 data empty");
1147 /* Retrieve the previous cell and convert to UTF-8 if not already. */
1148 gc
= grid_view_get_cell(gd
, s
->cx
- 1, s
->cy
);
1149 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
1150 tmp_gu
.data
[0] = gc
->data
;
1151 tmp_gu
.data
[1] = 0xff;
1154 grid_view_set_utf8(gd
, s
->cx
- 1, s
->cy
, &tmp_gu
);
1155 gc
->flags
|= GRID_FLAG_UTF8
;
1158 /* Append the current cell. */
1159 gu
= grid_view_get_utf8(gd
, s
->cx
- 1, s
->cy
);
1160 if (grid_utf8_append(gu
, utf8data
) != 0) {
1161 /* Failed: scrap this character and replace with underscores. */
1162 if (gu
->width
== 1) {
1164 gc
->flags
&= ~GRID_FLAG_UTF8
;
1166 for (i
= 0; i
< gu
->width
&& i
!= sizeof gu
->data
; i
++)
1168 if (i
!= sizeof gu
->data
)
1178 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1179 * cell on the screen, so following cells must not be drawn by marking them as
1182 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1183 * character, it is necessary to also overwrite any other cells which covered
1184 * by the same character.
1187 screen_write_overwrite(struct screen_write_ctx
*ctx
, u_int width
)
1189 struct screen
*s
= ctx
->s
;
1190 struct grid
*gd
= s
->grid
;
1191 const struct grid_cell
*gc
;
1194 gc
= grid_view_peek_cell(gd
, s
->cx
, s
->cy
);
1195 if (gc
->flags
& GRID_FLAG_PADDING
) {
1197 * A padding cell, so clear any following and leading padding
1198 * cells back to the character. Don't overwrite the current
1199 * cell as that happens later anyway.
1203 gc
= grid_view_peek_cell(gd
, xx
, s
->cy
);
1204 if (!(gc
->flags
& GRID_FLAG_PADDING
))
1206 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1209 /* Overwrite the character at the start of this padding. */
1210 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1214 * Overwrite any padding cells that belong to a UTF-8 character
1215 * we'll be overwriting with the current character.
1217 xx
= s
->cx
+ width
- 1;
1218 while (++xx
< screen_size_x(s
)) {
1219 gc
= grid_view_peek_cell(gd
, xx
, s
->cy
);
1220 if (!(gc
->flags
& GRID_FLAG_PADDING
))
1222 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1227 screen_write_setselection(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
)
1229 struct tty_ctx ttyctx
;
1231 screen_write_initctx(ctx
, &ttyctx
, 0);
1235 tty_write(tty_cmd_setselection
, &ttyctx
);
1239 screen_write_rawstring(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
)
1241 struct tty_ctx ttyctx
;
1243 screen_write_initctx(ctx
, &ttyctx
, 0);
1247 tty_write(tty_cmd_rawstring
, &ttyctx
);