summarylogtreecommitdiffstats
path: root/0001-emulation-Track-the-cursor-separately-for-the-two-sc.patch
blob: 4c29eee515c0e5fd28c7963e8a4950434b4cc279 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
From fff3b5f904d3b6d1deb9beec9ae871b135696966 Mon Sep 17 00:00:00 2001
From: Egmont Koblinger <egmont@gmail.com>
Date: Thu, 28 Jan 2016 14:09:55 +0100
Subject: [PATCH] emulation: Track the cursor separately for the two screens

This is required to correctly resize the normal screen's contents
while the alternate screen is active, fixing a bug introduced by
commit 5a434e6c4457bdfe182a13213396e7a66a08f767.

https://bugzilla.gnome.org/show_bug.cgi?id=761097
---
 src/vte.cc         | 160 +++++++++++++++++++++++++----------------------------
 src/vteinternal.hh |   4 +-
 src/vteseq.cc      | 152 +++++++++++++++++++++++++-------------------------
 3 files changed, 153 insertions(+), 163 deletions(-)

diff --git a/src/vte.cc b/src/vte.cc
index 3a9a116d1973..701891ba93b3 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -743,8 +743,8 @@ _vte_invalidate_cursor_once(VteTerminal *terminal, gboolean periodic)
 	if (terminal->pvt->cursor_visible) {
 		preedit_width = vte_terminal_preedit_width(terminal, FALSE);
 
-                row = terminal->pvt->cursor.row;
-                column = terminal->pvt->cursor.col;
+                row = terminal->pvt->screen->cursor.row;
+                column = terminal->pvt->screen->cursor.col;
 		columns = 1;
 		column = find_start_column (terminal, column, row);
 		cell = vte_terminal_find_charcell(terminal, column, row);
@@ -1843,8 +1843,7 @@ _vte_terminal_adjust_adjustments(VteTerminal *terminal)
 	 * area.  Leave the scrolling delta alone because it will be updated
 	 * when the adjustment changes. */
 	screen->insert_delta = MAX(screen->insert_delta, delta);
-        terminal->pvt->cursor.row = MAX(terminal->pvt->cursor.row,
-                                        screen->insert_delta);
+        screen->cursor.row = MAX(screen->cursor.row, screen->insert_delta);
 
 	if (screen->scroll_delta > screen->insert_delta) {
 		vte_terminal_queue_adjustment_value_changed(terminal,
@@ -2137,7 +2136,7 @@ _vte_terminal_ensure_row (VteTerminal *terminal)
 
 	/* Must make sure we're in a sane area. */
 	screen = terminal->pvt->screen;
-        v = terminal->pvt->cursor.row;
+        v = screen->cursor.row;
 
 	/* Figure out how many rows we need to add. */
 	delta = v - _vte_ring_next(screen->row_data) + 1;
@@ -2159,7 +2158,7 @@ vte_terminal_ensure_cursor(VteTerminal *terminal)
 	VteRowData *row;
 
 	row = _vte_terminal_ensure_row (terminal);
-        _vte_row_data_fill (row, &basic_cell.cell, terminal->pvt->cursor.col);
+        _vte_row_data_fill (row, &basic_cell.cell, terminal->pvt->screen->cursor.col);
 
 	return row;
 }
@@ -2177,7 +2176,7 @@ _vte_terminal_update_insert_delta(VteTerminal *terminal)
 	/* The total number of lines.  Add one to the cursor offset
 	 * because it's zero-based. */
 	rows = _vte_ring_next (screen->row_data);
-        delta = terminal->pvt->cursor.row - rows + 1;
+        delta = screen->cursor.row - rows + 1;
 	if (G_UNLIKELY (delta > 0)) {
 		vte_terminal_insert_rows (terminal, delta);
 		rows = _vte_ring_next (screen->row_data);
@@ -2189,7 +2188,7 @@ _vte_terminal_update_insert_delta(VteTerminal *terminal)
 	delta = screen->insert_delta;
 	delta = MIN(delta, rows - terminal->pvt->row_count);
 	delta = MAX(delta,
-                    terminal->pvt->cursor.row - (terminal->pvt->row_count - 1));
+                    screen->cursor.row - (terminal->pvt->row_count - 1));
 	delta = MAX(delta, _vte_ring_delta(screen->row_data));
 
 	/* Adjust the insert delta and scroll if needed. */
@@ -2921,7 +2920,7 @@ _vte_terminal_cleanup_fragments(VteTerminal *terminal,
                         cell_end->attr.columns = 1;
                         _vte_invalidate_cells(terminal,
                                               end, 1,
-                                              terminal->pvt->cursor.row, 1);
+                                              terminal->pvt->screen->cursor.row, 1);
                 }
         }
 
@@ -2947,7 +2946,7 @@ _vte_terminal_cleanup_fragments(VteTerminal *terminal,
                                         g_assert(start - col == 1);
                                         _vte_invalidate_cells(terminal,
                                                               col, 1,
-                                                              terminal->pvt->cursor.row, 1);
+                                                              terminal->pvt->screen->cursor.row, 1);
                                 }
                                 keep_going = FALSE;
                         }
@@ -2974,19 +2973,19 @@ _vte_terminal_cursor_down (VteTerminal *terminal)
 		start = screen->insert_delta;
 		end = start + terminal->pvt->row_count - 1;
 	}
-        if (terminal->pvt->cursor.row == end) {
+        if (screen->cursor.row == end) {
                 if (terminal->pvt->scrolling_restricted) {
 			if (start == screen->insert_delta) {
 				/* Scroll this line into the scrollback
 				 * buffer by inserting a line at the next
 				 * line and scrolling the area up. */
 				screen->insert_delta++;
-                                terminal->pvt->cursor.row++;
+                                screen->cursor.row++;
 				/* update start and end, as they are relative
 				 * to insert_delta. */
 				start++;
 				end++;
-                                _vte_terminal_ring_insert (terminal, terminal->pvt->cursor.row, FALSE);
+                                _vte_terminal_ring_insert (terminal, screen->cursor.row, FALSE);
 				/* Force the areas below the region to be
 				 * redrawn -- they've moved. */
 				_vte_terminal_scroll_region(terminal, start,
@@ -3008,7 +3007,7 @@ _vte_terminal_cursor_down (VteTerminal *terminal)
 			}
 		} else {
 			/* Scroll up with history. */
-                        terminal->pvt->cursor.row++;
+                        screen->cursor.row++;
 			_vte_terminal_update_insert_delta(terminal);
 		}
 
@@ -3020,7 +3019,7 @@ _vte_terminal_cursor_down (VteTerminal *terminal)
 		}
 	} else {
 		/* Otherwise, just move the cursor down. */
-                terminal->pvt->cursor.row++;
+                screen->cursor.row++;
 	}
 }
 
