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
;
491 if (s
->cx
== screen_size_x(s
))
499 /* Cursor down by ny. */
501 screen_write_cursordown(struct screen_write_ctx
*ctx
, u_int ny
)
503 struct screen
*s
= ctx
->s
;
508 if (s
->cy
> s
->rlower
) {
510 if (ny
> screen_size_y(s
) - 1 - s
->cy
)
511 ny
= screen_size_y(s
) - 1 - s
->cy
;
514 if (ny
> s
->rlower
- s
->cy
)
515 ny
= s
->rlower
- s
->cy
;
517 if (s
->cx
== screen_size_x(s
))
525 /* Cursor right by nx. */
527 screen_write_cursorright(struct screen_write_ctx
*ctx
, u_int nx
)
529 struct screen
*s
= ctx
->s
;
534 if (nx
> screen_size_x(s
) - 1 - s
->cx
)
535 nx
= screen_size_x(s
) - 1 - s
->cx
;
542 /* Cursor left by nx. */
544 screen_write_cursorleft(struct screen_write_ctx
*ctx
, u_int nx
)
546 struct screen
*s
= ctx
->s
;
559 /* Backspace; cursor left unless at start of wrapped line when can move up. */
561 screen_write_backspace(struct screen_write_ctx
*ctx
)
563 struct screen
*s
= ctx
->s
;
564 struct grid_line
*gl
;
569 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ s
->cy
- 1];
570 if (gl
->flags
& GRID_LINE_WRAPPED
) {
572 s
->cx
= screen_size_x(s
) - 1;
578 /* VT100 alignment test. */
580 screen_write_alignmenttest(struct screen_write_ctx
*ctx
)
582 struct screen
*s
= ctx
->s
;
583 struct tty_ctx ttyctx
;
587 screen_write_initctx(ctx
, &ttyctx
, 0);
589 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
590 grid_cell_one(&gc
, 'E');
592 for (yy
= 0; yy
< screen_size_y(s
); yy
++) {
593 for (xx
= 0; xx
< screen_size_x(s
); xx
++)
594 grid_view_set_cell(s
->grid
, xx
, yy
, &gc
);
602 s
->rlower
= screen_size_y(s
) - 1;
604 tty_write(tty_cmd_alignmenttest
, &ttyctx
);
607 /* Insert nx characters. */
609 screen_write_insertcharacter(struct screen_write_ctx
*ctx
, u_int nx
)
611 struct screen
*s
= ctx
->s
;
612 struct tty_ctx ttyctx
;
617 if (nx
> screen_size_x(s
) - s
->cx
)
618 nx
= screen_size_x(s
) - s
->cx
;
622 screen_write_initctx(ctx
, &ttyctx
, 0);
624 if (s
->cx
<= screen_size_x(s
) - 1)
625 grid_view_insert_cells(s
->grid
, s
->cx
, s
->cy
, nx
);
628 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
631 /* Delete nx characters. */
633 screen_write_deletecharacter(struct screen_write_ctx
*ctx
, u_int nx
)
635 struct screen
*s
= ctx
->s
;
636 struct tty_ctx ttyctx
;
641 if (nx
> screen_size_x(s
) - s
->cx
)
642 nx
= screen_size_x(s
) - s
->cx
;
646 screen_write_initctx(ctx
, &ttyctx
, 0);
648 if (s
->cx
<= screen_size_x(s
) - 1)
649 grid_view_delete_cells(s
->grid
, s
->cx
, s
->cy
, nx
);
652 tty_write(tty_cmd_deletecharacter
, &ttyctx
);
655 /* Clear nx characters. */
657 screen_write_clearcharacter(struct screen_write_ctx
*ctx
, u_int nx
)
659 struct screen
*s
= ctx
->s
;
660 struct tty_ctx ttyctx
;
665 if (nx
> screen_size_x(s
) - s
->cx
)
666 nx
= screen_size_x(s
) - s
->cx
;
670 screen_write_initctx(ctx
, &ttyctx
, 0);
672 if (s
->cx
<= screen_size_x(s
) - 1)
673 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, nx
, 1);
676 tty_write(tty_cmd_clearcharacter
, &ttyctx
);
679 /* Insert ny lines. */
681 screen_write_insertline(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_insert_lines(s
->grid
, s
->cy
, ny
);
700 tty_write(tty_cmd_insertline
, &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_insert_lines(s
->grid
, s
->cy
, ny
);
714 grid_view_insert_lines_region(s
->grid
, s
->rlower
, s
->cy
, ny
);
717 tty_write(tty_cmd_insertline
, &ttyctx
);
720 /* Delete ny lines. */
722 screen_write_deleteline(struct screen_write_ctx
*ctx
, u_int ny
)
724 struct screen
*s
= ctx
->s
;
725 struct tty_ctx ttyctx
;
730 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
) {
731 if (ny
> screen_size_y(s
) - s
->cy
)
732 ny
= screen_size_y(s
) - s
->cy
;
736 screen_write_initctx(ctx
, &ttyctx
, 0);
738 grid_view_delete_lines(s
->grid
, s
->cy
, ny
);
741 tty_write(tty_cmd_deleteline
, &ttyctx
);
745 if (ny
> s
->rlower
+ 1 - s
->cy
)
746 ny
= s
->rlower
+ 1 - s
->cy
;
750 screen_write_initctx(ctx
, &ttyctx
, 0);
752 if (s
->cy
< s
->rupper
|| s
->cy
> s
->rlower
)
753 grid_view_delete_lines(s
->grid
, s
->cy
, ny
);
755 grid_view_delete_lines_region(s
->grid
, s
->rlower
, s
->cy
, ny
);
758 tty_write(tty_cmd_deleteline
, &ttyctx
);
761 /* Clear line at cursor. */
763 screen_write_clearline(struct screen_write_ctx
*ctx
)
765 struct screen
*s
= ctx
->s
;
766 struct tty_ctx ttyctx
;
768 screen_write_initctx(ctx
, &ttyctx
, 0);
770 grid_view_clear(s
->grid
, 0, s
->cy
, screen_size_x(s
), 1);
772 tty_write(tty_cmd_clearline
, &ttyctx
);
775 /* Clear to end of line from cursor. */
777 screen_write_clearendofline(struct screen_write_ctx
*ctx
)
779 struct screen
*s
= ctx
->s
;
780 struct tty_ctx ttyctx
;
783 screen_write_initctx(ctx
, &ttyctx
, 0);
785 sx
= screen_size_x(s
);
788 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, sx
- s
->cx
, 1);
790 tty_write(tty_cmd_clearendofline
, &ttyctx
);
793 /* Clear to start of line from cursor. */
795 screen_write_clearstartofline(struct screen_write_ctx
*ctx
)
797 struct screen
*s
= ctx
->s
;
798 struct tty_ctx ttyctx
;
801 screen_write_initctx(ctx
, &ttyctx
, 0);
803 sx
= screen_size_x(s
);
806 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1);
808 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1);
810 tty_write(tty_cmd_clearstartofline
, &ttyctx
);
813 /* Move cursor to px,py. */
815 screen_write_cursormove(struct screen_write_ctx
*ctx
, u_int px
, u_int py
)
817 struct screen
*s
= ctx
->s
;
819 if (px
> screen_size_x(s
) - 1)
820 px
= screen_size_x(s
) - 1;
821 if (py
> screen_size_y(s
) - 1)
822 py
= screen_size_y(s
) - 1;
828 /* Reverse index (up with scroll). */
830 screen_write_reverseindex(struct screen_write_ctx
*ctx
)
832 struct screen
*s
= ctx
->s
;
833 struct tty_ctx ttyctx
;
835 screen_write_initctx(ctx
, &ttyctx
, 0);
837 if (s
->cy
== s
->rupper
)
838 grid_view_scroll_region_down(s
->grid
, s
->rupper
, s
->rlower
);
842 tty_write(tty_cmd_reverseindex
, &ttyctx
);
845 /* Set scroll region. */
847 screen_write_scrollregion(
848 struct screen_write_ctx
*ctx
, u_int rupper
, u_int rlower
)
850 struct screen
*s
= ctx
->s
;
852 if (rupper
> screen_size_y(s
) - 1)
853 rupper
= screen_size_y(s
) - 1;
854 if (rlower
> screen_size_y(s
) - 1)
855 rlower
= screen_size_y(s
) - 1;
856 if (rupper
>= rlower
) /* cannot be one line */
859 /* Cursor moves to top-left. */
869 screen_write_linefeed(struct screen_write_ctx
*ctx
, int wrapped
)
871 struct screen
*s
= ctx
->s
;
872 struct grid_line
*gl
;
873 struct tty_ctx ttyctx
;
875 screen_write_initctx(ctx
, &ttyctx
, 0);
877 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ s
->cy
];
879 gl
->flags
|= GRID_LINE_WRAPPED
;
881 gl
->flags
&= ~GRID_LINE_WRAPPED
;
883 if (s
->cy
== s
->rlower
)
884 grid_view_scroll_region_up(s
->grid
, s
->rupper
, s
->rlower
);
885 else if (s
->cy
< screen_size_y(s
) - 1)
888 ttyctx
.num
= wrapped
;
889 tty_write(tty_cmd_linefeed
, &ttyctx
);
892 /* Carriage return (cursor to start of line). */
894 screen_write_carriagereturn(struct screen_write_ctx
*ctx
)
896 struct screen
*s
= ctx
->s
;
901 /* Clear to end of screen from cursor. */
903 screen_write_clearendofscreen(struct screen_write_ctx
*ctx
)
905 struct screen
*s
= ctx
->s
;
906 struct tty_ctx ttyctx
;
909 screen_write_initctx(ctx
, &ttyctx
, 0);
911 sx
= screen_size_x(s
);
912 sy
= screen_size_y(s
);
914 /* Scroll into history if it is enabled and clearing entire screen. */
915 if (s
->cy
== 0 && s
->grid
->flags
& GRID_HISTORY
)
916 grid_view_clear_history(s
->grid
);
919 grid_view_clear(s
->grid
, s
->cx
, s
->cy
, sx
- s
->cx
, 1);
920 grid_view_clear(s
->grid
, 0, s
->cy
+ 1, sx
, sy
- (s
->cy
+ 1));
923 tty_write(tty_cmd_clearendofscreen
, &ttyctx
);
926 /* Clear to start of screen. */
928 screen_write_clearstartofscreen(struct screen_write_ctx
*ctx
)
930 struct screen
*s
= ctx
->s
;
931 struct tty_ctx ttyctx
;
934 screen_write_initctx(ctx
, &ttyctx
, 0);
936 sx
= screen_size_x(s
);
939 grid_view_clear(s
->grid
, 0, 0, sx
, s
->cy
);
941 grid_view_clear(s
->grid
, 0, s
->cy
, sx
, 1);
943 grid_view_clear(s
->grid
, 0, s
->cy
, s
->cx
+ 1, 1);
945 tty_write(tty_cmd_clearstartofscreen
, &ttyctx
);
948 /* Clear entire screen. */
950 screen_write_clearscreen(struct screen_write_ctx
*ctx
)
952 struct screen
*s
= ctx
->s
;
953 struct tty_ctx ttyctx
;
955 screen_write_initctx(ctx
, &ttyctx
, 0);
957 /* Scroll into history if it is enabled. */
958 if (s
->grid
->flags
& GRID_HISTORY
)
959 grid_view_clear_history(s
->grid
);
962 s
->grid
, 0, 0, screen_size_x(s
), screen_size_y(s
));
965 tty_write(tty_cmd_clearscreen
, &ttyctx
);
968 /* Clear entire history. */
970 screen_write_clearhistory(struct screen_write_ctx
*ctx
)
972 struct screen
*s
= ctx
->s
;
973 struct grid
*gd
= s
->grid
;
975 grid_move_lines(gd
, 0, gd
->hsize
, gd
->sy
);
979 /* Write cell data. */
981 screen_write_cell(struct screen_write_ctx
*ctx
, const struct grid_cell
*gc
)
983 struct screen
*s
= ctx
->s
;
984 struct grid
*gd
= s
->grid
;
985 struct tty_ctx ttyctx
;
986 u_int width
, xx
, last
;
987 struct grid_cell tmp_gc
, *tmp_gcp
;
991 /* Ignore padding. */
992 if (gc
->flags
& GRID_FLAG_PADDING
)
994 width
= grid_cell_width(gc
);
997 * If this is a wide character and there is no room on the screen, for
998 * the entire character, don't print it.
1000 if (!(s
->mode
& MODE_WRAP
)
1001 && (width
> 1 && (width
> screen_size_x(s
) ||
1002 (s
->cx
!= screen_size_x(s
)
1003 && s
->cx
> screen_size_x(s
) - width
))))
1007 * If the width is zero, combine onto the previous character, if
1011 grid_cell_get(gc
, &ud
);
1012 if (screen_write_combine(ctx
, &ud
) == 0) {
1013 screen_write_initctx(ctx
, &ttyctx
, 0);
1014 tty_write(tty_cmd_utf8character
, &ttyctx
);
1019 /* Initialise the redraw context, saving the last cell. */
1020 screen_write_initctx(ctx
, &ttyctx
, 1);
1022 /* If in insert mode, make space for the cells. */
1023 if ((s
->mode
& MODE_INSERT
) && s
->cx
<= screen_size_x(s
) - width
) {
1024 xx
= screen_size_x(s
) - s
->cx
- width
;
1025 grid_move_cells(s
->grid
, s
->cx
+ width
, s
->cx
, s
->cy
, xx
);
1030 /* Check this will fit on the current line and wrap if not. */
1031 if ((s
->mode
& MODE_WRAP
) && s
->cx
> screen_size_x(s
) - width
) {
1032 screen_write_linefeed(ctx
, 1);
1033 s
->cx
= 0; /* carriage return */
1036 /* Sanity check cursor position. */
1037 if (s
->cx
> screen_size_x(s
) - width
|| s
->cy
> screen_size_y(s
) - 1)
1040 /* Handle overwriting of UTF-8 characters. */
1041 screen_write_overwrite(ctx
, width
);
1044 * If the new character is UTF-8 wide, fill in padding cells. Have
1045 * already ensured there is enough room.
1047 for (xx
= s
->cx
+ 1; xx
< s
->cx
+ width
; xx
++) {
1048 tmp_gcp
= grid_view_get_cell(gd
, xx
, s
->cy
);
1049 if (tmp_gcp
!= NULL
)
1050 tmp_gcp
->flags
|= GRID_FLAG_PADDING
;
1054 grid_view_set_cell(gd
, s
->cx
, s
->cy
, gc
);
1057 * Move the cursor. If not wrapping, stick at the last character and
1060 last
= !(s
->mode
& MODE_WRAP
);
1061 if (s
->cx
<= screen_size_x(s
) - last
- width
)
1064 s
->cx
= screen_size_x(s
) - last
;
1066 /* Draw to the screen if necessary. */
1069 tty_write(tty_cmd_insertcharacter
, &ttyctx
);
1071 if (screen_check_selection(s
, s
->cx
- width
, s
->cy
)) {
1072 memcpy(&tmp_gc
, &s
->sel
.cell
, sizeof tmp_gc
);
1073 grid_cell_get(gc
, &ud
);
1074 grid_cell_set(&tmp_gc
, &ud
);
1075 tmp_gc
.flags
= gc
->flags
& ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
1076 tmp_gc
.flags
|= s
->sel
.cell
.flags
&
1077 (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
1078 ttyctx
.cell
= &tmp_gc
;
1079 tty_write(tty_cmd_cell
, &ttyctx
);
1082 tty_write(tty_cmd_cell
, &ttyctx
);
1086 /* Combine a UTF-8 zero-width character onto the previous. */
1088 screen_write_combine(struct screen_write_ctx
*ctx
, const struct utf8_data
*ud
)
1090 struct screen
*s
= ctx
->s
;
1091 struct grid
*gd
= s
->grid
;
1092 struct grid_cell
*gc
;
1093 struct utf8_data ud1
;
1095 /* Can't combine if at 0. */
1099 /* Empty data is out. */
1101 fatalx("UTF-8 data empty");
1103 /* Retrieve the previous cell. */
1104 gc
= grid_view_get_cell(gd
, s
->cx
- 1, s
->cy
);
1105 grid_cell_get(gc
, &ud1
);
1107 /* Check there is enough space. */
1108 if (ud1
.size
+ ud
->size
> sizeof ud1
.data
)
1111 /* Append the data and set the cell. */
1112 memcpy(ud1
.data
+ ud1
.size
, ud
->data
, ud
->size
);
1113 ud1
.size
+= ud
->size
;
1114 grid_cell_set(gc
, &ud1
);
1120 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1121 * cell on the screen, so following cells must not be drawn by marking them as
1124 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1125 * character, it is necessary to also overwrite any other cells which covered
1126 * by the same character.
1129 screen_write_overwrite(struct screen_write_ctx
*ctx
, u_int width
)
1131 struct screen
*s
= ctx
->s
;
1132 struct grid
*gd
= s
->grid
;
1133 const struct grid_cell
*gc
;
1136 gc
= grid_view_peek_cell(gd
, s
->cx
, s
->cy
);
1137 if (gc
->flags
& GRID_FLAG_PADDING
) {
1139 * A padding cell, so clear any following and leading padding
1140 * cells back to the character. Don't overwrite the current
1141 * cell as that happens later anyway.
1145 gc
= grid_view_peek_cell(gd
, xx
, s
->cy
);
1146 if (!(gc
->flags
& GRID_FLAG_PADDING
))
1148 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1151 /* Overwrite the character at the start of this padding. */
1152 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1156 * Overwrite any padding cells that belong to a UTF-8 character
1157 * we'll be overwriting with the current character.
1159 xx
= s
->cx
+ width
- 1;
1160 while (++xx
< screen_size_x(s
)) {
1161 gc
= grid_view_peek_cell(gd
, xx
, s
->cy
);
1162 if (!(gc
->flags
& GRID_FLAG_PADDING
))
1164 grid_view_set_cell(gd
, xx
, s
->cy
, &grid_default_cell
);
1169 screen_write_setselection(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
)
1171 struct tty_ctx ttyctx
;
1173 screen_write_initctx(ctx
, &ttyctx
, 0);
1177 tty_write(tty_cmd_setselection
, &ttyctx
);
1181 screen_write_rawstring(struct screen_write_ctx
*ctx
, u_char
*str
, u_int len
)
1183 struct tty_ctx ttyctx
;
1185 screen_write_initctx(ctx
, &ttyctx
, 0);
1189 tty_write(tty_cmd_rawstring
, &ttyctx
);