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
);
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
> 0x7f && utf8_open(&utf8data
, *ptr
)) {
129 if (left
< utf8data
.size
- 1)
131 while (utf8_append(&utf8data
, *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
> 0x7f && utf8_open(&utf8data
, *ptr
)) {
187 if (left
< utf8data
.size
- 1)
189 while (utf8_append(&utf8data
, *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 screen_write_parsestyle(gc
, &lgc
, ptr
);
256 if (utf8flag
&& *ptr
> 0x7f && utf8_open(&utf8data
, *ptr
)) {
260 if (left
< utf8data
.size
- 1)
262 while (utf8_append(&utf8data
, *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
, *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 flags
&= ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
324 defgc
->flags
& (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
325 } else if (end
> 3 && strncasecmp(tmp
+ 1, "g=", 2) == 0) {
326 if ((val
= colour_fromstring(tmp
+ 3)) == -1)
328 if (*in
== 'f' || *in
== 'F') {
331 flags
|= GRID_FLAG_FG256
;
334 flags
&= ~GRID_FLAG_FG256
;
338 flags
&= ~GRID_FLAG_FG256
;
339 flags
|= defgc
->flags
& GRID_FLAG_FG256
;
341 } else if (*in
== 'b' || *in
== 'B') {
344 flags
|= GRID_FLAG_BG256
;
347 flags
&= ~GRID_FLAG_BG256
;
351 flags
&= ~GRID_FLAG_BG256
;
352 flags
|= defgc
->flags
& GRID_FLAG_BG256
;
356 } else if (end
> 2 && strncasecmp(tmp
, "no", 2) == 0) {
357 if ((val
= attributes_fromstring(tmp
+ 2)) == -1)
361 if ((val
= attributes_fromstring(tmp
)) == -1)
366 in
+= end
+ strspn(in
+ end
, delimiters
);
367 } while (*in
!= '\0');
374 /* Copy from another screen. */
376 screen_write_copy(struct screen_write_ctx
*ctx
,
377 struct screen
*src
, u_int px
, u_int py
, u_int nx
, u_int ny
)
379 struct screen
*s
= ctx
->s
;
380 struct grid
*gd
= src
->grid
;
381 struct grid_line
*gl
;
382 const struct grid_cell
*gc
;
384 u_int xx
, yy
, cx
, cy
, ax
, bx
;
388 for (yy
= py
; yy
< py
+ ny
; yy
++) {
389 gl
= &gd
->linedata
[yy
];
390 if (yy
< gd
->hsize
+ gd
->sy
) {
392 * Find start and end position and copy between
393 * them. Limit to the real end of the line then use a
394 * clear EOL only if copying to the end, otherwise
395 * could overwrite whatever is there already.
397 if (px
> gl
->cellsize
)
401 if (px
+ nx
== gd
->sx
&& px
+ nx
> gl
->cellsize
)
406 for (xx
= ax
; xx
< bx
; xx
++) {
407 if (xx
>= gl
->cellsize
)
408 gc
= &grid_default_cell
;
410 gc
= &gl
->celldata
[xx
];
411 grid_cell_get(gc
, &ud
);
412 screen_write_cell(ctx
, gc
);
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
;
433 ttyctx
->wp
= ctx
->wp
;
438 ttyctx
->orlower
= s
->rlower
;
439 ttyctx
->orupper
= s
->rupper
;
444 /* Save the last cell on the screen. */
445 gc
= &grid_default_cell
;
446 for (xx
= 1; xx
<= screen_size_x(s
); xx
++) {
447 gc
= grid_view_peek_cell(gd
, screen_size_x(s
) - xx
, s
->cy
);
448 if (!(gc
->flags
& GRID_FLAG_PADDING
))
451 ttyctx
->last_width
= xx
;
452 memcpy(&ttyctx
->last_cell
, gc
, sizeof ttyctx
->last_cell
);
457 screen_write_mode_set(struct screen_write_ctx
*ctx
, int mode
)
459 struct screen
*s
= ctx
->s
;
466 screen_write_mode_clear(struct screen_write_ctx
*ctx
, int mode
)
468 struct screen
*s
= ctx
->s
;
473 /* Cursor up by ny. */
475 screen_write_cursorup(struct screen_write_ctx
*ctx
, u_int ny
)
477 struct screen
*s
= ctx
->s
;
482 if (s
->cy
< s
->rupper
) {
488 if (ny
> s
->cy
- s
->rupper
)
489 ny
= s
->cy
- s
->rupper
;
497 /* Cursor down by ny. */
499 screen_write_cursordown(struct screen_write_ctx
*ctx
, u_int ny
)
501 struct screen
*s
= ctx
->s
;
506 if (s
->cy
> s
->rlower
) {
508 if (ny
> screen_size_y(s
) - 1 - s
->cy
)
509 ny
= screen_size_y(s
) - 1 - s
->cy
;
512 if (ny
> s
->rlower
- s
->cy
)
513 ny
= s
->rlower
- s
->cy
;
521 /* Cursor right by nx. */
523 screen_write_cursorright(struct screen_write_ctx
*ctx
, u_int nx
)
525 struct screen
*s
= ctx
->s
;
530 if (nx
> screen_size_x(s
) - 1 - s
->cx
)
531 nx
= screen_size_x(s
) - 1 - s
->cx
;
538 /* Cursor left by nx. */
540 screen_write_cursorleft(struct screen_write_ctx
*ctx
, u_int nx
)
542 struct screen
*s
= ctx
->s
;
555 /* Backspace; cursor left unless at start of wrapped line when can move up. */
557 screen_write_backspace(struct screen_write_ctx
*ctx
)
559 struct screen
*s
= ctx
->s
;
560 struct grid_line
*gl
;
565 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ s
->cy
- 1];
566 if (gl
->flags
& GRID_LINE_WRAPPED
) {
568 s
->cx
= screen_size_x(s
) - 1;
574 /* VT100 alignment test. */
576 screen_write_alignmenttest(struct screen_write_ctx
*ctx
)
578 struct screen
*s
= ctx
->s
;
579 struct tty_ctx ttyctx
;
583 screen_write_initctx(ctx
, &ttyctx
, 0);
585 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
586 grid_cell_one(&gc
, 'E');
588 for (yy
= 0; yy
< screen_size_y(s
); yy
++) {
589 for (xx
= 0; xx
< screen_size_x(s
); xx
++)
590 grid_view_set_cell(s
->grid
, xx
, yy
, &gc
);
598 s
->rlower
= screen_size_y(s
) - 1;
600 tty_write(tty_cmd_alignmenttest
, &ttyctx
);
603 /* Insert nx characters. */
605 screen_write_insertcharacter(struct screen_write_ctx
*ctx
, u_int nx
)
607 struct screen
*s
= ctx
->s
;
608 struct tty_ctx ttyctx
;
613 if (nx
> screen_size_x(s
) - s
->cx
)
614 nx
= screen_size_x(s
) - s
->cx
;
618 screen_write_initctx(ctx
, &ttyctx
, 0);
620 if (s
->cx
<= screen_size_x(s
) - 1)
621 grid_view_insert_cells(s
->grid
, s
->cx
, s
->cy
, nx
);
624 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
627 /* Delete nx characters. */
629 screen_write_deletecharacter(struct screen_write_ctx
*ctx
, u_int nx
)
631 struct screen
*s
= ctx
->s
;
632 struct tty_ctx ttyctx
;
637 if (nx
> screen_size_x(s
) - s
->cx
)
638 nx
= screen_size_x(s
) - s
->cx
;
642 screen_write_initctx(ctx
, &ttyctx
, 0);
644 if (s
->cx
<= screen_size_x(s
) - 1)
645 grid_view_delete_cells(s
->grid
, s
->cx
, s
->cy
, nx
);
648 tty_write(tty_cmd_deletecharacter
, &ttyctx
);
651 /* Clear nx characters. */
653 screen_write_clearcharacter(struct screen_write_ctx
*ctx
, u_int nx
)
655 struct screen
*s
= ctx
->s
;
656 struct tty_ctx ttyctx
;
661 if (nx
> screen_size_x(s
) - s
->cx
)
662 nx
= screen_size_x(s
) - s
->cx
;
666 screen_write_initctx(ctx
, &ttyctx
, 0);
668 if (s
->cx
<= screen_size_x(s
) - 1)
669 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, nx
, 1);
672 tty_write(tty_cmd_clearcharacter
, &ttyctx
);
675 /* Insert ny lines. */
677 screen_write_insertline(struct screen_write_ctx
*ctx
, u_int ny
)
679 struct screen
*s
= ctx
->s
;
680 struct tty_ctx ttyctx
;
685 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
686 if (ny
> screen_size_y(s
) - s
->cy
)
687 ny
= screen_size_y(s
) - s
->cy
;
691 screen_write_initctx(ctx
, &ttyctx
, 0);
693 grid_view_insert_lines(s
->grid
, s
->cy
, ny
);
696 tty_write(tty_cmd_insertline
, &ttyctx
);
700 if (ny
> s
->rlower
+ 1 - s
->cy
)
701 ny
= s
->rlower
+ 1 - s
->cy
;
705 screen_write_initctx(ctx
, &ttyctx
, 0);
707 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
708 grid_view_insert_lines(s
->grid
, s
->cy
, ny
);
710 grid_view_insert_lines_region(s
->grid
, s
->rlower
, s
->cy
, ny
);
713 tty_write(tty_cmd_insertline
, &ttyctx
);
716 /* Delete ny lines. */
718 screen_write_deleteline(struct screen_write_ctx
*ctx
, u_int ny
)
720 struct screen
*s
= ctx
->s
;
721 struct tty_ctx ttyctx
;
726 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
727 if (ny
> screen_size_y(s
) - s
->cy
)
728 ny
= screen_size_y(s
) - s
->cy
;
732 screen_write_initctx(ctx
, &ttyctx
, 0);
734 grid_view_delete_lines(s
->grid
, s
->cy
, ny
);
737 tty_write(tty_cmd_deleteline
, &ttyctx
);
741 if (ny
> s
->rlower
+ 1 - s
->cy
)
742 ny
= s
->rlower
+ 1 - s
->cy
;
746 screen_write_initctx(ctx
, &ttyctx
, 0);
748 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
749 grid_view_delete_lines(s
->grid
, s
->cy
, ny
);
751 grid_view_delete_lines_region(s
->grid
, s
->rlower
, s
->cy
, ny
);
754 tty_write(tty_cmd_deleteline
, &ttyctx
);
757 /* Clear line at cursor. */
759 screen_write_clearline(struct screen_write_ctx
*ctx
)
761 struct screen
*s
= ctx
->s
;
762 struct tty_ctx ttyctx
;
764 screen_write_initctx(ctx
, &ttyctx
, 0);
766 grid_view_clear(s
->grid
, 0, s
->cy
, screen_size_x(s
), 1);
768 tty_write(tty_cmd_clearline
, &ttyctx
);
771 /* Clear to end of line from cursor. */
773 screen_write_clearendofline(struct screen_write_ctx
*ctx
)
775 struct screen
*s
= ctx
->s
;
776 struct tty_ctx ttyctx
;
779 screen_write_initctx(ctx
, &ttyctx
, 0);
781 sx
= screen_size_x(s
);
784 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, sx
- s
->cx
, 1);
786 tty_write(tty_cmd_clearendofline
, &ttyctx
);
789 /* Clear to start of line from cursor. */
791 screen_write_clearstartofline(struct screen_write_ctx
*ctx
)
793 struct screen
*s
= ctx
->s
;
794 struct tty_ctx ttyctx
;
797 screen_write_initctx(ctx
, &ttyctx
, 0);
799 sx
= screen_size_x(s
);
802 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1);
804 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1);
806 tty_write(tty_cmd_clearstartofline
, &ttyctx
);
809 /* Move cursor to px,py. */
811 screen_write_cursormove(struct screen_write_ctx
*ctx
, u_int px
, u_int py
)
813 struct screen
*s
= ctx
->s
;
815 if (px
> screen_size_x(s
) - 1)
816 px
= screen_size_x(s
) - 1;
817 if (py
> screen_size_y(s
) - 1)
818 py
= screen_size_y(s
) - 1;
824 /* Reverse index (up with scroll). */
826 screen_write_reverseindex(struct screen_write_ctx
*ctx
)
828 struct screen
*s
= ctx
->s
;
829 struct tty_ctx ttyctx
;
831 screen_write_initctx(ctx
, &ttyctx
, 0);
833 if (s
->cy
== s
->rupper
)
834 grid_view_scroll_region_down(s
->grid
, s
->rupper
, s
->rlower
);
838 tty_write(tty_cmd_reverseindex
, &ttyctx
);
841 /* Set scroll region. */
843 screen_write_scrollregion(
844 struct screen_write_ctx
*ctx
, u_int rupper
, u_int rlower
)
846 struct screen
*s
= ctx
->s
;
848 if (rupper
> screen_size_y(s
) - 1)
849 rupper
= screen_size_y(s
) - 1;
850 if (rlower
> screen_size_y(s
) - 1)
851 rlower
= screen_size_y(s
) - 1;
852 if (rupper
>= rlower
) /* cannot be one line */
855 /* Cursor moves to top-left. */
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 /* Clear to end of screen from cursor. */
899 screen_write_clearendofscreen(struct screen_write_ctx
*ctx
)
901 struct screen
*s
= ctx
->s
;
902 struct tty_ctx ttyctx
;
905 screen_write_initctx(ctx
, &ttyctx
, 0);
907 sx
= screen_size_x(s
);
908 sy
= screen_size_y(s
);
910 /* Scroll into history if it is enabled and clearing entire screen. */
911 if (s
->cy
== 0 && s
->grid
->flags
& GRID_HISTORY
)
912 grid_view_clear_history(s
->grid
);
915 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, sx
- s
->cx
, 1);
916 grid_view_clear(s
->grid
, 0, s
->cy
+ 1, sx
, sy
- (s
->cy
+ 1));
919 tty_write(tty_cmd_clearendofscreen
, &ttyctx
);
922 /* Clear to start of screen. */
924 screen_write_clearstartofscreen(struct screen_write_ctx
*ctx
)
926 struct screen
*s
= ctx
->s
;
927 struct tty_ctx ttyctx
;
930 screen_write_initctx(ctx
, &ttyctx
, 0);
932 sx
= screen_size_x(s
);
935 grid_view_clear(s
->grid
, 0, 0, sx
, s
->cy
);
937 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1);
939 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1);
941 tty_write(tty_cmd_clearstartofscreen
, &ttyctx
);
944 /* Clear entire screen. */
946 screen_write_clearscreen(struct screen_write_ctx
*ctx
)
948 struct screen
*s
= ctx
->s
;
949 struct tty_ctx ttyctx
;
951 screen_write_initctx(ctx
, &ttyctx
, 0);
953 /* Scroll into history if it is enabled. */
954 if (s
->grid
->flags
& GRID_HISTORY
)
955 grid_view_clear_history(s
->grid
);
958 s
->grid
, 0, 0, screen_size_x(s
), screen_size_y(s
));
961 tty_write(tty_cmd_clearscreen
, &ttyctx
);
964 /* Clear entire history. */
966 screen_write_clearhistory(struct screen_write_ctx
*ctx
)
968 struct screen
*s
= ctx
->s
;
969 struct grid
*gd
= s
->grid
;
971 grid_move_lines(gd
, 0, gd
->hsize
, gd
->sy
);
975 /* Write cell data. */
977 screen_write_cell(struct screen_write_ctx
*ctx
, const struct grid_cell
*gc
)
979 struct screen
*s
= ctx
->s
;
980 struct grid
*gd
= s
->grid
;
981 struct tty_ctx ttyctx
;
982 u_int width
, xx
, last
;
983 struct grid_cell tmp_gc
, *tmp_gcp
;
987 /* Ignore padding. */
988 if (gc
->flags
& GRID_FLAG_PADDING
)
990 width
= grid_cell_width(gc
);
993 * If this is a wide character and there is no room on the screen, for
994 * the entire character, don't print it.
996 if (!(s
->mode
& MODE_WRAP
)
997 && (width
> 1 && (width
> screen_size_x(s
) ||
998 (s
->cx
!= screen_size_x(s
)
999 && s
->cx
> screen_size_x(s
) - width
))))
1003 * If the width is zero, combine onto the previous character, if
1007 grid_cell_get(gc
, &ud
);
1008 if (screen_write_combine(ctx
, &ud
) == 0) {
1009 screen_write_initctx(ctx
, &ttyctx
, 0);
1010 tty_write(tty_cmd_utf8character
, &ttyctx
);
1015 /* Initialise the redraw context, saving the last cell. */
1016 screen_write_initctx(ctx
, &ttyctx
, 1);
1018 /* If in insert mode, make space for the cells. */
1019 if ((s
->mode
& MODE_INSERT
) && s
->cx
<= screen_size_x(s
) - width
) {
1020 xx
= screen_size_x(s
) - s
->cx
- width
;
1021 grid_move_cells(s
->grid
, s
->cx
+ width
, s
->cx
, s
->cy
, xx
);
1026 /* Check this will fit on the current line and wrap if not. */
1027 if ((s
->mode
& MODE_WRAP
) && s
->cx
> screen_size_x(s
) - width
) {
1028 screen_write_linefeed(ctx
, 1);
1029 s
->cx
= 0; /* carriage return */
1032 /* Sanity check cursor position. */
1033 if (s
->cx
> screen_size_x(s
) - width
|| s
->cy
> screen_size_y(s
) - 1)
1036 /* Handle overwriting of UTF-8 characters. */
1037 screen_write_overwrite(ctx
, width
);
1040 * If the new character is UTF-8 wide, fill in padding cells. Have
1041 * already ensured there is enough room.
1043 for (xx
= s
->cx
+ 1; xx
< s
->cx
+ width
; xx
++) {
1044 tmp_gcp
= grid_view_get_cell(gd
, xx
, s
->cy
);
1045 if (tmp_gcp
!= NULL
)
1046 tmp_gcp
->flags
|= GRID_FLAG_PADDING
;
1050 grid_view_set_cell(gd
, s
->cx
, s
->cy
, gc
);
1053 * Move the cursor. If not wrapping, stick at the last character and
1056 last
= !(s
->mode
& MODE_WRAP
);
1057 if (s
->cx
<= screen_size_x(s
) - last
- width
)
1060 s
->cx
= screen_size_x(s
) - last
;
1062 /* Draw to the screen if necessary. */
1065 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
1067 if (screen_check_selection(s
, s
->cx
- width
, s
->cy
)) {
1068 memcpy(&tmp_gc
, &s
->sel
.cell
, sizeof tmp_gc
);
1069 grid_cell_get(gc
, &ud
);
1070 grid_cell_set(&tmp_gc
, &ud
);
1071 tmp_gc
.flags
= gc
->flags
& ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
1072 tmp_gc
.flags
|= s
->sel
.cell
.flags
&
1073 (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
1074 ttyctx
.cell
= &tmp_gc
;
1075 tty_write(tty_cmd_cell
, &ttyctx
);
1078 tty_write(tty_cmd_cell
, &ttyctx
);
1082 /* Combine a UTF-8 zero-width character onto the previous. */
1084 screen_write_combine(struct screen_write_ctx
*ctx
, const struct utf8_data
*ud
)
1086 struct screen
*s
= ctx
->s
;
1087 struct grid
*gd
= s
->grid
;
1088 struct grid_cell
*gc
;
1089 struct utf8_data ud1
;
1091 /* Can't combine if at 0. */
1095 /* Empty data is out. */
1097 fatalx("UTF-8 data empty");
1099 /* Retrieve the previous cell. */
1100 gc
= grid_view_get_cell(gd
, s
->cx
- 1, s
->cy
);
1101 grid_cell_get(gc
, &ud1
);
1103 /* Check there is enough space. */
1104 if (ud1
.size
+ ud
->size
> sizeof ud1
.data
)
1107 /* Append the data and set the cell. */
1108 memcpy(ud1
.data
+ ud1
.size
, ud
->data
, ud
->size
);
1109 ud1
.size
+= ud
->size
;
1110 grid_cell_set(gc
, &ud1
);
1116 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1117 * cell on the screen, so following cells must not be drawn by marking them as
1120 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1121 * character, it is necessary to also overwrite any other cells which covered
1122 * by the same character.
1125 screen_write_overwrite(struct screen_write_ctx
*ctx
, u_int width
)
1127 struct screen
*s
= ctx
->s
;
1128 struct grid
*gd
= s
->grid
;
1129 const struct grid_cell
*gc
;
1132 gc
= grid_view_peek_cell(gd
, s
->cx
, s
->cy
);
1133 if (gc
->flags
& GRID_FLAG_PADDING
) {
1135 * A padding cell, so clear any following and leading padding
1136 * cells back to the character. Don't overwrite the current
1137 * cell as that happens later anyway.
1141 gc
= grid_view_peek_cell(gd
, xx
, s
->cy
);
1142 if (!(gc
->flags
& GRID_FLAG_PADDING
))
1144 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1147 /* Overwrite the character at the start of this padding. */
1148 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1152 * Overwrite any padding cells that belong to a UTF-8 character
1153 * we'll be overwriting with the current character.
1155 xx
= s
->cx
+ width
- 1;
1156 while (++xx
< screen_size_x(s
)) {
1157 gc
= grid_view_peek_cell(gd
, xx
, s
->cy
);
1158 if (!(gc
->flags
& GRID_FLAG_PADDING
))
1160 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1165 screen_write_setselection(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
)
1167 struct tty_ctx ttyctx
;
1169 screen_write_initctx(ctx
, &ttyctx
, 0);
1173 tty_write(tty_cmd_setselection
, &ttyctx
);
1177 screen_write_rawstring(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
)
1179 struct tty_ctx ttyctx
;
1181 screen_write_initctx(ctx
, &ttyctx
, 0);
1185 tty_write(tty_cmd_rawstring
, &ttyctx
);