/
termout.c
5489 lines (5214 loc) · 175 KB
/
termout.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// termout.c (part of mintty)
// Copyright 2008-22 Andy Koppe, 2017-22 Thomas Wolff
// Adapted from code from PuTTY-0.60 by Simon Tatham and team.
// Licensed under the terms of the GNU General Public License v3 or later.
#include "termpriv.h"
#include "winpriv.h" // colours, win_get_font, win_change_font, win_led, win_set_scrollview
#include "win.h"
#include "appinfo.h"
#include "charset.h"
#include "child.h"
#include "print.h"
#include "sixel.h"
#include "winimg.h"
#include "tek.h"
#include "base64.h"
#include "unicodever.t"
#include <termios.h>
#include <sys/time.h>
#define TERM_CMD_BUF_INC_STEP 128
#define TERM_CMD_BUF_MAX_SIZE (1024 * 1024)
#define SUB_PARS (1 << (sizeof(*term.csi_argv) * 8 - 1))
/* This combines two characters into one value, for the purpose of pairing
* any modifier byte and the final byte in escape sequences.
*/
#define CPAIR(x, y) ((x) << 8 | (y))
static string primary_da1 = "\e[?1;2c";
static string primary_da2 = "\e[?62;1;2;4;6;9;15;22;29c";
static string primary_da3 = "\e[?63;1;2;4;6;9;11;15;22;29c";
static string primary_da4 = "\e[?64;1;2;4;6;9;11;15;21;22;28;29c";
static string primary_da5 = "\e[?65;1;2;4;6;9;11;15;21;22;28;29c";
/* Registered Extensions to the Character Cell Display Service Class
1 132 Column Display
2 Printer Port
3 ReGIS Display
4 Sixels Display
6 Selectively Erasable Characters
9 National Replacement Character Sets
11 Status Line (DEC STD 070)
15 Technical Character Set
21 Horizontal Scrolling
22 Color Text
28 Rectangular Editing
29 Text Locator
*/
static bool
term_push_cmd(char c)
{
uint new_size;
/* Need 1 more for null byte */
if (term.cmd_len + 1 < term.cmd_buf_cap) {
term.cmd_buf[term.cmd_len++] = c;
term.cmd_buf[term.cmd_len] = 0;
return true;
}
if (term.cmd_buf_cap >= TERM_CMD_BUF_MAX_SIZE) {
/* Server sends too many cmd characters */
return false;
}
new_size = term.cmd_buf_cap + TERM_CMD_BUF_INC_STEP;
if (new_size >= TERM_CMD_BUF_MAX_SIZE) {
// cosmetic limitation (relevant limitation above)
new_size = TERM_CMD_BUF_MAX_SIZE;
}
term.cmd_buf = renewn(term.cmd_buf, new_size);
term.cmd_buf_cap = new_size;
term.cmd_buf[term.cmd_len++] = c;
term.cmd_buf[term.cmd_len] = 0;
return true;
}
static void
enable_progress(void)
{
term.lines[term.curs.y]->lattr |= LATTR_PROGRESS;
}
/*
* Move the cursor to a given position, clipping at boundaries.
* We may or may not want to clip at the scroll margin: marg_clip is
* 0 not to,
* 1 to disallow _passing_ the margins, and
* 2 to disallow even _being_ outside the margins.
*/
static void
move(int x, int y, int marg_clip)
{
term_cursor *curs = &term.curs;
if (marg_clip) {
if ((curs->y >= term.marg_top || marg_clip == 2) && y < term.marg_top)
y = term.marg_top;
if ((curs->y <= term.marg_bot || marg_clip == 2) && y > term.marg_bot)
y = term.marg_bot;
if ((curs->x >= term.marg_left || marg_clip == 2) && x < term.marg_left)
x = term.marg_left;
if ((curs->x <= term.marg_right || marg_clip == 2) && x > term.marg_right)
x = term.marg_right;
}
if (x < 0)
x = 0;
if (x >= term.cols)
x = term.cols - 1;
if (term.st_active) {
if (curs->y < term.rows)
y = term.rows;
if (y >= term_allrows)
y = term_allrows - 1;
}
else {
if (y < 0)
y = 0;
if (y >= term.rows)
y = term.rows - 1;
}
curs->x = x;
curs->y = y;
curs->wrapnext = false;
}
/*
* Save the cursor and SGR mode.
About status line save/restore cursor, refer to DEC VT420 p. 271, VT520 p. 5-92:
Notes on DECSC and DECRC
• The terminal maintains a separate DECSC buffer for the main
display and the status line. This feature lets you save a
separate operating state for the main display and the status line.
*/
static void
save_cursor(void)
{
if (term.st_active) {
term.st_saved_curs = term.curs;
term.st_saved_curs.y -= term.rows;
}
else
term.saved_cursors[term.on_alt_screen] = term.curs;
}
/*
* Restore the cursor and SGR mode.
*/
static void
restore_cursor(void)
{
term_cursor *curs = &term.curs;
if (term.st_active) {
*curs = term.st_saved_curs;
curs->y += term.rows;
}
else {
*curs = term.saved_cursors[term.on_alt_screen];
term.erase_char.attr = curs->attr;
term.erase_char.attr.attr &= (ATTR_FGMASK | ATTR_BGMASK);
term.erase_char.attr.attr |= TATTR_CLEAR;
}
/* Make sure the window hasn't shrunk since the save */
if (curs->x >= term.cols)
curs->x = term.cols - 1;
short rows = term.st_active ? term_allrows : term.rows;
if (curs->y >= rows)
curs->y = rows - 1;
/* In origin mode, make sure the cursor position is within margins */
if (curs->origin) {
if (curs->x < term.marg_left)
curs->x = term.marg_left;
else if (curs->x > term.marg_right)
curs->x = term.marg_right;
if (!term.st_active) {
if (curs->y < term.marg_top)
curs->y = term.marg_top;
else if (curs->y > term.marg_bot)
curs->y = term.marg_bot;
}
}
/*
* wrapnext might reset to False
* if the x position is no longer at the rightmost edge.
*/
if (curs->wrapnext && curs->x < term.cols - 1 && curs->x != term.marg_right)
curs->wrapnext = false;
term_update_cs();
}
/*
* Insert or delete characters within the current line.
* n is +ve if insertion is desired, and -ve for deletion.
*/
static void
insert_char(int n)
{
if (term.curs.x < term.marg_left || term.curs.x > term.marg_right)
return;
bool del = n < 0;
int m;
term_cursor *curs = &term.curs;
termline *line = term.lines[curs->y];
int cols = min(line->cols, line->size);
cols = min(cols, term.marg_right + 1);
n = (n < 0 ? -n : n);
if (n > cols - curs->x)
n = cols - curs->x;
m = cols - curs->x - n;
term_check_boundary(curs->x, curs->y);
term_check_boundary(curs->x + m, curs->y);
if (del) {
for (int j = 0; j < m; j++)
move_termchar(line, line->chars + curs->x + j,
line->chars + curs->x + j + n);
while (n--)
line->chars[curs->x + m++] = term.erase_char;
}
else {
for (int j = m; j--;)
move_termchar(line, line->chars + curs->x + j + n,
line->chars + curs->x + j);
while (n--)
line->chars[curs->x + n] = term.erase_char;
}
}
static int
charwidth(xchar chr)
{
#if HAS_LOCALES
if (cfg.charwidth % 10)
return xcwidth(chr);
else
if (chr > 0xFFFF)
return wcswidth((wchar[]){high_surrogate(chr), low_surrogate(chr)}, 2);
else
return wcwidth(chr);
#else
return xcwidth(chr);
#endif
}
#define top_y (term.st_active ? term.rows : 0)
#define bot_y (term.st_active ? term_allrows : term.rows)
#define marg_y (term.st_active ? term_allrows - 1 : term.marg_bot)
static void
attr_rect(cattrflags add, cattrflags sub, cattrflags xor, short y0, short x0, short y1, short x1)
{
//printf("attr_rect %d,%d..%d,%d +%llX -%llX ^%llX\n", y0, x0, y1, x1, add, sub, xor);
if (term.st_active) {
y0 += term.rows;
y1 += term.rows;
}
y0--; x0--; y1--; x1--;
if (term.curs.origin) {
y0 += term.marg_top;
x0 += term.marg_left;
y1 += term.marg_top;
x1 += term.marg_left;
}
if (y0 < top_y)
y0 = top_y;
if (x0 < 0)
x0 = 0;
if (y1 >= bot_y)
y1 = bot_y - 1;
if (x1 >= term.cols)
x1 = term.cols - 1;
//printf("%d,%d..%d,%d\n", y0, x0, y1, x1);
for (int y = y0; y <= y1; y++) {
termline * l = term.lines[y];
int xl = x0;
int xr = x1;
if (!term.attr_rect) {
if (y != y0)
xl = term.marg_left;
if (y != y1)
xr = term.marg_right;
}
for (int x = xl; x <= xr; x++) {
//printf("attr %d:%d\n", y, x);
cattrflags ca = l->chars[x].attr.attr;
ca ^= xor;
ca &= ~sub;
ca |= add;
if (ca != l->chars[x].attr.attr) {
if (x == xl)
term_check_boundary(x, y);
if (x == xr)
term_check_boundary(x + 1, y);
}
l->chars[x].attr.attr = ca;
}
}
}
//static void write_char(wchar c, int width);
static void term_do_write(const char *buf, uint len, bool fix_status);
/*
Fix cursor position with respect to status area;
this final fix saves a lot of detailed checks elsewhere.
*/
static void
term_fix_status(void)
{
if (term.st_active && term.curs.y < term.rows)
term.curs.y = term.rows;
else if (!term.st_active && term.curs.y >= term.rows)
term.curs.y = term.rows - 1;
}
static void
fill_rect(xchar chr, cattr attr, bool sel, short y0, short x0, short y1, short x1)
{
//printf("fill_rect %d,%d..%d,%d\n", y0, x0, y1, x1);
if (term.st_active) {
y0 += term.rows;
y1 += term.rows;
}
int width = charwidth(chr);
if (chr == UCSWIDE || width < 1)
return;
wchar low = 0;
if (chr > 0xFFFF) {
low = low_surrogate(chr);
chr = high_surrogate(chr);
}
y0--; x0--; y1--; x1--;
if (term.curs.origin) {
y0 += term.marg_top;
x0 += term.marg_left;
y1 += term.marg_top;
x1 += term.marg_left;
}
if (y0 < top_y)
y0 = top_y;
if (x0 < 0)
x0 = 0;
if (y1 >= bot_y)
y1 = bot_y - 1;
if (x1 >= term.cols)
x1 = term.cols - 1;
//printf("%d,%d..%d,%d\n", y0, x0, y1, x1);
//printf("gl %d gr %d csets %d %d %d %d /%d sup %d acs %d\n", term.curs.gl, term.curs.gr, term.curs.csets[0], term.curs.csets[1], term.curs.csets[2], term.curs.csets[3], term.curs.cset_single, term.curs.decsupp, term.curs.oem_acs);
if ((chr > ' ' && chr < 0x80
&& (term.curs.csets[term.curs.gl] != CSET_ASCII
||
term.curs.cset_single != CSET_ASCII
)
)
||
(chr >= 0x80 && chr < 0x100
&& ((term.curs.gr && term.curs.csets[term.curs.gr] != CSET_ASCII)
|| term.curs.oem_acs
)
)
|| (chr >= 0x2580 && chr <= 0x259F)
)
{
term_cursor csav = term.curs;
term.curs.attr = attr;
#ifdef debug_FRA_special
// make this code branch visible
term.curs.attr.attr &= ~ATTR_FGMASK;
term.curs.attr.attr |= RED_I << ATTR_FGSHIFT;
#endif
term.curs.width = 1;
if (!(width < 2 || (cs_ambig_wide && is_ambig(chr))))
term.curs.attr.attr |= TATTR_CLEAR | TATTR_NARROW;
term.state = NORMAL;
char * cbuf = 0;
if (chr > 0xFF) {
wchar * wc = (wchar[]){chr, low, 0};
cbuf = cs__wcstombs(wc);
}
for (int y = y0; y <= y1; y++) {
term.curs.y = y;
for (int x = x0; x <= x1; x++) {
term.curs.x = x;
term.curs.cset_single = csav.cset_single;
if (chr > 0xFF) {
//write_char(chr, 1); // would skip NRCS handling in term_do_write
term_do_write(cbuf, strlen(cbuf), false);
}
else {
char c = chr;
term_do_write(&c, 1, false);
}
}
}
if (cbuf)
free(cbuf);
term.curs = csav;
term.curs.cset_single = CSET_ASCII;
return;
}
if (width > 1)
attr.attr |= TATTR_CLEAR | TATTR_NARROW;
for (int y = y0; y <= y1; y++) {
termline * l = term.lines[y];
bool prevprot = true; // not false!
for (int x = x0; x <= x1; x++) {
//printf("fill %d:%d\n", y, x);
bool prot = sel && l->chars[x].attr.attr & ATTR_PROTECTED;
if (prot != prevprot) {
// |P not here, no check
// |N check
// NP check only current position
// PN check
if (!prot) { // includes the case x == x0
// clear previous half of wide char, even if protected
term_check_boundary(x0, y);
}
else if (l->chars[x].chr == UCSWIDE) {
// clear right half of wide char, even if protected;
// calling term_check_boundary would overwrite previous fill char
clear_cc(l, x);
l->chars[x].chr = ' ';
}
}
// clear wide char on right area border unless protected
if (!prot && x == x1)
term_check_boundary(x1 + 1, y);
prevprot = prot;
if (!sel || !prot) {
clear_cc(l, x);
l->chars[x].chr = chr;
l->chars[x].attr = attr;
if (low)
add_cc(l, x, low, attr);
}
}
}
}
static void
copy_rect(short y0, short x0, short y1, short x1, short y2, short x2)
{
//printf("copy_rect %d,%d..%d,%d -> %d,%d\n", y0, x0, y1, x1, y2, x2);
if (term.st_active) {
y0 += term.rows;
y1 += term.rows;
y2 += term.rows;
}
y0--; x0--; y1--; x1--; y2--; x2--;
if (term.curs.origin) {
y0 += term.marg_top;
x0 += term.marg_left;
y1 += term.marg_top;
x1 += term.marg_left;
y2 += term.marg_top;
x2 += term.marg_left;
}
if (y0 < top_y)
y0 = top_y;
if (x0 < 0)
x0 = 0;
if (y1 >= bot_y)
y1 = bot_y - 1;
if (x1 >= term.cols)
x1 = term.cols - 1;
if (y2 < top_y)
y2 = top_y;
if (x2 < 0)
x2 = 0;
if (y2 + y1 - y0 >= bot_y)
y1 = bot_y + y0 - y2 - 1;
if (x2 + x1 - x0 >= term.cols)
x1 = term.cols + x0 - x2 - 1;
//printf("%d,%d..%d,%d -> %d,%d\n", y0, x0, y1, x1, y2, x2);
bool down = y2 > y0;
bool left = x2 > x0;
for (int y = down ? y1 : y0; down ? y >= y0 : y <= y1; down ? y-- : y++) {
termline * src = term.lines[y];
termline * dst = term.lines[y + y2 - y0];
term_check_boundary(x2, y + y2 - y0);
term_check_boundary(x2 + x1 - x0 + 1, y + y2 - y0);
for (int x = left ? x1 : x0; left ? x >= x0 : x <= x1; left ? x-- : x++) {
copy_termchar(dst, x + x2 - x0, &src->chars[x]);
//printf("copy %d:%d -> %d:%d\n", y, x, y + y2 - y0, x + x2 - x0);
if ((x == x0 && src->chars[x].chr == UCSWIDE)
|| (x == x1 && charwidth(src->chars[x].chr) != 1)
)
{
clear_cc(dst, x);
dst->chars[x].chr = ' ';
}
}
}
}
void
scroll_rect(int topline, int botline, int lines)
{
//printf("scroll_rect %d..%d %s%d\n", topline, botline, lines > 0 ? "+" : "", lines);
int y0, y1, y2, e0, e1;
if (lines < 0) { // downwards
// scroll copy clear
// 4 -2 4 6 4
// 20 18 5
if (topline - lines > term.marg_bot + 1)
lines = topline - term.marg_bot - 1;
y0 = topline;
y1 = botline + lines;
y2 = topline - lines;
e0 = y0;
e1 = y0 - lines - 1;
}
else {
// scroll copy clear
// 4 +2 6 4 19
// 20 20 20
if (topline + lines > term.marg_bot + 1)
lines = term.marg_bot + 1 - topline;
y0 = topline + lines;
y1 = botline;
y2 = topline;
e0 = y1 - lines + 1;
e1 = y1;
}
y0++; y1++; y2++; e0++; e1++;
int xl = term.marg_left + 1;
int xr = term.marg_right + 1;
if (term.curs.origin) {
// compensate for the originmode applied in the functions called below
xl = 1;
xr = term.marg_right - term.marg_left + 1;
y0 -= term.marg_top;
y1 -= term.marg_top;
y2 -= term.marg_top;
e0 -= term.marg_top;
e1 -= term.marg_top;
}
copy_rect(y0, xl, y1, xr, y2, xl);
fill_rect(' ', term.curs.attr, false, e0, xl, e1, xr);
}
static void
insdel_column(int col, bool del, int n)
{
//printf("insdel_column @%d %d marg %d..%d\n", col, n, term.marg_left, term.marg_right);
int x0, x1, x2, e0, e1;
if (del) {
x0 = col + n;
x1 = term.marg_right;
x2 = col;
e0 = term.marg_right - n + 1;
e1 = term.marg_right;
if (x0 > term.marg_right) {
x0 = term.marg_right;
e0 = col;
}
}
else {
if (col + n > term.marg_right + 1)
n = term.marg_right + 1 - col;
x0 = col;
x1 = term.marg_right - n;
x2 = col + n;
e0 = col;
e1 = col + n - 1;
}
x0++; x1++; x2++; e0++; e1++;
int yt = term.marg_top + 1;
int yb = marg_y + 1;
if (term.curs.origin) {
// compensate for the originmode applied in the functions called below
yt = 1;
yb = marg_y - term.marg_top + 1;
x0 -= term.marg_left;
x1 -= term.marg_left;
x2 -= term.marg_left;
e0 -= term.marg_left;
e1 -= term.marg_left;
}
copy_rect(yt, x0, yb, x1, yt, x2);
fill_rect(' ', term.curs.attr, false, yt, e0, yb, e1);
}
static uint
sum_rect(short y0, short x0, short y1, short x1)
{
//printf("sum_rect %d,%d..%d,%d\n", y0, x0, y1, x1);
if (term.st_active) {
y0 += term.rows;
y1 += term.rows;
}
y0--; x0--; y1--; x1--;
if (term.curs.origin) {
y0 += term.marg_top;
x0 += term.marg_left;
y1 += term.marg_top;
x1 += term.marg_left;
}
if (y0 < top_y)
y0 = top_y;
if (x0 < 0)
x0 = 0;
if (y1 >= bot_y)
y1 = bot_y - 1;
if (x1 >= term.cols)
x1 = term.cols - 1;
//printf("%d,%d..%d,%d\n", y0, x0, y1, x1);
uint sum = 0;
for (int y = y0; y <= y1; y++) {
termline * line = term.lines[y];
for (int x = x0; x <= x1; x++) {
//printf("add %d:%d\n", y, x);
if (line->chars[x].chr == UCSWIDE) {
}
else {
sum += line->chars[x].chr; // xterm default would mask & 0xFF
cattrflags attr = line->chars[x].attr.attr;
if (attr & ATTR_UNDER)
sum += 0x10;
else if (attr & ATTR_REVERSE)
sum += 0x20;
else if (attr & ATTR_BLINK)
sum += 0x40;
else if (attr & ATTR_BOLD)
sum += 0x80;
int xc = x;
while (line->chars[xc].cc_next) {
xc += line->chars[xc].cc_next;
sum += line->chars[xc].chr & 0xFF;
}
}
}
}
return sum;
}
static void
do_linefeed(void)
{
term_cursor *curs = &term.curs;
if (curs->y == marg_y)
term_do_scroll(term.marg_top, term.marg_bot, 1, true);
else if (curs->y < bot_y - 1)
curs->y++;
}
static void
wrapparabidi(ushort parabidi, termline * line, int y)
{
line->lattr = (line->lattr & ~LATTR_BIDIMASK) | parabidi | LATTR_WRAPCONTD;
#ifdef determine_parabidi_during_output
if (parabidi & (LATTR_BIDISEL | LATTR_AUTOSEL))
return;
// if direction autodetection pending:
// from current line, extend backward and forward to adjust
// "paragraph" bidi attributes (esp. direction) to wrapped lines
termline * paraline = line;
int paray = y;
while ((paraline->lattr & LATTR_WRAPCONTD) && paray > -sblines()) {
paraline = fetch_line(--paray);
paraline->lattr = (paraline->lattr & ~LATTR_BIDIMASK) | parabidi;
release_line(paraline);
}
paraline = line;
paray = y;
while ((paraline->lattr & LATTR_WRAPPED) && paray < term.rows) {
paraline = fetch_line(++paray);
paraline->lattr = (paraline->lattr & ~LATTR_BIDIMASK) | parabidi;
release_line(paraline);
}
#else
(void)y;
#endif
}
static termline *
do_wrap(termline * line, ushort lattr)
{
term_cursor * curs = &term.curs;
line->lattr |= lattr;
line->wrappos = curs->x;
ushort parabidi = getparabidi(line);
do_linefeed();
curs->x = term.marg_left;
curs->wrapnext = false;
line = term.lines[curs->y];
wrapparabidi(parabidi, line, curs->y);
return line;
}
static void
write_bell(void)
{
if (cfg.bell_flash)
term_schedule_vbell(false, 0);
win_bell(&cfg);
}
static void
write_backspace(void)
{
term_cursor *curs = &term.curs;
if (curs->x == term.marg_left && curs->y == term.marg_top
&& term.rev_wrap && !cfg.old_wrapmodes
)
{
curs->y = term.marg_bot;
curs->x = term.marg_right;
}
else if (curs->x == 0 && (curs->y == term.marg_top || !term.autowrap
|| (!cfg.old_wrapmodes && !term.rev_wrap)))
/* skip */;
else if (curs->x == term.marg_left && curs->y > term.marg_top && !term.st_active) {
curs->y--;
curs->x = term.marg_right;
}
else if (curs->wrapnext) {
curs->wrapnext = false;
if (!term.rev_wrap && !cfg.old_wrapmodes)
curs->x--;
}
else if (curs->x > 0 && curs->x != term.marg_left)
curs->x--;
}
static void
write_tab(void)
{
term_cursor *curs = &term.curs;
if (cfg.wrap_tab && curs->wrapnext && term.autowrap) {
termline * line = term.lines[curs->y];
(void)do_wrap(line, LATTR_WRAPPED);
}
int last = -1;
do {
if (curs->x == term.marg_right)
break;
last = curs->x;
if (term.lines[curs->y]->chars[last].chr == ' '
&& (term.lines[curs->y]->chars[last].attr.attr & TATTR_CLEAR)
)
term.lines[curs->y]->chars[last].attr.attr |= ATTR_DIM;
curs->x++;
} while (curs->x < term.cols - 1 && !term.tabs[curs->x]);
if (last >= 0
&& term.lines[curs->y]->chars[last].chr == ' '
&& (term.lines[curs->y]->chars[last].attr.attr & TATTR_CLEAR)
)
term.lines[curs->y]->chars[last].attr.attr |= ATTR_BOLD;
if ((term.lines[curs->y]->lattr & LATTR_MODE) != LATTR_NORM) {
if (curs->x >= term.cols / 2)
curs->x = term.cols / 2 - 1;
if (cfg.wrap_tab > 1 && curs->x == term.cols / 2 - 1)
curs->wrapnext = true;
}
else {
if (curs->x >= term.cols)
curs->x = term.cols - 1;
if (cfg.wrap_tab > 1 &&
(curs->x == term.cols - 1 || curs->x == term.marg_right))
curs->wrapnext = true;
}
}
static void
write_return(void)
{
term.curs.wrapnext = false;
if (term.curs.x < term.marg_left)
term.curs.x = 0;
else
term.curs.x = term.marg_left;
enable_progress();
}
static void
write_linefeed(void)
{
term_cursor *curs = &term.curs;
if (curs->x < term.marg_left || curs->x > term.marg_right)
return;
clear_wrapcontd(term.lines[curs->y], curs->y);
do_linefeed();
curs->wrapnext = false;
}
static void
write_primary_da(void)
{
string primary_da = primary_da4;
char * vt = strstr(cfg.term, "vt");
if (vt) {
unsigned int ver;
if (sscanf(vt + 2, "%u", &ver) == 1) {
if (ver >= 500)
primary_da = primary_da5;
else if (ver >= 400)
primary_da = primary_da4;
else if (ver >= 300)
primary_da = primary_da3;
else if (ver >= 200)
primary_da = primary_da2;
else
primary_da = primary_da1;
}
}
child_write(primary_da, strlen(primary_da));
}
static wchar last_high = 0;
static wchar last_char = 0;
static int last_width = 0;
cattr last_attr = {.attr = ATTR_DEFAULT,
.truefg = 0, .truebg = 0, .ulcolr = (colour)-1};
void
write_char(wchar c, int width)
{
//if (kb_trace) printf("[%ld] write_char 'q'\n", mtime());
if (tek_mode) {
tek_write(c, width);
return;
}
if (!c)
return;
term_cursor * curs = &term.curs;
termline * line = term.lines[curs->y];
// support non-BMP for the REP function;
// this is a hack, it would be cleaner to fold the term_write block
// switch (term.state) when NORMAL:
// and repeat that
if (width == -1) { // low surrogate
last_high = last_char;
}
else {
last_high = 0;
last_width = width;
}
last_char = c;
last_attr = curs->attr;
void put_char(wchar c)
{
if (term.ring_enabled && curs->x == term.marg_right + 1 - 8) {
win_margin_bell(&cfg);
term.ring_enabled = false;
}
clear_cc(line, curs->x);
line->chars[curs->x].chr = c;
line->chars[curs->x].attr = curs->attr;
#ifdef insufficient_approach
#warning this does not help when scrolling via rectangular copy
if (term.lrmargmode)
line->lattr &= ~LATTR_MODE;
#endif
if (term.curs.rewrap_on_resize)
line->lattr |= LATTR_REWRAP;
else
line->lattr &= ~LATTR_REWRAP;
if (!(line->lattr & LATTR_WRAPCONTD))
line->lattr = (line->lattr & ~LATTR_BIDIMASK) | curs->bidimode;
//TODO: if changed, propagate mode onto paragraph
if (cfg.ligatures_support)
term_invalidate(0, curs->y, curs->x, curs->y);
}
if (curs->wrapnext && term.autowrap && width > 0) {
line = do_wrap(line, LATTR_WRAPPED);
}
bool overstrike = false;
if (curs->attr.attr & ATTR_OVERSTRIKE) {
width = 0;
overstrike = true;
curs->wrapnext = false;
}
bool single_width = false;
// adjust to explicit width attribute; not for combinings and low surrogates
if (curs->width && width > 0) {
//if ((c & 0xFFF) == 0x153) printf("%llX %d\n", curs->attr.attr, width);
if (curs->width == 1) {
if (!(width < 2 || (cs_ambig_wide && is_ambig(c))))
curs->attr.attr |= TATTR_CLEAR | TATTR_NARROW;
width = 1;
}
else if (curs->width == 11) {
if (width > 1) {
if (!(cs_ambig_wide && is_ambig(c))) {
single_width = true;
curs->attr.attr |= TATTR_SINGLE;
}
width = 1;
}
}
else if (curs->width == 2) {
if (width < 2) {
curs->attr.attr |= TATTR_EXPAND;
width = 2;
}
}
#ifdef support_triple_width
else if (curs->width == 3) {
if (width < 2 || (cs_ambig_wide && is_ambig(c)))
curs->attr.attr |= TATTR_EXPAND;
#define TATTR_TRIPLE 0x0080000000000000u
curs->attr.attr |= TATTR_TRIPLE;
width = 3;
}
#endif
}
#ifdef enforce_ambiguous_narrow_here
// enforce ambiguous-narrow as configured or for WSL;
// this could be done here but is now sufficiently achieved in charset.c
if (cs_ambig_narrow && width > 1 && is_ambig(c))
width = 1;
#endif
if (cfg.charwidth >= 10 || cs_single_forced) {
if (width > 1) {
single_width = true;
width = 1;
}
else if (is_wide(c) || (cs_ambig_wide && is_ambig(c))) {
single_width = true;
}
}
if (term.insert && width > 0)
insert_char(width);
switch (width) {
when 1: // Normal character.
term_check_boundary(curs->x, curs->y);
term_check_boundary(curs->x + 1, curs->y);
put_char(c);
if (single_width)
line->chars[curs->x].attr.attr |= TATTR_SINGLE;
when 2 or 3: // Double-width char (Triple-width was an experimental option).
/*
* If we're about to display a double-width character
* starting in the rightmost column,
* then we do something special instead.
* We must print a space in the last column of the screen, then wrap;
* and we also set LATTR_WRAPPED2 which instructs subsequent
* cut-and-pasting not only to splice this line to the one after it,
* but to ignore the space in the last character position as well.
* (Because what was actually output to the terminal was presumably
* just a sequence of CJK characters, and we don't want a space to be
* pasted in the middle of those just because they had the misfortune
* to start in the wrong parity column. xterm concurs.)
*/
term_check_boundary(curs->x, curs->y);
term_check_boundary(curs->x + width, curs->y);
if (curs->x == term.marg_right || curs->x == term.cols - 1