-
Notifications
You must be signed in to change notification settings - Fork 138
/
vterm-module.c
1550 lines (1365 loc) · 50.2 KB
/
vterm-module.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
#include "vterm-module.h"
#include "elisp.h"
#include "utf8.h"
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <vterm.h>
static LineInfo *alloc_lineinfo() {
LineInfo *info = malloc(sizeof(LineInfo));
info->directory = NULL;
info->prompt_col = -1;
return info;
}
void free_lineinfo(LineInfo *line) {
if (line == NULL) {
return;
}
if (line->directory != NULL) {
free(line->directory);
line->directory = NULL;
}
free(line);
}
static int term_sb_push(int cols, const VTermScreenCell *cells, void *data) {
Term *term = (Term *)data;
if (!term->sb_size) {
return 0;
}
// copy vterm cells into sb_buffer
size_t c = (size_t)cols;
ScrollbackLine *sbrow = NULL;
if (term->sb_current == term->sb_size) {
if (term->sb_buffer[term->sb_current - 1]->cols == c) {
// Recycle old row if it's the right size
sbrow = term->sb_buffer[term->sb_current - 1];
} else {
if (term->sb_buffer[term->sb_current - 1]->info != NULL) {
free_lineinfo(term->sb_buffer[term->sb_current - 1]->info);
term->sb_buffer[term->sb_current - 1]->info = NULL;
}
free(term->sb_buffer[term->sb_current - 1]);
}
// Make room at the start by shifting to the right.
memmove(term->sb_buffer + 1, term->sb_buffer,
sizeof(term->sb_buffer[0]) * (term->sb_current - 1));
} else if (term->sb_current > 0) {
// Make room at the start by shifting to the right.
memmove(term->sb_buffer + 1, term->sb_buffer,
sizeof(term->sb_buffer[0]) * term->sb_current);
}
if (!sbrow) {
sbrow = malloc(sizeof(ScrollbackLine) + c * sizeof(sbrow->cells[0]));
sbrow->cols = c;
sbrow->info = NULL;
}
if (sbrow->info != NULL) {
free_lineinfo(sbrow->info);
}
sbrow->info = term->lines[0];
memmove(term->lines, term->lines + 1,
sizeof(term->lines[0]) * (term->lines_len - 1));
if (term->resizing) {
/* pushed by window height decr */
if (term->lines[term->lines_len - 1] != NULL) {
/* do not need free here ,it is reused ,we just need set null */
term->lines[term->lines_len - 1] = NULL;
}
term->lines_len--;
} else {
LineInfo *lastline = term->lines[term->lines_len - 1];
if (lastline != NULL) {
LineInfo *line = alloc_lineinfo();
if (lastline->directory != NULL) {
line->directory = malloc(1 + strlen(lastline->directory));
strcpy(line->directory, lastline->directory);
}
term->lines[term->lines_len - 1] = line;
}
}
// New row is added at the start of the storage buffer.
term->sb_buffer[0] = sbrow;
if (term->sb_current < term->sb_size) {
term->sb_current++;
}
if (term->sb_pending < term->sb_size) {
term->sb_pending++;
/* when window height decreased */
if (term->height_resize < 0 &&
term->sb_pending_by_height_decr < -term->height_resize) {
term->sb_pending_by_height_decr++;
}
}
memcpy(sbrow->cells, cells, c * sizeof(cells[0]));
return 1;
}
/// Scrollback pop handler (from pangoterm).
///
/// @param cols
/// @param cells VTerm state to update.
/// @param data Term
static int term_sb_pop(int cols, VTermScreenCell *cells, void *data) {
Term *term = (Term *)data;
if (!term->sb_current) {
return 0;
}
if (term->sb_pending) {
term->sb_pending--;
}
ScrollbackLine *sbrow = term->sb_buffer[0];
term->sb_current--;
// Forget the "popped" row by shifting the rest onto it.
memmove(term->sb_buffer, term->sb_buffer + 1,
sizeof(term->sb_buffer[0]) * (term->sb_current));
size_t cols_to_copy = (size_t)cols;
if (cols_to_copy > sbrow->cols) {
cols_to_copy = sbrow->cols;
}
// copy to vterm state
memcpy(cells, sbrow->cells, sizeof(cells[0]) * cols_to_copy);
size_t col;
for (col = cols_to_copy; col < (size_t)cols; col++) {
cells[col].chars[0] = 0;
cells[col].width = 1;
}
LineInfo **lines = malloc(sizeof(LineInfo *) * (term->lines_len + 1));
memmove(lines + 1, term->lines, sizeof(term->lines[0]) * term->lines_len);
lines[0] = sbrow->info;
free(sbrow);
term->lines_len += 1;
free(term->lines);
term->lines = lines;
return 1;
}
static int term_sb_clear(void *data) {
Term *term = (Term *)data;
if (term->sb_clear_pending) {
// Another scrollback clear is already pending, so skip this one.
return 0;
}
for (int i = 0; i < term->sb_current; i++) {
if (term->sb_buffer[i]->info != NULL) {
free_lineinfo(term->sb_buffer[i]->info);
term->sb_buffer[i]->info = NULL;
}
free(term->sb_buffer[i]);
}
free(term->sb_buffer);
term->sb_buffer = malloc(sizeof(ScrollbackLine *) * term->sb_size);
term->sb_clear_pending = true;
term->sb_current = 0;
term->sb_pending = 0;
term->sb_pending_by_height_decr = 0;
invalidate_terminal(term, -1, -1);
return 0;
}
static int row_to_linenr(Term *term, int row) {
return row != INT_MAX ? row + (int)term->sb_current + 1 : INT_MAX;
}
static int linenr_to_row(Term *term, int linenr) {
return linenr - (int)term->sb_current - 1;
}
static void fetch_cell(Term *term, int row, int col, VTermScreenCell *cell) {
if (row < 0) {
ScrollbackLine *sbrow = term->sb_buffer[-row - 1];
if ((size_t)col < sbrow->cols) {
*cell = sbrow->cells[col];
} else {
// fill the pointer with an empty cell
VTermColor fg, bg;
VTermState *state = vterm_obtain_state(term->vt);
vterm_state_get_default_colors(state, &fg, &bg);
*cell = (VTermScreenCell){.chars = {0}, .width = 1, .bg = bg};
}
} else {
vterm_screen_get_cell(term->vts, (VTermPos){.row = row, .col = col}, cell);
}
}
static char *get_row_directory(Term *term, int row) {
if (row < 0) {
ScrollbackLine *sbrow = term->sb_buffer[-row - 1];
return sbrow->info->directory;
/* return term->dirs[0]; */
} else {
LineInfo *line = term->lines[row];
return line ? line->directory : NULL;
}
}
static LineInfo *get_lineinfo(Term *term, int row) {
if (row < 0) {
ScrollbackLine *sbrow = term->sb_buffer[-row - 1];
return sbrow->info;
/* return term->dirs[0]; */
} else {
return term->lines[row];
}
}
static bool is_eol(Term *term, int end_col, int row, int col) {
/* This cell is EOL if this and every cell to the right is black */
if (row >= 0) {
VTermPos pos = {.row = row, .col = col};
return vterm_screen_is_eol(term->vts, pos);
}
ScrollbackLine *sbrow = term->sb_buffer[-row - 1];
int c;
for (c = col; c < end_col && c < sbrow->cols;) {
if (sbrow->cells[c].chars[0]) {
return 0;
}
c += sbrow->cells[c].width;
}
return 1;
}
static int is_end_of_prompt(Term *term, int end_col, int row, int col) {
LineInfo *info = get_lineinfo(term, row);
if (info == NULL) {
return 0;
}
if (info->prompt_col < 0) {
return 0;
}
if (info->prompt_col == col) {
return 1;
}
if (is_eol(term, end_col, row, col) && info->prompt_col >= col) {
return 1;
}
return 0;
}
static void goto_col(Term *term, emacs_env *env, int row, int end_col) {
int col = 0;
size_t offset = 0;
size_t beyond_eol = 0;
int height;
int width;
vterm_get_size(term->vt, &height, &width);
while (col < end_col) {
VTermScreenCell cell;
fetch_cell(term, row, col, &cell);
if (cell.chars[0]) {
if (cell.width > 1) {
offset += cell.width - 1;
}
} else {
if (is_eol(term, term->width, row, col)) {
offset += cell.width;
beyond_eol += cell.width;
}
}
col += cell.width;
}
forward_char(env, env->make_integer(env, end_col - offset));
emacs_value space = env->make_string(env, " ", 1);
for (int i = 0; i < beyond_eol; i += 1)
insert(env, space);
}
static void refresh_lines(Term *term, emacs_env *env, int start_row,
int end_row, int end_col) {
if (end_row < start_row) {
return;
}
int i, j;
#define PUSH_BUFFER(c) \
do { \
if (length == capacity) { \
capacity += end_col * 4; \
buffer = realloc(buffer, capacity * sizeof(char)); \
} \
buffer[length] = (c); \
length++; \
} while (0)
int capacity = ((end_row - start_row + 1) * end_col) * 4;
int length = 0;
char *buffer = malloc(capacity * sizeof(char));
VTermScreenCell cell;
VTermScreenCell lastCell;
fetch_cell(term, start_row, 0, &lastCell);
for (i = start_row; i < end_row; i++) {
int newline = 0;
int isprompt = 0;
for (j = 0; j < end_col; j++) {
fetch_cell(term, i, j, &cell);
if (isprompt && length > 0) {
emacs_value text = render_text(env, term, buffer, length, &lastCell);
insert(env, render_prompt(env, text));
length = 0;
}
isprompt = is_end_of_prompt(term, end_col, i, j);
if (isprompt && length > 0) {
insert(env, render_text(env, term, buffer, length, &lastCell));
length = 0;
}
if (!compare_cells(&cell, &lastCell)) {
emacs_value text = render_text(env, term, buffer, length, &lastCell);
insert(env, text);
length = 0;
}
lastCell = cell;
if (cell.chars[0] == 0) {
if (is_eol(term, end_col, i, j)) {
/* This cell is EOL if this and every cell to the right is black */
PUSH_BUFFER('\n');
newline = 1;
break;
}
PUSH_BUFFER(' ');
} else {
for (int k = 0; k < VTERM_MAX_CHARS_PER_CELL && cell.chars[k]; ++k) {
unsigned char bytes[4];
size_t count = codepoint_to_utf8(cell.chars[k], bytes);
for (int l = 0; l < count; l++) {
PUSH_BUFFER(bytes[l]);
}
}
}
if (cell.width > 1) {
int w = cell.width - 1;
j = j + w;
}
}
if (isprompt && length > 0) {
emacs_value text = render_text(env, term, buffer, length, &lastCell);
insert(env, render_prompt(env, text));
length = 0;
isprompt = 0;
}
if (!newline) {
emacs_value text = render_text(env, term, buffer, length, &lastCell);
insert(env, text);
length = 0;
text = render_fake_newline(env, term);
insert(env, text);
}
}
emacs_value text = render_text(env, term, buffer, length, &lastCell);
insert(env, text);
#undef PUSH_BUFFER
free(buffer);
return;
}
// Refresh the screen (visible part of the buffer when the terminal is
// focused) of a invalidated terminal
static void refresh_screen(Term *term, emacs_env *env) {
// Term height may have decreased before `invalid_end` reflects it.
term->invalid_end = MIN(term->invalid_end, term->height);
if (term->invalid_end >= term->invalid_start) {
int startrow = -(term->height - term->invalid_start - term->linenum_added);
/* startrow is negative,so we backward -startrow lines from end of buffer
then delete lines there.
*/
goto_line(env, startrow);
delete_lines(env, startrow, term->invalid_end - term->invalid_start, true);
refresh_lines(term, env, term->invalid_start, term->invalid_end,
term->width);
/* term->linenum_added is lines added by window height increased */
term->linenum += term->linenum_added;
term->linenum_added = 0;
}
term->invalid_start = INT_MAX;
term->invalid_end = -1;
}
static int term_resize(int rows, int cols, void *user_data) {
/* can not use invalidate_terminal here */
/* when the window height decreased, */
/* the value of term->invalid_end can't bigger than window height */
Term *term = (Term *)user_data;
term->invalid_start = 0;
term->invalid_end = rows;
/* if rows=term->lines_len, that means term_sb_pop already resize term->lines
*/
/* if rows<term->lines_len, term_sb_push would resize term->lines there */
/* we noly need to take care of rows>term->height */
if (rows > term->height) {
if (rows > term->lines_len) {
LineInfo **infos = term->lines;
term->lines = malloc(sizeof(LineInfo *) * rows);
memmove(term->lines, infos, sizeof(infos[0]) * term->lines_len);
LineInfo *lastline = term->lines[term->lines_len - 1];
for (int i = term->lines_len; i < rows; i++) {
if (lastline != NULL) {
LineInfo *line = alloc_lineinfo();
if (lastline->directory != NULL) {
line->directory =
malloc(1 + strlen(term->lines[term->lines_len - 1]->directory));
strcpy(line->directory,
term->lines[term->lines_len - 1]->directory);
}
term->lines[i] = line;
} else {
term->lines[i] = NULL;
}
}
term->lines_len = rows;
free(infos);
}
}
term->width = cols;
term->height = rows;
invalidate_terminal(term, -1, -1);
term->resizing = false;
return 1;
}
// Refresh the scrollback of an invalidated terminal.
static void refresh_scrollback(Term *term, emacs_env *env) {
int max_line_count = (int)term->sb_current + term->height;
int del_cnt = 0;
if (term->sb_clear_pending) {
del_cnt = term->linenum - term->height;
if (del_cnt > 0) {
delete_lines(env, 1, del_cnt, true);
term->linenum -= del_cnt;
}
term->sb_clear_pending = false;
}
if (term->sb_pending > 0) {
// This means that either the window height has decreased or the screen
// became full and libvterm had to push all rows up. Convert the first
// pending scrollback row into a string and append it just above the visible
// section of the buffer
del_cnt = term->linenum - term->height - (int)term->sb_size +
term->sb_pending - term->sb_pending_by_height_decr;
if (del_cnt > 0) {
delete_lines(env, 1, del_cnt, true);
term->linenum -= del_cnt;
}
term->linenum += term->sb_pending;
del_cnt = term->linenum - max_line_count; /* extra lines at the bottom */
/* buf_index is negative,so we move to end of buffer,then backward
-buf_index lines. goto lines backward is effectively when
vterm-max-scrollback is a large number.
*/
int buf_index = -(term->height + del_cnt);
goto_line(env, buf_index);
refresh_lines(term, env, -term->sb_pending, 0, term->width);
term->sb_pending = 0;
}
// Remove extra lines at the bottom
del_cnt = term->linenum - max_line_count;
if (del_cnt > 0) {
term->linenum -= del_cnt;
/* -del_cnt is negative,so we delete_lines from end of buffer.
this line means: delete del_cnt count of lines at end of buffer.
*/
delete_lines(env, -del_cnt, del_cnt, true);
}
term->sb_pending_by_height_decr = 0;
term->height_resize = 0;
}
static void adjust_topline(Term *term, emacs_env *env) {
VTermState *state = vterm_obtain_state(term->vt);
VTermPos pos;
vterm_state_get_cursorpos(state, &pos);
/* pos.row-term->height is negative,so we backward term->height-pos.row
* lines from end of buffer
*/
goto_line(env, pos.row - term->height);
goto_col(term, env, pos.row, pos.col);
emacs_value windows = get_buffer_window_list(env);
emacs_value swindow = selected_window(env);
int winnum = env->extract_integer(env, length(env, windows));
for (int i = 0; i < winnum; i++) {
emacs_value window = nth(env, i, windows);
if (eq(env, window, swindow)) {
int win_body_height =
env->extract_integer(env, window_body_height(env, window));
/* recenter:If ARG is negative, it counts up from the bottom of the
* window. (ARG should be less than the height of the window ) */
if (term->height - pos.row <= win_body_height) {
recenter(env, env->make_integer(env, pos.row - term->height));
} else {
recenter(env, env->make_integer(env, pos.row));
}
} else {
if (env->is_not_nil(env, window)) {
set_window_point(env, window, point(env));
}
}
}
}
static void invalidate_terminal(Term *term, int start_row, int end_row) {
if (start_row != -1 && end_row != -1) {
term->invalid_start = MIN(term->invalid_start, start_row);
term->invalid_end = MAX(term->invalid_end, end_row);
}
term->is_invalidated = true;
}
static int term_damage(VTermRect rect, void *data) {
invalidate_terminal(data, rect.start_row, rect.end_row);
return 1;
}
static int term_moverect(VTermRect dest, VTermRect src, void *data) {
invalidate_terminal(data, MIN(dest.start_row, src.start_row),
MAX(dest.end_row, src.end_row));
return 1;
}
static int term_movecursor(VTermPos new, VTermPos old, int visible,
void *data) {
Term *term = data;
term->cursor.row = new.row;
term->cursor.col = new.col;
invalidate_terminal(term, old.row, old.row + 1);
invalidate_terminal(term, new.row, new.row + 1);
return 1;
}
static void term_redraw_cursor(Term *term, emacs_env *env) {
if (term->cursor.cursor_blink_changed) {
term->cursor.cursor_blink_changed = false;
set_cursor_blink(env, term->cursor.cursor_blink);
}
if (term->cursor.cursor_type_changed) {
term->cursor.cursor_type_changed = false;
if (!term->cursor.cursor_visible) {
set_cursor_type(env, Qnil);
return;
}
switch (term->cursor.cursor_type) {
case VTERM_PROP_CURSORSHAPE_BLOCK:
set_cursor_type(env, Qbox);
break;
case VTERM_PROP_CURSORSHAPE_UNDERLINE:
set_cursor_type(env, Qhbar);
break;
case VTERM_PROP_CURSORSHAPE_BAR_LEFT:
set_cursor_type(env, Qbar);
break;
default:
set_cursor_type(env, Qt);
break;
}
}
}
static void term_redraw(Term *term, emacs_env *env) {
term_redraw_cursor(term, env);
if (term->is_invalidated) {
int oldlinenum = term->linenum;
refresh_scrollback(term, env);
refresh_screen(term, env);
term->linenum_added = term->linenum - oldlinenum;
adjust_topline(term, env);
term->linenum_added = 0;
}
if (term->title_changed) {
set_title(env, env->make_string(env, term->title, strlen(term->title)));
term->title_changed = false;
}
if (term->directory_changed) {
set_directory(
env, env->make_string(env, term->directory, strlen(term->directory)));
term->directory_changed = false;
}
while (term->elisp_code_first) {
ElispCodeListNode *node = term->elisp_code_first;
term->elisp_code_first = node->next;
emacs_value elisp_code = env->make_string(env, node->code, node->code_len);
vterm_eval(env, elisp_code);
free(node->code);
free(node);
}
term->elisp_code_p_insert = &term->elisp_code_first;
if (term->selection_data) {
emacs_value selection_mask = env->make_integer(env, term->selection_mask);
emacs_value selection_data = env->make_string(env, term->selection_data,
strlen(term->selection_data));
vterm_set_selection(env, selection_mask, selection_data);
free(term->selection_data);
term->selection_data = NULL;
term->selection_mask = 0;
}
term->is_invalidated = false;
}
static VTermScreenCallbacks vterm_screen_callbacks = {
.damage = term_damage,
.moverect = term_moverect,
.movecursor = term_movecursor,
.settermprop = term_settermprop,
.resize = term_resize,
.sb_pushline = term_sb_push,
.sb_popline = term_sb_pop,
#if !defined(VTermSBClearNotExists)
.sb_clear = term_sb_clear,
#endif
};
static bool compare_cells(VTermScreenCell *a, VTermScreenCell *b) {
bool equal = true;
equal = equal && vterm_color_is_equal(&a->fg, &b->fg);
equal = equal && vterm_color_is_equal(&a->bg, &b->bg);
equal = equal && (a->attrs.bold == b->attrs.bold);
equal = equal && (a->attrs.underline == b->attrs.underline);
equal = equal && (a->attrs.italic == b->attrs.italic);
equal = equal && (a->attrs.reverse == b->attrs.reverse);
equal = equal && (a->attrs.strike == b->attrs.strike);
return equal;
}
static bool is_key(unsigned char *key, size_t len, char *key_description) {
return (len == strlen(key_description) &&
memcmp(key, key_description, len) == 0);
}
/* str1=concat(str1,str2,str2_len,true); */
/* str1 can be NULL */
static char *concat(char *str1, const char *str2, size_t str2_len,
bool free_str1) {
if (str1 == NULL) {
str1 = malloc(str2_len + 1);
memcpy(str1, str2, str2_len);
str1[str2_len] = '\0';
return str1;
}
size_t str1_len = strlen(str1);
char *buf = malloc(str1_len + str2_len + 1);
memcpy(buf, str1, str1_len);
memcpy(&buf[str1_len], str2, str2_len);
buf[str1_len + str2_len] = '\0';
if (free_str1) {
free(str1);
}
return buf;
}
static void term_set_title(Term *term, const char *title, size_t len,
bool initial, bool final) {
if (term->title && initial) {
free(term->title);
term->title = NULL;
term->title_changed = false;
}
term->title = concat(term->title, title, len, true);
if (final) {
term->title_changed = true;
}
return;
}
static int term_settermprop(VTermProp prop, VTermValue *val, void *user_data) {
Term *term = (Term *)user_data;
switch (prop) {
case VTERM_PROP_CURSORVISIBLE:
invalidate_terminal(term, term->cursor.row, term->cursor.row + 1);
term->cursor.cursor_visible = val->boolean;
term->cursor.cursor_type_changed = true;
break;
case VTERM_PROP_CURSORBLINK:
if (term->ignore_blink_cursor)
break;
invalidate_terminal(term, term->cursor.row, term->cursor.row + 1);
term->cursor.cursor_blink = val->boolean;
term->cursor.cursor_blink_changed = true;
break;
case VTERM_PROP_CURSORSHAPE:
invalidate_terminal(term, term->cursor.row, term->cursor.row + 1);
term->cursor.cursor_type = val->number;
term->cursor.cursor_type_changed = true;
break;
case VTERM_PROP_TITLE:
#ifdef VTermStringFragmentNotExists
term_set_title(term, val->string, strlen(val->string), true, true);
#else
term_set_title(term, val->string.str, val->string.len, val->string.initial,
val->string.final);
#endif
break;
case VTERM_PROP_ALTSCREEN:
invalidate_terminal(term, 0, term->height);
break;
default:
return 0;
}
return 1;
}
static emacs_value render_text(emacs_env *env, Term *term, char *buffer,
int len, VTermScreenCell *cell) {
emacs_value text;
if (len == 0) {
text = env->make_string(env, "", 0);
return text;
} else {
text = env->make_string(env, buffer, len);
}
emacs_value fg = cell_rgb_color(env, term, cell, true);
emacs_value bg = cell_rgb_color(env, term, cell, false);
/* With vterm-disable-bold-font, vterm-disable-underline,
* vterm-disable-inverse-video, users can disable some text properties.
* Here, we check whether the text would require adding such properties.
* In case it does, and the user does not disable the attribute, we later
* append the property to the list props. If the text does not require
* such property, or the user disable it, we set the variable to nil.
* Properties that are marked as nil are not added to the text. */
emacs_value bold =
cell->attrs.bold && !term->disable_bold_font ? Qbold : Qnil;
emacs_value underline =
cell->attrs.underline && !term->disable_underline ? Qt : Qnil;
emacs_value italic = cell->attrs.italic ? Qitalic : Qnil;
emacs_value reverse =
cell->attrs.reverse && !term->disable_inverse_video ? Qt : Qnil;
emacs_value strike = cell->attrs.strike ? Qt : Qnil;
// TODO: Blink, font, dwl, dhl is missing
int emacs_major_version =
env->extract_integer(env, symbol_value(env, Qemacs_major_version));
emacs_value properties;
emacs_value props[64];
int props_len = 0;
if (env->is_not_nil(env, fg))
props[props_len++] = Qforeground, props[props_len++] = fg;
if (env->is_not_nil(env, bg))
props[props_len++] = Qbackground, props[props_len++] = bg;
if (bold != Qnil)
props[props_len++] = Qweight, props[props_len++] = bold;
if (underline != Qnil)
props[props_len++] = Qunderline, props[props_len++] = underline;
if (italic != Qnil)
props[props_len++] = Qslant, props[props_len++] = italic;
if (reverse != Qnil)
props[props_len++] = Qreverse, props[props_len++] = reverse;
if (strike != Qnil)
props[props_len++] = Qstrike, props[props_len++] = strike;
if (emacs_major_version >= 27)
props[props_len++] = Qextend, props[props_len++] = Qt;
properties = list(env, props, props_len);
if (props_len)
put_text_property(env, text, Qface, properties);
return text;
}
static emacs_value render_prompt(emacs_env *env, emacs_value text) {
emacs_value properties;
properties =
list(env, (emacs_value[]){Qvterm_prompt, Qt, Qrear_nonsticky, Qt}, 4);
add_text_properties(env, text, properties);
return text;
}
static emacs_value render_fake_newline(emacs_env *env, Term *term) {
emacs_value text;
text = env->make_string(env, "\n", 1);
emacs_value properties;
properties =
list(env, (emacs_value[]){Qvterm_line_wrap, Qt, Qrear_nonsticky, Qt}, 4);
add_text_properties(env, text, properties);
return text;
}
static emacs_value cell_rgb_color(emacs_env *env, Term *term,
VTermScreenCell *cell, bool is_foreground) {
VTermColor *color = is_foreground ? &cell->fg : &cell->bg;
int props_len = 0;
emacs_value props[3];
if (is_foreground)
props[props_len++] = Qforeground;
if (cell->attrs.underline)
props[props_len++] = Qunderline;
if (cell->attrs.reverse)
props[props_len++] = Qreverse;
emacs_value args = list(env, props, props_len);
/** NOTE: -10 is used as index offset for special indexes,
* see C-h f vterm--get-color RET
*/
if (VTERM_COLOR_IS_DEFAULT_FG(color) || VTERM_COLOR_IS_DEFAULT_BG(color)) {
return vterm_get_color(env, -1, args);
}
if (VTERM_COLOR_IS_INDEXED(color)) {
if (color->indexed.idx < 16) {
return vterm_get_color(env, color->indexed.idx, args);
} else {
VTermState *state = vterm_obtain_state(term->vt);
vterm_state_get_palette_color(state, color->indexed.idx, color);
}
} else if (VTERM_COLOR_IS_RGB(color)) {
/* do nothing just use the argument color directly */
}
char buffer[8];
snprintf(buffer, 8, "#%02X%02X%02X", color->rgb.red, color->rgb.green,
color->rgb.blue);
return env->make_string(env, buffer, 7);
}
static void term_flush_output(Term *term, emacs_env *env) {
size_t len = vterm_output_get_buffer_current(term->vt);
if (len) {
char buffer[len];
len = vterm_output_read(term->vt, buffer, len);
emacs_value output = env->make_string(env, buffer, len);
env->funcall(env, Fvterm_flush_output, 1, (emacs_value[]){output});
}
}
static void term_clear_scrollback(Term *term, emacs_env *env) {
term_sb_clear(term);
vterm_screen_flush_damage(term->vts);
term_redraw(term, env);
}
static void term_process_key(Term *term, emacs_env *env, unsigned char *key,
size_t len, VTermModifier modifier) {
if (is_key(key, len, "<clear_scrollback>")) {
term_clear_scrollback(term, env);
} else if (is_key(key, len, "<start>")) {
tcflow(term->pty_fd, TCOON);
} else if (is_key(key, len, "<stop>")) {
tcflow(term->pty_fd, TCOOFF);
} else if (is_key(key, len, "<start_paste>")) {
vterm_keyboard_start_paste(term->vt);
} else if (is_key(key, len, "<end_paste>")) {
vterm_keyboard_end_paste(term->vt);
} else if (is_key(key, len, "<tab>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_TAB, modifier);
} else if (is_key(key, len, "<backtab>") ||
is_key(key, len, "<iso-lefttab>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_TAB, VTERM_MOD_SHIFT);
} else if (is_key(key, len, "<backspace>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_BACKSPACE, modifier);
} else if (is_key(key, len, "<escape>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_ESCAPE, modifier);
} else if (is_key(key, len, "<up>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_UP, modifier);
} else if (is_key(key, len, "<down>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_DOWN, modifier);
} else if (is_key(key, len, "<left>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_LEFT, modifier);
} else if (is_key(key, len, "<right>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_RIGHT, modifier);
} else if (is_key(key, len, "<insert>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_INS, modifier);
} else if (is_key(key, len, "<delete>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_DEL, modifier);
} else if (is_key(key, len, "<home>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_HOME, modifier);
} else if (is_key(key, len, "<end>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_END, modifier);
} else if (is_key(key, len, "<prior>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_PAGEUP, modifier);
} else if (is_key(key, len, "<next>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_PAGEDOWN, modifier);
} else if (is_key(key, len, "<f0>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(0), modifier);
} else if (is_key(key, len, "<f1>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(1), modifier);
} else if (is_key(key, len, "<f2>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(2), modifier);
} else if (is_key(key, len, "<f3>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(3), modifier);
} else if (is_key(key, len, "<f4>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(4), modifier);
} else if (is_key(key, len, "<f5>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(5), modifier);
} else if (is_key(key, len, "<f6>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(6), modifier);
} else if (is_key(key, len, "<f7>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(7), modifier);
} else if (is_key(key, len, "<f8>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(8), modifier);
} else if (is_key(key, len, "<f9>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(9), modifier);
} else if (is_key(key, len, "<f10>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(10), modifier);
} else if (is_key(key, len, "<f11>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(11), modifier);
} else if (is_key(key, len, "<f12>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_FUNCTION(12), modifier);
} else if (is_key(key, len, "<kp-0>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_0, modifier);
} else if (is_key(key, len, "<kp-1>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_1, modifier);
} else if (is_key(key, len, "<kp-2>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_2, modifier);
} else if (is_key(key, len, "<kp-3>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_3, modifier);
} else if (is_key(key, len, "<kp-4>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_4, modifier);
} else if (is_key(key, len, "<kp-5>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_5, modifier);
} else if (is_key(key, len, "<kp-6>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_6, modifier);
} else if (is_key(key, len, "<kp-7>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_7, modifier);
} else if (is_key(key, len, "<kp-8>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_8, modifier);
} else if (is_key(key, len, "<kp-9>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_9, modifier);
} else if (is_key(key, len, "<kp-add>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_PLUS, modifier);
} else if (is_key(key, len, "<kp-subtract>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_MINUS, modifier);
} else if (is_key(key, len, "<kp-multiply>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_MULT, modifier);
} else if (is_key(key, len, "<kp-divide>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_DIVIDE, modifier);
} else if (is_key(key, len, "<kp-equal>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_EQUAL, modifier);
} else if (is_key(key, len, "<kp-decimal>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_PERIOD, modifier);
} else if (is_key(key, len, "<kp-separator>")) {
vterm_keyboard_key(term->vt, VTERM_KEY_KP_COMMA, modifier);
} else if (is_key(key, len, "<kp-enter>")) {