-
Notifications
You must be signed in to change notification settings - Fork 72
/
vteseq.cc
9459 lines (8549 loc) · 275 KB
/
vteseq.cc
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
/*
* Copyright © 2001-2004 Red Hat, Inc.
* Copyright © 2015 David Herrmann <[email protected]>
* Copyright © 2008-2018 Christian Persch
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <search.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#if __has_include(<sys/syslimits.h>)
#include <sys/syslimits.h>
#endif
#include <glib.h>
#include <vte/vte.h>
#include "vteinternal.hh"
#include "vtegtk.hh"
#include "caps.hh"
#include "debug.h"
#define BEL_C0 "\007"
#define ST_C0 _VTE_CAP_ST
#include <algorithm>
using namespace std::literals;
enum {
VTE_XTERM_WM_RESTORE_WINDOW = 1,
VTE_XTERM_WM_MINIMIZE_WINDOW = 2,
VTE_XTERM_WM_SET_WINDOW_POSITION = 3,
VTE_XTERM_WM_SET_WINDOW_SIZE_PIXELS = 4,
VTE_XTERM_WM_RAISE_WINDOW = 5,
VTE_XTERM_WM_LOWER_WINDOW = 6,
VTE_XTERM_WM_REFRESH_WINDOW = 7,
VTE_XTERM_WM_SET_WINDOW_SIZE_CELLS = 8,
VTE_XTERM_WM_MAXIMIZE_WINDOW = 9,
VTE_XTERM_WM_FULLSCREEN_WINDOW = 10,
VTE_XTERM_WM_GET_WINDOW_STATE = 11,
VTE_XTERM_WM_GET_WINDOW_POSITION = 13,
VTE_XTERM_WM_GET_WINDOW_SIZE_PIXELS = 14,
VTE_XTERM_WM_GET_WINDOW_SIZE_CELLS = 18,
VTE_XTERM_WM_GET_SCREEN_SIZE_CELLS = 19,
VTE_XTERM_WM_GET_ICON_TITLE = 20,
VTE_XTERM_WM_GET_WINDOW_TITLE = 21,
VTE_XTERM_WM_TITLE_STACK_PUSH = 22,
VTE_XTERM_WM_TITLE_STACK_POP = 23,
};
enum {
VTE_SGR_COLOR_SPEC_RGB = 2,
VTE_SGR_COLOR_SPEC_LEGACY = 5
};
inline consteval int firmware_version() noexcept
{
return (VTE_MAJOR_VERSION * 100 + VTE_MINOR_VERSION) * 100 + VTE_MICRO_VERSION;
}
void
vte::parser::Sequence::print() const noexcept
{
#if VTE_DEBUG
auto c = m_seq != nullptr ? terminator() : 0;
char c_buf[7];
g_snprintf(c_buf, sizeof(c_buf), "%lc", c);
g_printerr("%s:%s [%s]", type_string(), command_string(),
g_unichar_isprint(c) ? c_buf : _vte_debug_sequence_to_string(c_buf, -1));
if (m_seq != nullptr && m_seq->n_args > 0) {
g_printerr("[ ");
for (unsigned int i = 0; i < m_seq->n_args; i++) {
if (i > 0)
g_print(", ");
g_printerr("%d", vte_seq_arg_value(m_seq->args[i]));
}
g_printerr(" ]");
}
if (m_seq->type == VTE_SEQ_OSC || m_seq->type == VTE_SEQ_DCS) {
char* str = string_param();
g_printerr(" \"%s\"", str);
g_free(str);
}
g_printerr("\n");
#endif
}
char const*
vte::parser::Sequence::type_string() const
{
if (G_UNLIKELY(m_seq == nullptr))
return "(nil)";
switch (type()) {
case VTE_SEQ_NONE: return "NONE";
case VTE_SEQ_IGNORE: return "IGNORE";
case VTE_SEQ_GRAPHIC: return "GRAPHIC";
case VTE_SEQ_CONTROL: return "CONTROL";
case VTE_SEQ_ESCAPE: return "ESCAPE";
case VTE_SEQ_CSI: return "CSI";
case VTE_SEQ_DCS: return "DCS";
case VTE_SEQ_OSC: return "OSC";
case VTE_SEQ_SCI: return "SCI";
case VTE_SEQ_APC: return "APC";
case VTE_SEQ_PM: return "PM";
case VTE_SEQ_SOS: return "SOS";
default:
g_assert(false);
return nullptr;
}
}
char const*
vte::parser::Sequence::command_string() const
{
if (G_UNLIKELY(m_seq == nullptr))
return "(nil)";
switch (command()) {
#define _VTE_CMD(cmd) case VTE_CMD_##cmd: return #cmd;
#define _VTE_NOP(cmd)
#include "parser-cmd.hh"
#undef _VTE_CMD
#undef _VTE_NOP
default:
static char buf[32];
snprintf(buf, sizeof(buf), "NOP OR UNKOWN(%u)", command());
return buf;
}
}
// FIXMEchpe optimise this
std::string
vte::parser::Sequence::string_utf8() const noexcept
{
std::string str;
size_t len;
auto buf = vte_seq_string_get(&m_seq->arg_str, &len);
char u[6];
for (size_t i = 0; i < len; ++i) {
auto ulen = g_unichar_to_utf8(buf[i], u);
str.append((char const*)u, ulen);
}
return str;
}
/* A couple are duplicated from vte.c, to keep them static... */
/* Check how long a string of unichars is. Slow version. */
static gsize
vte_unichar_strlen(gunichar const* c)
{
gsize i;
for (i = 0; c[i] != 0; i++) ;
return i;
}
/* Convert a wide character string to a multibyte string */
/* Simplified from glib's g_ucs4_to_utf8() to simply allocate the maximum
* length instead of walking the input twice.
*/
char*
vte::parser::Sequence::ucs4_to_utf8(gunichar const* str,
ssize_t len) const noexcept
{
if (len < 0)
len = vte_unichar_strlen(str);
auto outlen = (len * VTE_UTF8_BPC) + 1;
auto result = (char*)g_try_malloc(outlen);
if (result == nullptr)
return nullptr;
auto end = str + len;
auto p = result;
for (auto i = str; i < end; i++)
p += g_unichar_to_utf8(*i, p);
*p = '\0';
return result;
}
namespace vte {
namespace terminal {
/* Emit a "bell" signal. */
void
Terminal::emit_bell()
{
_vte_debug_print(VTE_DEBUG_SIGNALS, "Emitting `bell'.\n");
g_signal_emit(m_terminal, signals[SIGNAL_BELL], 0);
}
/* Emit a "resize-window" signal. (Grid size.) */
void
Terminal::emit_resize_window(guint columns,
guint rows)
{
_vte_debug_print(VTE_DEBUG_SIGNALS, "Emitting `resize-window'.\n");
g_signal_emit(m_terminal, signals[SIGNAL_RESIZE_WINDOW], 0, columns, rows);
}
/* Some common functions */
/* In Xterm, upon printing a character in the last column the cursor doesn't
* advance. It's special cased that printing the following letter will first
* wrap to the next row.
*
* As a rule of thumb, escape sequences that move the cursor (e.g. cursor up)
* or immediately update the visible contents (e.g. clear in line) disable
* this special mode, whereas escape sequences with no immediate visible
* effect (e.g. color change) leave this special mode on. There are
* exceptions of course (e.g. scroll up).
*
* In VTE, a different technical approach is used. The cursor is advanced to
* the invisible column on the right, but it's set back to the visible
* rightmost column whenever necessary (that is, before handling any of the
* sequences that disable the special cased mode in xterm).
*
* Similarly, if a right margin is set up and the cursor moved just beyond
* that margin due to a graphic character (as opposed to a cursor moving
* escape sequence) then set back the cursor by one column.
*
* See https://gitlab.gnome.org/GNOME/vte/-/issues/2108
* and https://gitlab.gnome.org/GNOME/vte/-/issues/2677
*/
void
Terminal::maybe_retreat_cursor()
{
m_screen->cursor.col = get_xterm_cursor_column();
m_screen->cursor_advanced_by_graphic_character = false;
}
void
Terminal::home_cursor()
{
set_cursor_coords(0, 0);
}
void
Terminal::clear_screen()
{
maybe_retreat_cursor();
auto row = get_xterm_cursor_row();
auto initial = m_screen->row_data->next();
/* Add a new screen's worth of rows. */
for (auto i = 0; i < m_row_count; i++)
ring_append(true);
/* Move the cursor and insertion delta to the first line in the
* newly-cleared area and scroll if need be. */
m_screen->insert_delta = initial;
m_screen->cursor.row = row + m_screen->insert_delta;
m_screen->cursor_advanced_by_graphic_character = false;
adjust_adjustments();
/* Redraw everything. */
invalidate_all();
/* We've modified the display. Make a note of it. */
m_text_deleted_flag = TRUE;
}
/* Clear the current line. */
void
Terminal::clear_current_line()
{
VteRowData *rowdata;
maybe_retreat_cursor();
/* If the cursor's row is covered by the ring, clear data in the row
* which corresponds to the cursor. */
if (long(m_screen->row_data->next()) > m_screen->cursor.row) {
/* Get the data for the row which the cursor points to. */
rowdata = m_screen->row_data->index_writable(m_screen->cursor.row);
g_assert(rowdata != NULL);
/* Remove it. */
_vte_row_data_shrink (rowdata, 0);
/* Add enough cells to the end of the line to fill out the row. */
_vte_row_data_fill (rowdata, &m_color_defaults, m_column_count);
set_hard_wrapped(m_screen->cursor.row);
rowdata->attr.bidi_flags = get_bidi_flags();
/* Repaint this row's paragraph (might need to extend upwards). */
invalidate_row_and_context(m_screen->cursor.row);
}
/* We've modified the display. Make a note of it. */
m_text_deleted_flag = TRUE;
}
/* Clear above the current line. */
void
Terminal::clear_above_current()
{
/* Make the line just above the writable area hard wrapped. */
if (m_screen->insert_delta > long(m_screen->row_data->delta())) {
set_hard_wrapped(m_screen->insert_delta - 1);
}
/* Clear data in all the writable rows above (excluding) the cursor's. */
for (auto i = m_screen->insert_delta; i < m_screen->cursor.row; i++) {
if (long(m_screen->row_data->next()) > i) {
/* Get the data for the row we're erasing. */
auto rowdata = m_screen->row_data->index_writable(i);
g_assert(rowdata != NULL);
/* Remove it. */
_vte_row_data_shrink (rowdata, 0);
/* Add new cells until we fill the row. */
_vte_row_data_fill (rowdata, &m_color_defaults, m_column_count);
set_hard_wrapped(i);
rowdata->attr.bidi_flags = get_bidi_flags();
}
}
/* Repaint the cleared area. No need to extend, set_hard_wrapped() took care of
* invalidating the context lines if necessary. */
invalidate_rows(m_screen->insert_delta, m_screen->cursor.row - 1);
/* We've modified the display. Make a note of it. */
m_text_deleted_flag = TRUE;
}
void
Terminal::restore_cursor()
{
restore_cursor(m_screen);
}
void
Terminal::save_cursor()
{
save_cursor(m_screen);
}
/* Switch to normal screen. */
void
Terminal::switch_normal_screen()
{
switch_screen(&m_normal_screen);
}
void
Terminal::switch_screen(VteScreen *new_screen)
{
/* if (new_screen == m_screen) return; ? */
/* The two screens use different hyperlink pools, so carrying on the idx
* wouldn't make sense and could lead to crashes.
* Ideally we'd carry the target URI itself, but I'm just lazy.
* Also, run a GC before we switch away from that screen. */
m_hyperlink_hover_idx = m_screen->row_data->get_hyperlink_at_position(-1, -1, true, NULL);
g_assert (m_hyperlink_hover_idx == 0);
m_hyperlink_hover_uri = NULL;
emit_hyperlink_hover_uri_changed(NULL); /* FIXME only emit if really changed */
m_defaults.attr.hyperlink_idx = m_screen->row_data->get_hyperlink_idx(NULL);
g_assert (m_defaults.attr.hyperlink_idx == 0);
/* cursor.row includes insert_delta, adjust accordingly */
auto cr = m_screen->cursor.row - m_screen->insert_delta;
auto cc = m_screen->cursor.col;
auto cadv = m_screen->cursor_advanced_by_graphic_character;
m_screen = new_screen;
m_screen->cursor.row = cr + m_screen->insert_delta;
m_screen->cursor.col = cc;
m_screen->cursor_advanced_by_graphic_character = cadv;
/* Make sure the ring is large enough */
ensure_row();
}
/* Switch to alternate screen. */
void
Terminal::switch_alternate_screen()
{
switch_screen(&m_alternate_screen);
}
void
Terminal::set_mode_ecma(vte::parser::Sequence const& seq,
bool set) noexcept
{
auto const n_params = seq.size();
for (unsigned int i = 0; i < n_params; i = seq.next(i)) {
auto const param = seq.collect1(i);
auto const mode = m_modes_ecma.mode_from_param(param);
_vte_debug_print(VTE_DEBUG_MODES,
"Mode %d (%s) %s\n",
param, m_modes_ecma.mode_to_cstring(mode),
set ? "set" : "reset");
if (mode < 0)
continue;
m_modes_ecma.set(mode, set);
if (mode == m_modes_ecma.eBDSM) {
_vte_debug_print(VTE_DEBUG_BIDI,
"BiDi %s mode\n",
set ? "implicit" : "explicit");
maybe_apply_bidi_attributes(VTE_BIDI_FLAG_IMPLICIT);
}
}
}
void
Terminal::update_mouse_protocol() noexcept
{
if (m_modes_private.XTERM_MOUSE_ANY_EVENT())
m_mouse_tracking_mode = MouseTrackingMode::eALL_MOTION_TRACKING;
else if (m_modes_private.XTERM_MOUSE_BUTTON_EVENT())
m_mouse_tracking_mode = MouseTrackingMode::eCELL_MOTION_TRACKING;
else if (m_modes_private.XTERM_MOUSE_VT220_HIGHLIGHT())
m_mouse_tracking_mode = MouseTrackingMode::eHILITE_TRACKING;
else if (m_modes_private.XTERM_MOUSE_VT220())
m_mouse_tracking_mode = MouseTrackingMode::eSEND_XY_ON_BUTTON;
else if (m_modes_private.XTERM_MOUSE_X10())
m_mouse_tracking_mode = MouseTrackingMode::eSEND_XY_ON_CLICK;
else
m_mouse_tracking_mode = MouseTrackingMode::eNONE;
m_mouse_smooth_scroll_x_delta = 0.0;
m_mouse_smooth_scroll_y_delta = 0.0;
/* Mouse pointer might change */
apply_mouse_cursor();
_vte_debug_print(VTE_DEBUG_MODES,
"Mouse protocol is now %d\n", (int)m_mouse_tracking_mode);
}
void
Terminal::set_mode_private(int mode,
bool set) noexcept
{
/* Pre actions */
switch (mode) {
default:
break;
}
m_modes_private.set(mode, set);
/* Post actions */
switch (mode) {
case vte::terminal::modes::Private::eDEC_132_COLUMN:
/* DECCOLM: set/reset to 132/80 columns mode, clear screen and cursor home */
// FIXMEchpe don't do clear screen if DECNCSM is set
/* FIXMEchpe!!!
* Changing this mode resets the top, bottom, left, right margins;
* clears the screen (unless DECNCSM is set); resets DECLRMM; and clears
* the status line if host-writable.
*/
if (m_modes_private.XTERM_DECCOLM()) {
emit_resize_window(set ? 132 : 80, m_row_count);
m_scrolling_region.reset();
clear_screen();
home_cursor();
}
break;
case vte::terminal::modes::Private::eDEC_REVERSE_IMAGE:
invalidate_all();
break;
case vte::terminal::modes::Private::eDEC_ORIGIN:
/* Reposition the cursor in its new home position. */
home_cursor();
break;
case vte::terminal::modes::Private::eDEC_TEXT_CURSOR:
/* No need to invalidate the cursor here, this is done
* in process_incoming().
*/
break;
case vte::terminal::modes::Private::eDECLRMM:
if (!set) {
m_scrolling_region.reset_horizontal();
}
break;
case vte::terminal::modes::Private::eXTERM_ALTBUF:
[[fallthrough]];
case vte::terminal::modes::Private::eXTERM_OPT_ALTBUF:
[[fallthrough]];
case vte::terminal::modes::Private::eXTERM_OPT_ALTBUF_SAVE_CURSOR:
if (set) {
if (mode == vte::terminal::modes::Private::eXTERM_OPT_ALTBUF_SAVE_CURSOR)
save_cursor();
switch_alternate_screen();
/* Clear the alternate screen */
if (mode == vte::terminal::modes::Private::eXTERM_OPT_ALTBUF_SAVE_CURSOR)
clear_screen();
} else {
if (mode == vte::terminal::modes::Private::eXTERM_OPT_ALTBUF &&
m_screen == &m_alternate_screen)
clear_screen();
switch_normal_screen();
if (mode == vte::terminal::modes::Private::eXTERM_OPT_ALTBUF_SAVE_CURSOR)
restore_cursor();
}
/* Reset scrollbars and repaint everything. */
queue_adjustment_value_changed(m_screen->scroll_delta);
set_scrollback_lines(m_scrollback_lines);
queue_contents_changed();
invalidate_all();
break;
case vte::terminal::modes::Private::eXTERM_SAVE_CURSOR:
if (set)
save_cursor();
else
restore_cursor();
break;
case vte::terminal::modes::Private::eXTERM_MOUSE_X10:
case vte::terminal::modes::Private::eXTERM_MOUSE_VT220:
case vte::terminal::modes::Private::eXTERM_MOUSE_VT220_HIGHLIGHT:
case vte::terminal::modes::Private::eXTERM_MOUSE_BUTTON_EVENT:
case vte::terminal::modes::Private::eXTERM_MOUSE_ANY_EVENT:
case vte::terminal::modes::Private::eXTERM_MOUSE_EXT:
case vte::terminal::modes::Private::eXTERM_MOUSE_EXT_SGR:
update_mouse_protocol();
break;
case vte::terminal::modes::Private::eXTERM_FOCUS:
if (set)
feed_focus_event_initial();
break;
case vte::terminal::modes::Private::eVTE_BIDI_BOX_MIRROR:
_vte_debug_print(VTE_DEBUG_BIDI,
"BiDi box drawing mirroring %s\n",
set ? "enabled" : "disabled");
maybe_apply_bidi_attributes(VTE_BIDI_FLAG_BOX_MIRROR);
break;
case vte::terminal::modes::Private::eVTE_BIDI_AUTO:
_vte_debug_print(VTE_DEBUG_BIDI,
"BiDi dir autodetection %s\n",
set ? "enabled" : "disabled");
maybe_apply_bidi_attributes(VTE_BIDI_FLAG_AUTO);
break;
default:
break;
}
}
void
Terminal::set_mode_private(vte::parser::Sequence const& seq,
bool set) noexcept
{
auto const n_params = seq.size();
for (unsigned int i = 0; i < n_params; i = seq.next(i)) {
auto const param = seq.collect1(i);
auto const mode = m_modes_private.mode_from_param(param);
_vte_debug_print(VTE_DEBUG_MODES,
"Private mode %d (%s) %s\n",
param, m_modes_private.mode_to_cstring(mode),
set ? "set" : "reset");
if (mode < 0)
continue;
set_mode_private(mode, set);
}
}
void
Terminal::save_mode_private(vte::parser::Sequence const& seq,
bool save) noexcept
{
auto const n_params = seq.size();
for (unsigned int i = 0; i < n_params; i = seq.next(i)) {
auto const param = seq.collect1(i);
auto const mode = m_modes_private.mode_from_param(param);
if (mode < 0) {
_vte_debug_print(VTE_DEBUG_MODES,
"Saving private mode %d (%s)\n",
param, m_modes_private.mode_to_cstring(mode));
continue;
}
if (save) {
_vte_debug_print(VTE_DEBUG_MODES,
"Saving private mode %d (%s) is %s\n",
param, m_modes_private.mode_to_cstring(mode),
m_modes_private.get(mode) ? "set" : "reset");
m_modes_private.push_saved(mode);
} else {
bool const set = m_modes_private.pop_saved(mode);
_vte_debug_print(VTE_DEBUG_MODES,
"Restoring private mode %d (%s) to %s\n",
param, m_modes_private.mode_to_cstring(mode),
set ? "set" : "reset");
set_mode_private(mode, set);
}
}
}
void
Terminal::set_character_replacement(unsigned slot)
{
g_assert(slot < G_N_ELEMENTS(m_character_replacements));
m_character_replacement = &m_character_replacements[slot];
}
/* Clear from the cursor position (inclusive!) to the beginning of the line. */
void
Terminal::clear_to_bol()
{
maybe_retreat_cursor();
/* Get the data for the row which the cursor points to. */
auto rowdata = ensure_row();
/* Clean up Tab/CJK fragments. */
cleanup_fragments(0, m_screen->cursor.col + 1);
/* Clear the data up to the current column with the default
* attributes. If there is no such character cell, we need
* to add one. */
vte::grid::column_t i;
for (i = 0; i <= m_screen->cursor.col; i++) {
if (i < (glong) _vte_row_data_length (rowdata)) {
/* Muck with the cell in this location. */
auto pcell = _vte_row_data_get_writable(rowdata, i);
*pcell = m_color_defaults;
} else {
/* Add new cells until we have one here. */
_vte_row_data_append (rowdata, &m_color_defaults);
}
}
/* Repaint this row's paragraph. */
invalidate_row_and_context(m_screen->cursor.row);
/* We've modified the display. Make a note of it. */
m_text_deleted_flag = TRUE;
}
/* Clear to the right of the cursor and below the current line. */
void
Terminal::clear_below_current()
{
maybe_retreat_cursor();
/* If the cursor is actually on the screen, clear the rest of the
* row the cursor is on and all of the rows below the cursor. */
VteRowData *rowdata;
auto i = m_screen->cursor.row;
if (i < long(m_screen->row_data->next())) {
/* Get the data for the row we're clipping. */
rowdata = m_screen->row_data->index_writable(i);
/* Clean up Tab/CJK fragments. */
if ((glong) _vte_row_data_length(rowdata) > m_screen->cursor.col)
cleanup_fragments(m_screen->cursor.col, _vte_row_data_length(rowdata));
/* Clear everything to the right of the cursor. */
if (rowdata)
_vte_row_data_shrink(rowdata, m_screen->cursor.col);
}
/* Now for the rest of the lines. */
for (i = m_screen->cursor.row + 1;
i < long(m_screen->row_data->next());
i++) {
/* Get the data for the row we're removing. */
rowdata = m_screen->row_data->index_writable(i);
/* Remove it. */
if (rowdata)
_vte_row_data_shrink (rowdata, 0);
}
/* Now fill the cleared areas. */
bool const not_default_bg = (m_color_defaults.attr.back() != VTE_DEFAULT_BG);
for (i = m_screen->cursor.row;
i < m_screen->insert_delta + m_row_count;
i++) {
/* Retrieve the row's data, creating it if necessary. */
if (m_screen->row_data->contains(i)) {
rowdata = m_screen->row_data->index_writable(i);
g_assert(rowdata != NULL);
} else {
rowdata = ring_append(false);
}
/* Pad out the row. */
if (not_default_bg) {
_vte_row_data_fill(rowdata, &m_color_defaults, m_column_count);
}
set_hard_wrapped(i);
if (i > m_screen->cursor.row)
rowdata->attr.bidi_flags = get_bidi_flags();
}
/* Repaint the cleared area (might need to extend upwards). */
invalidate_rows_and_context(m_screen->cursor.row, m_screen->insert_delta + m_row_count - 1);
/* We've modified the display. Make a note of it. */
m_text_deleted_flag = TRUE;
}
/* Clear from the cursor position to the end of the line. */
void
Terminal::clear_to_eol()
{
/* If we were to strictly emulate xterm, we'd ensure the cursor is onscreen.
* But due to https://bugzilla.gnome.org/show_bug.cgi?id=740789 we intentionally
* deviate and do instead what konsole does. This way emitting a \e[K doesn't
* influence the text flow, and serves as a perfect workaround against a new line
* getting painted with the active background color (except for a possible flicker).
*/
/* maybe_retreat_cursor(); */
/* Get the data for the row which the cursor points to. */
auto rowdata = ensure_cursor();
g_assert(rowdata != NULL);
if ((glong) _vte_row_data_length(rowdata) > m_screen->cursor.col) {
/* Clean up Tab/CJK fragments. */
cleanup_fragments(m_screen->cursor.col, _vte_row_data_length(rowdata));
/* Remove the data at the end of the array until the current column
* is the end of the array. */
_vte_row_data_shrink(rowdata, m_screen->cursor.col);
/* We've modified the display. Make a note of it. */
m_text_deleted_flag = TRUE;
}
bool const not_default_bg = (m_color_defaults.attr.back() != VTE_DEFAULT_BG);
if (not_default_bg) {
/* Add enough cells to fill out the row. */
_vte_row_data_fill(rowdata, &m_color_defaults, m_column_count);
}
set_hard_wrapped(m_screen->cursor.row);
/* Repaint this row's paragraph. */
invalidate_row_and_context(m_screen->cursor.row);
}
/*
* Terminal::set_cursor_column:
* @col: the column. 0-based from 0 to m_column_count - 1
*
* Sets the cursor column to @col.
*
* @col is relative relative to the DECSLRM scrolling region iff origin mode (DECOM) is enabled.
*/
void
Terminal::set_cursor_column(vte::grid::column_t col)
{
_vte_debug_print(VTE_DEBUG_PARSER,
"Moving cursor to column %ld.\n", col);
vte::grid::column_t left_col, right_col;
if (m_modes_private.DEC_ORIGIN()) {
left_col = m_scrolling_region.left();
right_col = m_scrolling_region.right();
} else {
left_col = 0;
right_col = m_column_count - 1;
}
col += left_col;
col = CLAMP(col, left_col, right_col);
m_screen->cursor.col = col;
m_screen->cursor_advanced_by_graphic_character = false;
}
void
Terminal::set_cursor_column1(vte::grid::column_t col)
{
set_cursor_column(col - 1);
}
/*
* Terminal::set_cursor_row:
* @row: the row. 0-based
*
* Sets the cursor row to @row.
*
* @row is relative to the scrolling DECSTBM scrolling region iff origin mode (DECOM) is enabled.
*/
void
Terminal::set_cursor_row(vte::grid::row_t row)
{
_vte_debug_print(VTE_DEBUG_PARSER,
"Moving cursor to row %ld.\n", row);
vte::grid::row_t top_row, bottom_row;
if (m_modes_private.DEC_ORIGIN()) {
top_row = m_scrolling_region.top();
bottom_row = m_scrolling_region.bottom();
} else {
top_row = 0;
bottom_row = m_row_count - 1;
}
row += top_row;
row = CLAMP(row, top_row, bottom_row);
m_screen->cursor.row = row + m_screen->insert_delta;
m_screen->cursor_advanced_by_graphic_character = false;
}
void
Terminal::set_cursor_row1(vte::grid::row_t row)
{
set_cursor_row(row - 1);
}
/*
* Terminal::set_cursor_coords:
* @row: the row. 0-based
* @col: the column. 0-based from 0 to m_column_count - 1
*
* Sets the cursor row to @row.
*
* Sets the cursor column to @col, clamped to the range 0..m_column_count-1.
*
* @row and @col are relative to the scrolling DECSTBM / DECSLRM scrolling region
* iff origin mode (DECOM) is enabled.
*/
void
Terminal::set_cursor_coords(vte::grid::row_t row,
vte::grid::column_t column)
{
set_cursor_column(column);
set_cursor_row(row);
}
void
Terminal::set_cursor_coords1(vte::grid::row_t row,
vte::grid::column_t column)
{
set_cursor_column1(column);
set_cursor_row1(row);
}
void
Terminal::erase_characters(long count,
bool use_basic)
{
VteCell *cell;
long col, i;
maybe_retreat_cursor();
count = CLAMP(count, 1, m_column_count - m_screen->cursor.col);
/* Clear out the given number of characters. */
auto rowdata = ensure_row();
if (long(m_screen->row_data->next()) > m_screen->cursor.row) {
g_assert(rowdata != NULL);
/* Clean up Tab/CJK fragments. */
cleanup_fragments(m_screen->cursor.col, m_screen->cursor.col + count);
/* Write over the characters. (If there aren't enough, we'll
* need to create them.) */
_vte_row_data_fill (rowdata, &basic_cell, m_screen->cursor.col);
for (i = 0; i < count; i++) {
col = m_screen->cursor.col + i;
if (col >= 0) {
if (col < (glong) _vte_row_data_length (rowdata)) {
/* Replace this cell with the current
* defaults. */
cell = _vte_row_data_get_writable (rowdata, col);
*cell = use_basic ? basic_cell : m_color_defaults;
} else {
/* Add new cells until we have one here. */
_vte_row_data_fill (rowdata, use_basic ? &basic_cell : &m_color_defaults, col + 1);
}
}
}
/* Repaint this row's paragraph. */
invalidate_row_and_context(m_screen->cursor.row);
}
/* We've modified the display. Make a note of it. */
m_text_deleted_flag = TRUE;
}
void
Terminal::erase_image_rect(vte::grid::row_t rows,
vte::grid::column_t columns)
{
auto const top = m_screen->cursor.row;
/* FIXMEchpe: simplify! */
for (auto i = 0; i < rows; ++i) {
auto const row = top + i;
erase_characters(columns, true);
if (row > m_screen->insert_delta - 1 &&
row < m_screen->insert_delta + m_row_count)
set_hard_wrapped(row);
if (i == rows - 1) {
if (m_modes_private.MINTTY_SIXEL_SCROLL_CURSOR_RIGHT())
move_cursor_forward(columns);
else
cursor_down_with_scrolling(true);
} else {
cursor_down_with_scrolling(true);
}
}
m_screen->cursor_advanced_by_graphic_character = false;
}
/* Terminal::move_cursor_up:
* Cursor up by n rows (respecting the DECSTBM / DECSLRM scrolling region).
*
* See the "CUU, CUD, CUB, CUF" picture in ../doc/scrolling-region.txt.
*
* DEC STD 070 says not to move further if the cursor hits the margin outside of the scrolling area.
* Xterm follows this, and so do we. Reportedly (#2526) DEC terminals move the cursor despite their doc.
*/
void
Terminal::move_cursor_up(vte::grid::row_t rows)
{
// FIXMEchpe allow 0 as no-op?
rows = CLAMP(rows, 1, m_row_count);
//FIXMEchpe why not do this afterward?
maybe_retreat_cursor();
vte::grid::row_t top;
if (m_screen->cursor.row >= m_screen->insert_delta + m_scrolling_region.top()) {
top = m_screen->insert_delta + m_scrolling_region.top();
} else {
top = m_screen->insert_delta;
}
m_screen->cursor.row = MAX(m_screen->cursor.row - rows, top);
m_screen->cursor_advanced_by_graphic_character = false;
}
/* Terminal::move_cursor_down:
* Cursor down by n rows (respecting the DECSTBM / DECSLRM scrolling region).
*
* See the "CUU, CUD, CUB, CUF" picture in ../doc/scrolling-region.txt.
*
* DEC STD 070 says not to move further if the cursor hits the margin outside of the scrolling area.
* Xterm follows this, and so do we. Reportedly (#2526) DEC terminals move the cursor despite their doc.
*/
void
Terminal::move_cursor_down(vte::grid::row_t rows)
{
rows = CLAMP(rows, 1, m_row_count);
// FIXMEchpe why not do this afterwards?
maybe_retreat_cursor();
vte::grid::row_t bottom;
if (m_screen->cursor.row <= m_screen->insert_delta + m_scrolling_region.bottom()) {
bottom = m_screen->insert_delta + m_scrolling_region.bottom();
} else {
bottom = m_screen->insert_delta + m_row_count - 1;
}
m_screen->cursor.row = MIN(m_screen->cursor.row + rows, bottom);
m_screen->cursor_advanced_by_graphic_character = false;
}
/* Terminal::move_cursor_backward:
* Cursor left by n columns (respecting the DECSTBM / DECSLRM scrolling region).
*
* See the "CUU, CUD, CUB, CUF" picture in ../doc/scrolling-region.txt.
*
* DEC STD 070 says not to move further if the cursor hits the margin outside of the scrolling area.
* Xterm follows this, and so do we. Reportedly (#2526) DEC terminals move the cursor despite their doc.
*/
void
Terminal::move_cursor_backward(vte::grid::column_t columns)
{
columns = CLAMP(columns, 1, m_column_count);
maybe_retreat_cursor();
vte::grid::column_t left;
if (m_screen->cursor.col >= m_scrolling_region.left()) {
left = m_scrolling_region.left();
} else {