@@ -3042,9 +3041,9 @@ _vte_terminal_drop_scrollback (VteTerminal *terminal)
 void
 _vte_terminal_restore_cursor (VteTerminal *terminal, VteScreen *screen)
 {
-        terminal->pvt->cursor.col = screen->saved.cursor.col;
-        terminal->pvt->cursor.row = screen->insert_delta + CLAMP(screen->saved.cursor.row,
-                                                                 0, terminal->pvt->row_count - 1);
+        screen->cursor.col = screen->saved.cursor.col;
+        screen->cursor.row = screen->insert_delta + CLAMP(screen->saved.cursor.row,
+                                                          0, terminal->pvt->row_count - 1);
 
         terminal->pvt->reverse_mode = screen->saved.reverse_mode;
         terminal->pvt->origin_mode = screen->saved.origin_mode;
@@ -3063,8 +3062,8 @@ _vte_terminal_restore_cursor (VteTerminal *terminal, VteScreen *screen)
 void
 _vte_terminal_save_cursor (VteTerminal *terminal, VteScreen *screen)
 {
-        screen->saved.cursor.col = terminal->pvt->cursor.col;
-        screen->saved.cursor.row = terminal->pvt->cursor.row - screen->insert_delta;
+        screen->saved.cursor.col = screen->cursor.col;
+        screen->saved.cursor.row = screen->cursor.row - screen->insert_delta;
 
         screen->saved.reverse_mode = terminal->pvt->reverse_mode;
         screen->saved.origin_mode = terminal->pvt->origin_mode;
@@ -3144,21 +3143,21 @@ _vte_terminal_insert_char(VteTerminal *terminal, gunichar c,
         columns = _vte_unichar_width(c, terminal->pvt->utf8_ambiguous_width);
 
 	/* If we're autowrapping here, do it. */
-        col = terminal->pvt->cursor.col;
+        col = screen->cursor.col;
 	if (G_UNLIKELY (columns && col + columns > terminal->pvt->column_count)) {
 		if (terminal->pvt->autowrap) {
 			_vte_debug_print(VTE_DEBUG_ADJ,
 					"Autowrapping before character\n");
 			/* Wrap. */
 			/* XXX clear to the end of line */
-                        col = terminal->pvt->cursor.col = 0;
+                        col = screen->cursor.col = 0;
 			/* Mark this line as soft-wrapped. */
 			row = _vte_terminal_ensure_row (terminal);
 			row->attr.soft_wrapped = 1;
 			_vte_terminal_cursor_down (terminal);
 		} else {
 			/* Don't wrap, stay at the rightmost column. */
-                        col = terminal->pvt->cursor.col =
+                        col = screen->cursor.col =
 				terminal->pvt->column_count - columns;
 		}
 		line_wrapped = TRUE;
@@ -3169,7 +3168,7 @@ _vte_terminal_insert_char(VteTerminal *terminal, gunichar c,
 			(long)c, c < 256 ? c : ' ',
                          (int)terminal->pvt->color_defaults.attr.fore,
                          (int)terminal->pvt->color_defaults.attr.back,
-                        col, columns, (long)terminal->pvt->cursor.row,
+                        col, columns, (long)screen->cursor.row,
 			(long)screen->insert_delta);
 
 
@@ -3182,7 +3181,7 @@ _vte_terminal_insert_char(VteTerminal *terminal, gunichar c,
 
 		_vte_debug_print(VTE_DEBUG_PARSE, "combining U+%04X", c);
 
-                row_num = terminal->pvt->cursor.row;
+                row_num = screen->cursor.row;
 		row = NULL;
 		if (G_UNLIKELY (col == 0)) {
 			/* We are at first column.  See if the previous line softwrapped.
@@ -3282,10 +3281,10 @@ _vte_terminal_insert_char(VteTerminal *terminal, gunichar c,
 		_vte_invalidate_cells(terminal,
 				col - columns,
 				insert ? terminal->pvt->column_count : columns,
-                                terminal->pvt->cursor.row, 1);
+                                screen->cursor.row, 1);
 	}
 
-        terminal->pvt->cursor.col = col;
+        screen->cursor.col = col;
 
 done:
 	/* We added text, so make a note of it. */
@@ -3724,12 +3723,12 @@ vte_terminal_process_incoming(VteTerminal *terminal)
 	bottom = screen->insert_delta == delta;
 
 	/* Save the current cursor position. */
-        cursor = terminal->pvt->cursor;
+        cursor = screen->cursor;
 	cursor_visible = terminal->pvt->cursor_visible;
 
         in_scroll_region = terminal->pvt->scrolling_restricted
-            && (terminal->pvt->cursor.row >= (screen->insert_delta + terminal->pvt->scrolling_region.start))
-            && (terminal->pvt->cursor.row <= (screen->insert_delta + terminal->pvt->scrolling_region.end));
+            && (screen->cursor.row >= (screen->insert_delta + terminal->pvt->scrolling_region.start))
+            && (screen->cursor.row <= (screen->insert_delta + terminal->pvt->scrolling_region.end));
 
 	/* We should only be called when there's data to process. */
 	g_assert(terminal->pvt->incoming ||
@@ -3839,8 +3838,8 @@ skip_chunk:
 			modified = TRUE;
 
                         new_in_scroll_region = terminal->pvt->scrolling_restricted
-                            && (terminal->pvt->cursor.row >= (screen->insert_delta + terminal->pvt->scrolling_region.start))
-                            && (terminal->pvt->cursor.row <= (screen->insert_delta + terminal->pvt->scrolling_region.end));
+                            && (screen->cursor.row >= (screen->insert_delta + terminal->pvt->scrolling_region.start))
+                            && (screen->cursor.row <= (screen->insert_delta + terminal->pvt->scrolling_region.end));
 
 			delta = screen->scroll_delta;	/* delta may have changed from sequence. */
 
@@ -3849,10 +3848,10 @@ skip_chunk:
                          */
 			if (invalidated_text &&
 					((new_in_scroll_region && !in_scroll_region) ||
-                                         (terminal->pvt->cursor.col > bbox_bottomright.x + VTE_CELL_BBOX_SLACK ||
-                                          terminal->pvt->cursor.col < bbox_topleft.x - VTE_CELL_BBOX_SLACK     ||
-                                          terminal->pvt->cursor.row > bbox_bottomright.y + VTE_CELL_BBOX_SLACK ||
-                                          terminal->pvt->cursor.row < bbox_topleft.y - VTE_CELL_BBOX_SLACK))) {
+                                         (screen->cursor.col > bbox_bottomright.x + VTE_CELL_BBOX_SLACK ||
+                                          screen->cursor.col < bbox_topleft.x - VTE_CELL_BBOX_SLACK     ||
+                                          screen->cursor.row > bbox_bottomright.y + VTE_CELL_BBOX_SLACK ||
+                                          screen->cursor.row < bbox_topleft.y - VTE_CELL_BBOX_SLACK))) {
 				/* Clip off any part of the box which isn't already on-screen. */
 				bbox_topleft.x = MAX(bbox_topleft.x, 0);
 				bbox_topleft.y = MAX(bbox_topleft.y, delta);
@@ -3931,20 +3930,18 @@ skip_chunk:
 				}
 			}
 
-			bbox_topleft.x = MIN(bbox_topleft.x,
-                                        terminal->pvt->cursor.col);
-			bbox_topleft.y = MIN(bbox_topleft.y,
-                                        terminal->pvt->cursor.row);
+			bbox_topleft.x = MIN(bbox_topleft.x, screen->cursor.col);
+			bbox_topleft.y = MIN(bbox_topleft.y, screen->cursor.row);
 
 			/* Insert the character. */
 			if (G_UNLIKELY (_vte_terminal_insert_char(terminal, c,
 						 FALSE, FALSE))) {
 				/* line wrapped, correct bbox */
 				if (invalidated_text &&
-                                                (terminal->pvt->cursor.col > bbox_bottomright.x + VTE_CELL_BBOX_SLACK	||
-                                                 terminal->pvt->cursor.col < bbox_topleft.x - VTE_CELL_BBOX_SLACK	||
-                                                 terminal->pvt->cursor.row > bbox_bottomright.y + VTE_CELL_BBOX_SLACK	||
-                                                 terminal->pvt->cursor.row < bbox_topleft.y - VTE_CELL_BBOX_SLACK)) {
+                                                (screen->cursor.col > bbox_bottomright.x + VTE_CELL_BBOX_SLACK	||
+                                                 screen->cursor.col < bbox_topleft.x - VTE_CELL_BBOX_SLACK	||
+                                                 screen->cursor.row > bbox_bottomright.y + VTE_CELL_BBOX_SLACK	||
+                                                 screen->cursor.row < bbox_topleft.y - VTE_CELL_BBOX_SLACK)) {
 					/* Clip off any part of the box which isn't already on-screen. */
 					bbox_topleft.x = MAX(bbox_topleft.x, 0);
 					bbox_topleft.y = MAX(bbox_topleft.y, delta);
@@ -3964,16 +3961,13 @@ skip_chunk:
 
 				}
 				bbox_topleft.x = MIN(bbox_topleft.x, 0);
-				bbox_topleft.y = MIN(bbox_topleft.y,
-                                                     terminal->pvt->cursor.row);
+				bbox_topleft.y = MIN(bbox_topleft.y, screen->cursor.row);
 			}
 			/* Add the cells over which we have moved to the region
 			 * which we need to refresh for the user. */
-			bbox_bottomright.x = MAX(bbox_bottomright.x,
-                                                 terminal->pvt->cursor.col);
+			bbox_bottomright.x = MAX(bbox_bottomright.x, screen->cursor.col);
                         /* cursor.row + 1 (defer until inv.) */
-			bbox_bottomright.y = MAX(bbox_bottomright.y,
-                                                 terminal->pvt->cursor.row);
+			bbox_bottomright.y = MAX(bbox_bottomright.y, screen->cursor.row);
 			invalidated_text = TRUE;
 
 			/* We *don't* emit flush pending signals here. */
@@ -4007,7 +4001,7 @@ skip_chunk:
 			 _vte_ring_delta(screen->row_data));
 		/* The cursor shouldn't be above or below the addressable
 		 * part of the display buffer. */
-                g_assert(terminal->pvt->cursor.row >= terminal->pvt->screen->insert_delta);
+                g_assert(screen->cursor.row >= screen->insert_delta);
 #endif
 
 next_match:
@@ -4082,8 +4076,8 @@ next_match:
 	}
 
 
-        if ((cursor.col != terminal->pvt->cursor.col) ||
-            (cursor.row != terminal->pvt->cursor.row)) {
+        if ((cursor.col != terminal->pvt->screen->cursor.col) ||
+            (cursor.row != terminal->pvt->screen->cursor.row)) {
 		/* invalidate the old and new cursor positions */
 		if (cursor_visible)
 			_vte_invalidate_cell(terminal, cursor.col, cursor.row);
@@ -4099,11 +4093,9 @@ next_match:
 	/* Tell the input method where the cursor is. */
 	if (gtk_widget_get_realized (&terminal->widget)) {
 		GdkRectangle rect;
-                rect.x = terminal->pvt->cursor.col *
-			 terminal->pvt->char_width + terminal->pvt->padding.left;
+                rect.x = terminal->pvt->screen->cursor.col * terminal->pvt->char_width + terminal->pvt->padding.left;
 		rect.width = terminal->pvt->char_width;
-                rect.y = (terminal->pvt->cursor.row - delta) *
-			 terminal->pvt->char_height + terminal->pvt->padding.top;
+                rect.y = (terminal->pvt->screen->cursor.row - delta) * terminal->pvt->char_height + terminal->pvt->padding.top;
 		rect.height = terminal->pvt->char_height;
 		gtk_im_context_set_cursor_location(terminal->pvt->im_context,
 						   &rect);
@@ -4830,8 +4822,7 @@ vte_terminal_key_press(GtkWidget *widget, GdkEventKey *event)
 		/* If we're in margin bell mode and on the border of the
 		 * margin, bell. */
 		if (terminal->pvt->margin_bell) {
-                        if ((terminal->pvt->cursor.col +
-			     (glong) terminal->pvt->bell_margin) ==
+                        if ((terminal->pvt->screen->cursor.col + (glong) terminal->pvt->bell_margin) ==
 			     terminal->pvt->column_count) {
 				_vte_terminal_beep (terminal);
 			}
@@ -6530,10 +6521,10 @@ vte_terminal_get_cursor_position(VteTerminal *terminal,
 {
 	g_return_if_fail(VTE_IS_TERMINAL(terminal));
 	if (column) {
-                *column = terminal->pvt->cursor.col;
+                *column = terminal->pvt->screen->cursor.col;
 	}
 	if (row) {
-                *row = terminal->pvt->cursor.row;
+                *row = terminal->pvt->screen->cursor.row;
 	}
 }
 
@@ -8058,14 +8049,14 @@ vte_terminal_screen_set_size(VteTerminal *terminal, VteScreen *screen, glong old
 			"     cursor_saved (relative to insert_delta)  row=%ld  col=%ld\n",
 			screen == &terminal->pvt->normal_screen ? "normal" : "alternate",
 			screen->insert_delta, screen->scroll_delta,
-                        terminal->pvt->cursor.row, terminal->pvt->cursor.row - screen->scroll_delta + 1, terminal->pvt->cursor.col,
+                        screen->cursor.row, screen->cursor.row - screen->scroll_delta + 1, screen->cursor.col,
                         screen->saved.cursor.row, screen->saved.cursor.col);
 
         cursor_saved_absolute.row = screen->saved.cursor.row + screen->insert_delta;
         cursor_saved_absolute.col = screen->saved.cursor.col;
 	below_viewport.row = screen->scroll_delta + old_rows;
 	below_viewport.col = 0;
-        below_current_paragraph.row = terminal->pvt->cursor.row + 1;
+        below_current_paragraph.row = screen->cursor.row + 1;
 	while (below_current_paragraph.row < _vte_ring_next(ring)
 	    && _vte_ring_index(ring, below_current_paragraph.row - 1)->attr.soft_wrapped) {
 		below_current_paragraph.row++;
@@ -8075,15 +8066,12 @@ vte_terminal_screen_set_size(VteTerminal *terminal, VteScreen *screen, glong old
         markers[0] = &cursor_saved_absolute;
         markers[1] = &below_viewport;
         markers[2] = &below_current_paragraph;
-        if (screen == terminal->pvt->screen) {
-                /* Tracking the current cursor only makes sense on the active screen. */
-                markers[3] = &terminal->pvt->cursor;
-                if (terminal->pvt->has_selection) {
-                        /* selection_end is inclusive, make it non-inclusive, see bug 722635. */
-                        terminal->pvt->selection_end.col++;
-                        markers[4] = &terminal->pvt->selection_start;
-                        markers[5] = &terminal->pvt->selection_end;
-		}
+        markers[3] = &screen->cursor;
+        if (terminal->pvt->has_selection) {
+                /* selection_end is inclusive, make it non-inclusive, see bug 722635. */
+                terminal->pvt->selection_end.col++;
+                markers[4] = &terminal->pvt->selection_start;
+                markers[5] = &terminal->pvt->selection_end;
 	}
 
 	old_top_lines = below_current_paragraph.row - screen->insert_delta;
@@ -8111,7 +8099,7 @@ vte_terminal_screen_set_size(VteTerminal *terminal, VteScreen *screen, glong old
 		}
 	}
 
-	if (screen == terminal->pvt->screen && terminal->pvt->has_selection) {
+        if (terminal->pvt->has_selection) {
 		/* Make selection_end inclusive again, see above. */
 		terminal->pvt->selection_end.col--;
 	}
@@ -8160,7 +8148,7 @@ vte_terminal_screen_set_size(VteTerminal *terminal, VteScreen *screen, glong old
                         "     cursor (absolute)  row=%ld  (visual line %ld)  col=%ld\n"
 			"     cursor_saved (relative to insert_delta)  row=%ld  col=%ld\n\n",
 			screen->insert_delta, new_scroll_delta,
-                        terminal->pvt->cursor.row, terminal->pvt->cursor.row - new_scroll_delta + 1, terminal->pvt->cursor.col,
+                        screen->cursor.row, screen->cursor.row - new_scroll_delta + 1, screen->cursor.col,
                         screen->saved.cursor.row, screen->saved.cursor.col);
 
 	if (screen == terminal->pvt->screen)
@@ -8225,10 +8213,10 @@ vte_terminal_set_size(VteTerminal *terminal, glong columns, glong rows)
                 vte_terminal_set_scrollback_lines(terminal,
                                                   terminal->pvt->scrollback_lines);
                 /* Ensure the cursor is valid */
-                terminal->pvt->cursor.row = CLAMP (terminal->pvt->cursor.row,
-                                                    _vte_ring_delta (terminal->pvt->screen->row_data),
-                                                    MAX (_vte_ring_delta (terminal->pvt->screen->row_data),
-                                                         _vte_ring_next (terminal->pvt->screen->row_data) - 1));
+                terminal->pvt->screen->cursor.row = CLAMP (terminal->pvt->screen->cursor.row,
+                                                           _vte_ring_delta (terminal->pvt->screen->row_data),
+                                                           MAX (_vte_ring_delta (terminal->pvt->screen->row_data),
+                                                                _vte_ring_next (terminal->pvt->screen->row_data) - 1));
 
 		_vte_terminal_adjust_adjustments_full (terminal);
 		gtk_widget_queue_resize_no_redraw (&terminal->widget);
@@ -9963,8 +9951,8 @@ vte_terminal_paint_cursor(VteTerminal *terminal)
 
 	screen = terminal->pvt->screen;
 	delta = screen->scroll_delta;
-        col = terminal->pvt->cursor.col;
-        drow = terminal->pvt->cursor.row;
+        col = screen->cursor.col;
+        drow = screen->cursor.row;
 	row = drow - delta;
 	width = terminal->pvt->char_width;
 	height = terminal->pvt->char_height;
@@ -10104,7 +10092,7 @@ vte_terminal_paint_im_preedit_string(VteTerminal *terminal)
 	height = terminal->pvt->char_height;
 	delta = screen->scroll_delta;
 
-        row = terminal->pvt->cursor.row - delta;
+        row = screen->cursor.row - delta;
 
 	/* Find out how many columns the pre-edit string takes up. */
 	columns = vte_terminal_preedit_width(terminal, FALSE);
@@ -10112,7 +10100,7 @@ vte_terminal_paint_im_preedit_string(VteTerminal *terminal)
 
 	/* If the pre-edit string won't fit on the screen if we start
 	 * drawing it at the cursor's position, move it left. */
-        col = terminal->pvt->cursor.col;
+        col = screen->cursor.col;
 	if (col + columns > terminal->pvt->column_count) {
 		col = MAX(0, terminal->pvt->column_count - columns);
 	}
@@ -12035,7 +12023,7 @@ vte_terminal_set_scrollback_lines(VteTerminal *terminal, glong lines)
         /* The main screen gets the full scrollback buffer. */
         screen = &terminal->pvt->normal_screen;
         lines = MAX (lines, terminal->pvt->row_count);
-        next = MAX (terminal->pvt->cursor.row + 1,
+        next = MAX (screen->cursor.row + 1,
                     _vte_ring_next (screen->row_data));
         _vte_ring_resize (screen->row_data, lines);
         low = _vte_ring_delta (screen->row_data);
@@ -12234,10 +12222,12 @@ vte_terminal_reset(VteTerminal *terminal,
                 pvt->screen = &pvt->normal_screen;
                 pvt->normal_screen.scroll_delta = pvt->normal_screen.insert_delta =
                         _vte_ring_reset(pvt->normal_screen.row_data);
+                pvt->normal_screen.cursor.row = pvt->normal_screen.insert_delta;
+                pvt->normal_screen.cursor.col = 0;
                 pvt->alternate_screen.scroll_delta = pvt->alternate_screen.insert_delta =
                         _vte_ring_reset(pvt->alternate_screen.row_data);
-                pvt->cursor.row = pvt->screen->insert_delta;
-                pvt->cursor.col = 0;
+                pvt->alternate_screen.cursor.row = pvt->alternate_screen.insert_delta;
+                pvt->alternate_screen.cursor.col = 0;
                 /* Adjust the scrollbar to the new location. */
                 /* Hack: force a change in scroll_delta even if the value remains, so that
                    vte_term_q_adj_val_changed() doesn't shortcut to no-op, see bug 730599. */
diff --git a/src/vteinternal.hh b/src/vteinternal.hh
index 04d8104b044d..f2152e6a8853 100644
--- a/src/vteinternal.hh
+++ b/src/vteinternal.hh
@@ -102,12 +102,13 @@ struct _vte_incoming_chunk{
 typedef struct _VteScreen VteScreen;
 struct _VteScreen {
         VteRing row_data[1];	/* buffer contents */
+        VteVisualPosition cursor;  /* absolute value, from the beginning of the terminal history */
         long scroll_delta;	/* scroll offset */
         long insert_delta;	/* insertion offset */
 
         /* Stuff saved along with the cursor */
         struct {
-                VteVisualPosition cursor;
+                VteVisualPosition cursor;  /* onscreen coordinate, that is, relative to insert_delta */
                 gboolean reverse_mode;
                 gboolean origin_mode;
                 gboolean sendrecv_mode;
@@ -208,7 +209,6 @@ public:
 	struct _VteScreen normal_screen, alternate_screen, *screen;
 
         /* Values we save along with the cursor */
-        VteVisualPosition cursor;	/* relative to the insertion delta */
         gboolean reverse_mode;	/* reverse mode */
         gboolean origin_mode;	/* origin mode */
         gboolean sendrecv_mode;	/* sendrecv mode */
diff --git a/src/vteseq.cc b/src/vteseq.cc
index af27ff5ef277..bf76995165bb 100644
--- a/src/vteseq.cc
+++ b/src/vteseq.cc
@@ -261,8 +261,8 @@ vte_terminal_emit_resize_window(VteTerminal *terminal,
 static void
 _vte_terminal_ensure_cursor_is_onscreen (VteTerminal *terminal)
 {
-        if (G_UNLIKELY (terminal->pvt->cursor.col >= terminal->pvt->column_count))
-                terminal->pvt->cursor.col = terminal->pvt->column_count - 1;
+        if (G_UNLIKELY (terminal->pvt->screen->cursor.col >= terminal->pvt->column_count))
+                terminal->pvt->screen->cursor.col = terminal->pvt->column_count - 1;
 }
 
 static void
@@ -279,8 +279,8 @@ _vte_terminal_home_cursor (VteTerminal *terminal)
                 origin = 0;
         }
 
-        terminal->pvt->cursor.row = screen->insert_delta + origin;
-        terminal->pvt->cursor.col = 0;
+        screen->cursor.row = screen->insert_delta + origin;
+        screen->cursor.col = 0;
 }
 
 /* Clear the entire screen. */
@@ -291,7 +291,7 @@ _vte_terminal_clear_screen (VteTerminal *terminal)
 	VteScreen *screen;
 	screen = terminal->pvt->screen;
 	initial = screen->insert_delta;
-        row = terminal->pvt->cursor.row - screen->insert_delta;
+        row = screen->cursor.row - screen->insert_delta;
 	initial = _vte_ring_next(screen->row_data);
 	/* Add a new screen's worth of rows. */
 	for (i = 0; i < terminal->pvt->row_count; i++)
@@ -299,7 +299,7 @@ _vte_terminal_clear_screen (VteTerminal *terminal)
 	/* Move the cursor and insertion delta to the first line in the
 	 * newly-cleared area and scroll if need be. */
 	screen->insert_delta = initial;
-        terminal->pvt->cursor.row = row + screen->insert_delta;
+        screen->cursor.row = row + screen->insert_delta;
 	_vte_terminal_adjust_adjustments(terminal);
 	/* Redraw everything. */
 	_vte_invalidate_all(terminal);
@@ -318,9 +318,9 @@ _vte_terminal_clear_current_line (VteTerminal *terminal)
 
 	/* If the cursor is actually on the screen, clear data in the row
 	 * which corresponds to the cursor. */
-        if (_vte_ring_next(screen->row_data) > terminal->pvt->cursor.row) {
+        if (_vte_ring_next(screen->row_data) > screen->cursor.row) {
 		/* Get the data for the row which the cursor points to. */
-                rowdata = _vte_ring_index_writable (screen->row_data, terminal->pvt->cursor.row);
+                rowdata = _vte_ring_index_writable (screen->row_data, screen->cursor.row);
 		g_assert(rowdata != NULL);
 		/* Remove it. */
 		_vte_row_data_shrink (rowdata, 0);
@@ -330,7 +330,7 @@ _vte_terminal_clear_current_line (VteTerminal *terminal)
 		/* Repaint this row. */
 		_vte_invalidate_cells(terminal,
 				      0, terminal->pvt->column_count,
-                                      terminal->pvt->cursor.row, 1);
+                                      screen->cursor.row, 1);
 	}
 
 	/* We've modified the display.  Make a note of it. */
@@ -347,7 +347,7 @@ _vte_terminal_clear_above_current (VteTerminal *terminal)
 	screen = terminal->pvt->screen;
 	/* If the cursor is actually on the screen, clear data in the row
 	 * which corresponds to the cursor. */
-        for (i = screen->insert_delta; i < terminal->pvt->cursor.row; i++) {
+        for (i = screen->insert_delta; i < screen->cursor.row; i++) {
 		if (_vte_ring_next(screen->row_data) > i) {
 			/* Get the data for the row we're erasing. */
 			rowdata = _vte_ring_index_writable (screen->row_data, i);
@@ -430,9 +430,9 @@ static void
 vte_sequence_handler_normal_screen (VteTerminal *terminal, GValueArray *params)
 {
         /* cursor.row includes insert_delta, adjust accordingly */
-        terminal->pvt->cursor.row -= terminal->pvt->screen->insert_delta;
+        long cr = terminal->pvt->screen->cursor.row - terminal->pvt->screen->insert_delta;
         terminal->pvt->screen = &terminal->pvt->normal_screen;
-        terminal->pvt->cursor.row += terminal->pvt->screen->insert_delta;
+        terminal->pvt->screen->cursor.row = cr + terminal->pvt->screen->insert_delta;
 
         /* Make sure the ring is large enough */
         _vte_terminal_ensure_row(terminal);
@@ -443,9 +443,9 @@ static void
 vte_sequence_handler_alternate_screen (VteTerminal *terminal, GValueArray *params)
 {
         /* cursor.row includes insert_delta, adjust accordingly */
-        terminal->pvt->cursor.row -= terminal->pvt->screen->insert_delta;
+        long cr = terminal->pvt->screen->cursor.row - terminal->pvt->screen->insert_delta;
         terminal->pvt->screen = &terminal->pvt->alternate_screen;
-        terminal->pvt->cursor.row += terminal->pvt->screen->insert_delta;
+        terminal->pvt->screen->cursor.row = cr + terminal->pvt->screen->insert_delta;
 
         /* Make sure the ring is large enough */
         _vte_terminal_ensure_row(terminal);
@@ -591,7 +591,7 @@ vte_sequence_handler_multiple_r(VteTerminal *terminal,
                                 VteTerminalSequenceHandler handler)
 {
         vte_sequence_handler_multiple_limited(terminal, params, handler,
-                                              terminal->pvt->column_count - terminal->pvt->cursor.col);
+                                              terminal->pvt->column_count - terminal->pvt->screen->cursor.col);
 }
 
 static void
@@ -1035,7 +1035,7 @@ vte_sequence_handler_cursor_back_tab (VteTerminal *terminal, GValueArray *params
 	long newcol;
 
 	/* Calculate which column is the previous tab stop. */
-        newcol = terminal->pvt->cursor.col;
+        newcol = terminal->pvt->screen->cursor.col;
 
 	if (terminal->pvt->tabstops != NULL) {
 		/* Find the next tabstop. */
@@ -1051,7 +1051,7 @@ vte_sequence_handler_cursor_back_tab (VteTerminal *terminal, GValueArray *params
 	/* Warp the cursor. */
 	_vte_debug_print(VTE_DEBUG_PARSE,
 			"Moving cursor to column %ld.\n", (long)newcol);
-        terminal->pvt->cursor.col = newcol;
+        terminal->pvt->screen->cursor.col = newcol;
 }
 
 /* Clear from the cursor position (inclusive!) to the beginning of the line. */
@@ -1067,11 +1067,11 @@ _vte_sequence_handler_cb (VteTerminal *terminal, GValueArray *params)
 	/* Get the data for the row which the cursor points to. */
 	rowdata = _vte_terminal_ensure_row(terminal);
         /* Clean up Tab/CJK fragments. */
-        _vte_terminal_cleanup_fragments (terminal, 0, terminal->pvt->cursor.col + 1);
+        _vte_terminal_cleanup_fragments (terminal, 0, terminal->pvt->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. */
-        for (i = 0; i <= terminal->pvt->cursor.col; i++) {
+        for (i = 0; i <= terminal->pvt->screen->cursor.col; i++) {
 		if (i < (glong) _vte_row_data_length (rowdata)) {
 			/* Muck with the cell in this location. */
 			pcell = _vte_row_data_get_writable (rowdata, i);
@@ -1083,8 +1083,8 @@ _vte_sequence_handler_cb (VteTerminal *terminal, GValueArray *params)
 	}
 	/* Repaint this row. */
 	_vte_invalidate_cells(terminal,
-                              0, terminal->pvt->cursor.col+1,
-                              terminal->pvt->cursor.row, 1);
+                              0, terminal->pvt->screen->cursor.col+1,
+                              terminal->pvt->screen->cursor.row, 1);
 
 	/* We've modified the display.  Make a note of it. */
 	terminal->pvt->text_deleted_flag = TRUE;
@@ -1103,19 +1103,19 @@ _vte_sequence_handler_cd (VteTerminal *terminal, GValueArray *params)
 	screen = terminal->pvt->screen;
 	/* 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. */
-        i = terminal->pvt->cursor.row;
+        i = screen->cursor.row;
 	if (i < _vte_ring_next(screen->row_data)) {
 		/* Get the data for the row we're clipping. */
 		rowdata = _vte_ring_index_writable (screen->row_data, i);
                 /* Clean up Tab/CJK fragments. */
-                if ((glong) _vte_row_data_length (rowdata) > terminal->pvt->cursor.col)
-                        _vte_terminal_cleanup_fragments (terminal, terminal->pvt->cursor.col, _vte_row_data_length (rowdata));
+                if ((glong) _vte_row_data_length (rowdata) > screen->cursor.col)
+                        _vte_terminal_cleanup_fragments (terminal, screen->cursor.col, _vte_row_data_length (rowdata));
 		/* Clear everything to the right of the cursor. */
 		if (rowdata)
-                        _vte_row_data_shrink (rowdata, terminal->pvt->cursor.col);
+                        _vte_row_data_shrink (rowdata, screen->cursor.col);
 	}
 	/* Now for the rest of the lines. */
-        for (i = terminal->pvt->cursor.row + 1;
+        for (i = screen->cursor.row + 1;
 	     i < _vte_ring_next(screen->row_data);
 	     i++) {
 		/* Get the data for the row we're removing. */
@@ -1125,7 +1125,7 @@ _vte_sequence_handler_cd (VteTerminal *terminal, GValueArray *params)
 			_vte_row_data_shrink (rowdata, 0);
 	}
 	/* Now fill the cleared areas. */
-        for (i = terminal->pvt->cursor.row;
+        for (i = screen->cursor.row;
 	     i < screen->insert_delta + terminal->pvt->row_count;
 	     i++) {
 		/* Retrieve the row's data, creating it if necessary. */
@@ -1167,12 +1167,12 @@ _vte_sequence_handler_ce (VteTerminal *terminal, GValueArray *params)
 	/* Get the data for the row which the cursor points to. */
 	rowdata = _vte_terminal_ensure_row(terminal);
 	g_assert(rowdata != NULL);
-        if ((glong) _vte_row_data_length (rowdata) > terminal->pvt->cursor.col) {
+        if ((glong) _vte_row_data_length (rowdata) > terminal->pvt->screen->cursor.col) {
                 /* Clean up Tab/CJK fragments. */
-                _vte_terminal_cleanup_fragments (terminal, terminal->pvt->cursor.col, _vte_row_data_length (rowdata));
+                _vte_terminal_cleanup_fragments (terminal, terminal->pvt->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, terminal->pvt->cursor.col);
+                _vte_row_data_shrink (rowdata, terminal->pvt->screen->cursor.col);
 		/* We've modified the display.  Make a note of it. */
 		terminal->pvt->text_deleted_flag = TRUE;
 	}
@@ -1183,10 +1183,10 @@ _vte_sequence_handler_ce (VteTerminal *terminal, GValueArray *params)
 	rowdata->attr.soft_wrapped = 0;
 	/* Repaint this row. */
 	_vte_invalidate_cells(terminal,
-                              terminal->pvt->cursor.col,
+                              terminal->pvt->screen->cursor.col,
 			      terminal->pvt->column_count -
-                              terminal->pvt->cursor.col,
-                              terminal->pvt->cursor.row, 1);
+                              terminal->pvt->screen->cursor.col,
+                              terminal->pvt->screen->cursor.row, 1);
 }
 
 /* Move the cursor to the given column (horizontal position), 1-based. */
@@ -1206,7 +1206,7 @@ vte_sequence_handler_cursor_character_absolute (VteTerminal *terminal, GValueArr
 		}
 	}
 
-        terminal->pvt->cursor.col = val;
+        terminal->pvt->screen->cursor.col = val;
 }
 
 /* Move the cursor to the given position, 1-based. */
@@ -1244,15 +1244,15 @@ vte_sequence_handler_cursor_position (VteTerminal *terminal, GValueArray *params
 			}
 		}
 	}
-        terminal->pvt->cursor.row = rowval + screen->insert_delta;
-        terminal->pvt->cursor.col = colval;
+        screen->cursor.row = rowval + screen->insert_delta;
+        screen->cursor.col = colval;
 }
 
 /* Carriage return. */
 static void
 vte_sequence_handler_carriage_return (VteTerminal *terminal, GValueArray *params)
 {
-        terminal->pvt->cursor.col = 0;
+        terminal->pvt->screen->cursor.col = 0;
 }
 
 /* Restrict scrolling and updates to a subset of the visible lines. */
@@ -1316,7 +1316,7 @@ vte_sequence_handler_set_scrolling_region (VteTerminal *terminal, GValueArray *p
 static void
 vte_sequence_handler_cursor_next_line (VteTerminal *terminal, GValueArray *params)
 {
-        terminal->pvt->cursor.col = 0;
+        terminal->pvt->screen->cursor.col = 0;
         vte_sequence_handler_cursor_down (terminal, params);
 }
 
@@ -1324,7 +1324,7 @@ vte_sequence_handler_cursor_next_line (VteTerminal *terminal, GValueArray *param
 static void
 vte_sequence_handler_cursor_preceding_line (VteTerminal *terminal, GValueArray *params)
 {
-        terminal->pvt->cursor.col = 0;
+        terminal->pvt->screen->cursor.col = 0;
         vte_sequence_handler_cursor_up (terminal, params);
 }
 
@@ -1356,7 +1356,7 @@ vte_sequence_handler_line_position_absolute (VteTerminal *terminal, GValueArray
         }
         val = val - 1 + origin;
         val = CLAMP(val, origin, rowmax);
-        terminal->pvt->cursor.row = screen->insert_delta + val;
+        screen->cursor.row = screen->insert_delta + val;
 }
 
 /* Delete a character at the current cursor position. */
@@ -1371,12 +1371,12 @@ _vte_sequence_handler_dc (VteTerminal *terminal, GValueArray *params)
 
 	screen = terminal->pvt->screen;
 
-        if (_vte_ring_next(screen->row_data) > terminal->pvt->cursor.row) {
+        if (_vte_ring_next(screen->row_data) > screen->cursor.row) {
 		long len;
 		/* Get the data for the row which the cursor points to. */
-                rowdata = _vte_ring_index_writable (screen->row_data, terminal->pvt->cursor.row);
+                rowdata = _vte_ring_index_writable (screen->row_data, screen->cursor.row);
 		g_assert(rowdata != NULL);
-                col = terminal->pvt->cursor.col;
+                col = screen->cursor.col;
 		len = _vte_row_data_length (rowdata);
 		/* Remove the column. */
 		if (col < len) {
@@ -1390,7 +1390,7 @@ _vte_sequence_handler_dc (VteTerminal *terminal, GValueArray *params)
 			/* Repaint this row. */
 			_vte_invalidate_cells(terminal,
 					col, len - col,
-                                        terminal->pvt->cursor.row, 1);
+                                        screen->cursor.row, 1);
 		}
 	}
 
@@ -1433,7 +1433,7 @@ vte_sequence_handler_cursor_down (VteTerminal *terminal, GValueArray *params)
                 }
         }
 
-        terminal->pvt->cursor.row = MIN(terminal->pvt->cursor.row + val, end);
+        screen->cursor.row = MIN(screen->cursor.row + val, end);
 }
 
 /* Erase characters starting at the cursor position (overwriting N with
@@ -1462,14 +1462,14 @@ vte_sequence_handler_erase_characters (VteTerminal *terminal, GValueArray *param
 
 	/* Clear out the given number of characters. */
 	rowdata = _vte_terminal_ensure_row(terminal);
-        if (_vte_ring_next(screen->row_data) > terminal->pvt->cursor.row) {
+        if (_vte_ring_next(screen->row_data) > screen->cursor.row) {
 		g_assert(rowdata != NULL);
                 /* Clean up Tab/CJK fragments. */
-                _vte_terminal_cleanup_fragments (terminal, terminal->pvt->cursor.col, terminal->pvt->cursor.col + count);
+                _vte_terminal_cleanup_fragments (terminal, screen->cursor.col, screen->cursor.col + count);
 		/* Write over the characters.  (If there aren't enough, we'll
 		 * need to create them.) */
 		for (i = 0; i < count; i++) {
-                        col = terminal->pvt->cursor.col + i;
+                        col = screen->cursor.col + i;
 			if (col >= 0) {
 				if (col < (glong) _vte_row_data_length (rowdata)) {
 					/* Replace this cell with the current
@@ -1484,8 +1484,8 @@ vte_sequence_handler_erase_characters (VteTerminal *terminal, GValueArray *param
 		}
 		/* Repaint this row. */
 		_vte_invalidate_cells(terminal,
-                                      terminal->pvt->cursor.col, count,
-                                      terminal->pvt->cursor.row, 1);
+                                      screen->cursor.col, count,
+                                      screen->cursor.row, 1);
 	}
 
 	/* We've modified the display.  Make a note of it. */
@@ -1507,11 +1507,11 @@ _vte_sequence_handler_insert_character (VteTerminal *terminal, GValueArray *para
 
         _vte_terminal_ensure_cursor_is_onscreen(terminal);
 
-        save = terminal->pvt->cursor;
+        save = terminal->pvt->screen->cursor;
 
 	_vte_terminal_insert_char(terminal, ' ', TRUE, TRUE);
 
-        terminal->pvt->cursor = save;
+        terminal->pvt->screen->cursor = save;
 }
 
 /* Insert N blank characters. */
@@ -1535,9 +1535,9 @@ vte_sequence_handler_backspace (VteTerminal *terminal, GValueArray *params)
 {
         _vte_terminal_ensure_cursor_is_onscreen(terminal);
 
-        if (terminal->pvt->cursor.col > 0) {
+        if (terminal->pvt->screen->cursor.col > 0) {
 		/* There's room to move left, so do so. */
-                terminal->pvt->cursor.col--;
+                terminal->pvt->screen->cursor.col--;
 	}
 }
 
@@ -1557,7 +1557,7 @@ vte_sequence_handler_cursor_backward (VteTerminal *terminal, GValueArray *params
                         val = MAX(g_value_get_long(value), 1);
                 }
         }
-        terminal->pvt->cursor.col = MAX(terminal->pvt->cursor.col - val, 0);
+        terminal->pvt->screen->cursor.col = MAX(terminal->pvt->screen->cursor.col - val, 0);
 }
 
 /* Cursor right N columns. */
@@ -1578,9 +1578,9 @@ vte_sequence_handler_cursor_forward (VteTerminal *terminal, GValueArray *params)
                 }
         }
         /* The cursor can be further to the right, don't move in that case. */
-        if (terminal->pvt->cursor.col < terminal->pvt->column_count) {
+        if (terminal->pvt->screen->cursor.col < terminal->pvt->column_count) {
 		/* There's room to move right. */
-                terminal->pvt->cursor.col = MIN(terminal->pvt->cursor.col + val,
+                terminal->pvt->screen->cursor.col = MIN(terminal->pvt->screen->cursor.col + val,
                                                 terminal->pvt->column_count - 1);
 	}
 }
@@ -1589,7 +1589,7 @@ vte_sequence_handler_cursor_forward (VteTerminal *terminal, GValueArray *params)
 static void
 vte_sequence_handler_next_line (VteTerminal *terminal, GValueArray *params)
 {
-        terminal->pvt->cursor.col = 0;
+        terminal->pvt->screen->cursor.col = 0;
 	_vte_terminal_cursor_down (terminal);
 }
 
@@ -1763,7 +1763,7 @@ vte_sequence_handler_reverse_index (VteTerminal *terminal, GValueArray *params)
 		end = start + terminal->pvt->row_count - 1;
 	}
 
-        if (terminal->pvt->cursor.row == start) {
+        if (screen->cursor.row == start) {
 		/* If we're at the top of the scrolling region, add a
 		 * line at the top to scroll the bottom off. */
 		_vte_terminal_ring_remove (terminal, end);
@@ -1775,7 +1775,7 @@ vte_sequence_handler_reverse_index (VteTerminal *terminal, GValueArray *params)
 				      start, 2);
 	} else {
 		/* Otherwise, just move the cursor up. */
-                terminal->pvt->cursor.row--;
+                screen->cursor.row--;
 	}
 	/* Adjust the scrollbars if necessary. */
 	_vte_terminal_adjust_adjustments(terminal);
@@ -1791,7 +1791,7 @@ vte_sequence_handler_tab_set (VteTerminal *terminal, GValueArray *params)
 		terminal->pvt->tabstops = g_hash_table_new(NULL, NULL);
 	}
 	_vte_terminal_set_tabstop(terminal,
-                                 terminal->pvt->cursor.col);
+                                  terminal->pvt->screen->cursor.col);
 }
 
 /* Tab. */
@@ -1801,7 +1801,7 @@ vte_sequence_handler_tab (VteTerminal *terminal, GValueArray *params)
 	long old_len, newcol, col;
 
 	/* Calculate which column is the next tab stop. */
-        newcol = col = terminal->pvt->cursor.col;
+        newcol = col = terminal->pvt->screen->cursor.col;
 
 	g_assert (col >= 0);
 
@@ -1873,10 +1873,10 @@ vte_sequence_handler_tab (VteTerminal *terminal, GValueArray *params)
 		}
 
 		_vte_invalidate_cells (terminal,
-                                terminal->pvt->cursor.col,
-                                newcol - terminal->pvt->cursor.col,
-                                terminal->pvt->cursor.row, 1);
-                terminal->pvt->cursor.col = newcol;
+                                       terminal->pvt->screen->cursor.col,
+                                       newcol - terminal->pvt->screen->cursor.col,
+                                       terminal->pvt->screen->cursor.row, 1);
+                terminal->pvt->screen->cursor.col = newcol;
 	}
 }
 
@@ -1901,7 +1901,7 @@ vte_sequence_handler_tab_clear (VteTerminal *terminal, GValueArray *params)
 	}
 	if (param == 0) {
 		_vte_terminal_clear_tabstop(terminal,
-                                           terminal->pvt->cursor.col);
+                                            terminal->pvt->screen->cursor.col);
 	} else
 	if (param == 3) {
 		if (terminal->pvt->tabstops != NULL) {
@@ -1939,7 +1939,7 @@ vte_sequence_handler_cursor_up (VteTerminal *terminal, GValueArray *params)
                 }
         }
 
-        terminal->pvt->cursor.row = MAX(terminal->pvt->cursor.row - val, start);
+        screen->cursor.row = MAX(screen->cursor.row - val, start);
 }
 
 /* Vertical tab. */
@@ -2593,7 +2593,7 @@ vte_sequence_handler_insert_lines (VteTerminal *terminal, GValueArray *params)
 		}
 	}
 	/* Find the region we're messing with. */
-        row = terminal->pvt->cursor.row;
+        row = screen->cursor.row;
         if (terminal->pvt->scrolling_restricted) {
                 end = screen->insert_delta + terminal->pvt->scrolling_region.end;
 	} else {
@@ -2612,7 +2612,7 @@ vte_sequence_handler_insert_lines (VteTerminal *terminal, GValueArray *params)
 		_vte_terminal_ring_remove (terminal, end);
 		_vte_terminal_ring_insert (terminal, row, TRUE);
 	}
-        terminal->pvt->cursor.col = 0;
+        screen->cursor.col = 0;
 	/* Update the display. */
 	_vte_terminal_scroll_region(terminal, row, end - row + 1, param);
 	/* Adjust the scrollbars if necessary. */
@@ -2640,7 +2640,7 @@ vte_sequence_handler_delete_lines (VteTerminal *terminal, GValueArray *params)
 		}
 	}
 	/* Find the region we're messing with. */
-        row = terminal->pvt->cursor.row;
+        row = screen->cursor.row;
         if (terminal->pvt->scrolling_restricted) {
                 end = screen->insert_delta + terminal->pvt->scrolling_region.end;
 	} else {
@@ -2660,7 +2660,7 @@ vte_sequence_handler_delete_lines (VteTerminal *terminal, GValueArray *params)
 		_vte_terminal_ring_remove (terminal, row);
 		_vte_terminal_ring_insert (terminal, end, TRUE);
 	}
-        terminal->pvt->cursor.col = 0;
+        screen->cursor.col = 0;
 	/* Update the display. */
 	_vte_terminal_scroll_region(terminal, row, end - row + 1, -param);
 	/* Adjust the scrollbars if necessary. */
@@ -2700,12 +2700,12 @@ vte_sequence_handler_device_status_report (VteTerminal *terminal, GValueArray *p
                                         origin = 0;
                                         rowmax = terminal->pvt->row_count - 1;
                                 }
-                                rowval = terminal->pvt->cursor.row - screen->insert_delta - origin;
+                                rowval = screen->cursor.row - screen->insert_delta - origin;
                                 rowval = CLAMP(rowval, 0, rowmax);
 				g_snprintf(buf, sizeof(buf),
 					   _VTE_CAP_CSI "%ld;%ldR",
                                            rowval + 1,
-                                           CLAMP(terminal->pvt->cursor.col + 1,
+                                           CLAMP(screen->cursor.col + 1,
                                                  1, terminal->pvt->column_count));
 				vte_terminal_feed_child(terminal, buf, -1);
 				break;
@@ -2742,12 +2742,12 @@ vte_sequence_handler_dec_device_status_report (VteTerminal *terminal, GValueArra
                                         origin = 0;
                                         rowmax = terminal->pvt->row_count - 1;
                                 }
-                                rowval = terminal->pvt->cursor.row - screen->insert_delta - origin;
+                                rowval = screen->cursor.row - screen->insert_delta - origin;
                                 rowval = CLAMP(rowval, 0, rowmax);
 				g_snprintf(buf, sizeof(buf),
 					   _VTE_CAP_CSI "?%ld;%ldR",
                                            rowval + 1,
-                                           CLAMP(terminal->pvt->cursor.col + 1,
+                                           CLAMP(screen->cursor.col + 1,
                                                  1, terminal->pvt->column_count));
 				vte_terminal_feed_child(terminal, buf, -1);
 				break;
-- 
2.5.0