File: screen.c

package info (click to toggle)
rxvt 1:2.7.10-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,068 kB
  • ctags: 4,415
  • sloc: ansic: 23,997; sh: 6,606; makefile: 649; perl: 33
file content (3777 lines) | stat: -rw-r--r-- 112,157 bytes parent folder | download | duplicates (2)
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
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
/*--------------------------------*-C-*--------------------------------------*
 * File:	screen.c
 *---------------------------------------------------------------------------*
 * $Id: screen.c,v 1.258 2003/03/23 16:55:50 gcw Exp $
 *
 * Copyright (c) 1997-2001 Geoff Wing <gcw@pobox.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *--------------------------------------------------------------------------*/
/*
 * We handle _all_ screen updates and selections
 */

#include "../config.h"		/* NECESSARY */
#define INTERN_SCREEN
#include "rxvt.h"		/* NECESSARY */
#include "screen.intpro"	/* PROTOS for internal routines */

/* ------------------------------------------------------------------------- */
#ifdef MULTICHAR_SET

#define RESET_CHSTAT(H)				\
    if ((H)->chstat == WBYTE)			\
	(H)->chstat = SBYTE, (H)->lost_multi = 1

const KNOWN_ENCODINGS known_encodings[] = {
    { SJIS, rxvt_sjis2jis, "sjis" },
    { EUCJ, rxvt_euc2jis, "eucj" },
    { GB, rxvt_euc2jis, "gb" },
    { BIG5, rxvt_decodedummy, "big5" },
    { EUCKR, rxvt_euc2jis, "kr" },
    { NOENC, rxvt_decodedummy, "noenc" },
    { 0, NULL, NULL }
};
#else
# define RESET_CHSTAT(H)
#endif

/* ------------------------------------------------------------------------- */
#define PROP_SIZE		16384
#define TABSIZE			8	/* default tab size */

/* ------------------------------------------------------------------------- *
 *             GENERAL SCREEN AND SELECTION UPDATE ROUTINES                  *
 * ------------------------------------------------------------------------- */
#define ZERO_SCROLLBACK(R)						\
    if (((R)->Options & Opt_scrollTtyOutput) == Opt_scrollTtyOutput)	\
	(R)->TermWin.view_start = 0
#define CLEAR_SELECTION(R)						\
    (R)->selection.beg.row = (R)->selection.beg.col			\
	= (R)->selection.end.row = (R)->selection.end.col = 0
#define CLEAR_ALL_SELECTION(R)						\
    (R)->selection.beg.row = (R)->selection.beg.col			\
	= (R)->selection.mark.row = (R)->selection.mark.col		\
	= (R)->selection.end.row = (R)->selection.end.col = 0

#define ROW_AND_COL_IS_AFTER(A, B, C, D)				\
    (((A) > (C)) || (((A) == (C)) && ((B) > (D))))
#define ROW_AND_COL_IS_BEFORE(A, B, C, D)				\
    (((A) < (C)) || (((A) == (C)) && ((B) < (D))))
#define ROW_AND_COL_IN_ROW_AFTER(A, B, C, D)				\
    (((A) == (C)) && ((B) > (D)))
#define ROW_AND_COL_IN_ROW_AT_OR_AFTER(A, B, C, D)			\
    (((A) == (C)) && ((B) >= (D)))
#define ROW_AND_COL_IN_ROW_BEFORE(A, B, C, D)				\
    (((A) == (C)) && ((B) < (D)))
#define ROW_AND_COL_IN_ROW_AT_OR_BEFORE(A, B, C, D)			\
    (((A) == (C)) && ((B) <= (D)))

/* these must be row_col_t */
#define ROWCOL_IS_AFTER(X, Y)						\
    ROW_AND_COL_IS_AFTER((X).row, (X).col, (Y).row, (Y).col)
#define ROWCOL_IS_BEFORE(X, Y)						\
    ROW_AND_COL_IS_BEFORE((X).row, (X).col, (Y).row, (Y).col)
#define ROWCOL_IN_ROW_AFTER(X, Y)					\
    ROW_AND_COL_IN_ROW_AFTER((X).row, (X).col, (Y).row, (Y).col)
#define ROWCOL_IN_ROW_BEFORE(X, Y)					\
    ROW_AND_COL_IN_ROW_BEFORE((X).row, (X).col, (Y).row, (Y).col)
#define ROWCOL_IN_ROW_AT_OR_AFTER(X, Y)					\
    ROW_AND_COL_IN_ROW_AT_OR_AFTER((X).row, (X).col, (Y).row, (Y).col)
#define ROWCOL_IN_ROW_AT_OR_BEFORE(X, Y)				\
    ROW_AND_COL_IN_ROW_AT_OR_BEFORE((X).row, (X).col, (Y).row, (Y).col)

/*
 * CLEAR_ROWS : clear <num> rows starting from row <row>
 * CLEAR_CHARS: clear <num> chars starting from pixel position <x,y>
 * ERASE_ROWS : set <num> rows starting from row <row> to the foreground colour
 */
#define drawBuffer	(r->TermWin.vt)

#define CLEAR_ROWS(row, num)						\
    if (r->TermWin.mapped)						\
	XClearArea(r->Xdisplay, drawBuffer, r->TermWin.int_bwidth,	\
		   Row2Pixel(row), (unsigned int)r->TermWin.width,	\
		   (unsigned int)Height2Pixel(num), False)

#define CLEAR_CHARS(x, y, num)						\
    if (r->TermWin.mapped)						\
	XClearArea(r->Xdisplay, drawBuffer, x, y,			\
		   (unsigned int)Width2Pixel(num),			\
		   (unsigned int)Height2Pixel(1), False)

#define ERASE_ROWS(row, num)						\
    XFillRectangle(r->Xdisplay, drawBuffer, r->TermWin.gc,		\
		   r->TermWin.int_bwidth, Row2Pixel(row),		\
		   (unsigned int)r->TermWin.width,			\
		   (unsigned int)Height2Pixel(num))

/* ------------------------------------------------------------------------- *
 *                        SCREEN `COMMON' ROUTINES                           *
 * ------------------------------------------------------------------------- */
/* Fill part/all of a line with blanks. */
/* INTPROTO */
void
rxvt_blank_line(text_t *et, rend_t *er, unsigned int width, rend_t efs)
{
    MEMSET(et, ' ', (size_t)width);
    efs &= ~RS_baseattrMask;
    for (; width--;)
	*er++ = efs;
}

/* ------------------------------------------------------------------------- */
/* Fill a full line with blanks - make sure it is allocated first */
/* INTPROTO */
void
rxvt_blank_screen_mem(rxvt_t *r, text_t **tp, rend_t **rp, unsigned int row, rend_t efs)
{
    int             width = r->TermWin.ncol;
    rend_t         *er;

#ifdef DEBUG_STRICT
    assert((tp[row] && rp[row]) || (tp[row] == NULL && rp[row] == NULL));
#endif
    if (tp[row] == NULL) {
	tp[row] = rxvt_malloc(sizeof(text_t) * width);
	rp[row] = rxvt_malloc(sizeof(rend_t) * width);
    }
    MEMSET(tp[row], ' ', width);
    efs &= ~RS_baseattrMask;
    for (er = rp[row]; width--;)
	*er++ = efs;
}

/* ------------------------------------------------------------------------- *
 *                          SCREEN INITIALISATION                            *
 * ------------------------------------------------------------------------- */
/* EXTPROTO */
void
rxvt_scr_reset(rxvt_t *r)
{
    unsigned int    ncol, nrow, prev_ncol, prev_nrow,
		    total_rows, prev_total_rows;
    unsigned int    p, q;
    int             k;
    rend_t          setrstyle;

    D_SCREEN((stderr, "rxvt_scr_reset()"));

    r->TermWin.view_start = 0;
    RESET_CHSTAT(r->h);
    r->h->num_scr = 0;

    prev_ncol = r->h->prev_ncol;
    prev_nrow = r->h->prev_nrow;
    if (r->TermWin.ncol == 0)
	r->TermWin.ncol = 80;
    if (r->TermWin.nrow == 0)
	r->TermWin.nrow = 24;
    ncol = r->TermWin.ncol;
    nrow = r->TermWin.nrow;
    if (ncol == prev_ncol && nrow == prev_nrow)
	return;

    r->h->want_refresh = 1;

    total_rows = nrow + r->TermWin.saveLines;
    prev_total_rows = prev_nrow + r->TermWin.saveLines;

    r->screen.tscroll = 0;
    r->screen.bscroll = nrow - 1;

    if (prev_nrow == 0) {
/*
 * A: first time called so just malloc everything : don't rely on realloc
 *    Note: this is still needed so that all the scrollback lines are NULL
 */
	r->screen.text = rxvt_calloc(total_rows, sizeof(text_t *));
	r->buf_text = rxvt_calloc(total_rows, sizeof(text_t *));
	r->drawn_text = rxvt_calloc(nrow, sizeof(text_t *));
	r->swap.text = rxvt_calloc(nrow, sizeof(text_t *));

	r->screen.tlen = rxvt_calloc(total_rows, sizeof(int16_t));
	r->swap.tlen = rxvt_calloc(nrow, sizeof(int16_t));

	r->screen.rend = rxvt_calloc(total_rows, sizeof(rend_t *));
	r->buf_rend = rxvt_calloc(total_rows, sizeof(rend_t *));
	r->drawn_rend = rxvt_calloc(nrow, sizeof(rend_t *));
	r->swap.rend = rxvt_calloc(nrow, sizeof(rend_t *));

	for (p = 0; p < nrow; p++) {
	    q = p + r->TermWin.saveLines;
	    rxvt_blank_screen_mem(r, r->screen.text, r->screen.rend,
				  q, DEFAULT_RSTYLE);
	    rxvt_blank_screen_mem(r, r->swap.text, r->swap.rend,
				  p, DEFAULT_RSTYLE);
	    r->screen.tlen[q] = r->swap.tlen[p] = 0;
	    rxvt_blank_screen_mem(r, r->drawn_text, r->drawn_rend,
				  p, DEFAULT_RSTYLE);
	}
	MEMSET(r->h->charsets, 'B', sizeof(r->h->charsets));
	r->TermWin.nscrolled = 0;	/* no saved lines */
	r->h->rstyle = DEFAULT_RSTYLE;
	r->screen.flags = Screen_DefaultFlags;
	r->screen.cur.row = r->screen.cur.col = 0;
	r->screen.charset = 0;
	r->h->current_screen = PRIMARY;
	rxvt_scr_cursor(r, SAVE);
#if NSCREENS
	r->swap.flags = Screen_DefaultFlags;
	r->swap.cur.row = r->swap.cur.col = 0;
	r->swap.charset = 0;
	r->h->current_screen = SECONDARY;
	rxvt_scr_cursor(r, SAVE);
	r->h->current_screen = PRIMARY;
#endif
	r->selection.text = NULL;
	r->selection.len = 0;
	r->selection.op = SELECTION_CLEAR;
	r->selection.screen = PRIMARY;
	r->selection.clicks = 0;
	CLEAR_ALL_SELECTION(r);
	r->h->rvideo = 0;
#ifdef MULTICHAR_SET
	r->h->multi_byte = 0;
	r->h->lost_multi = 0;
	r->h->chstat = SBYTE;
#endif

    } else {
/*
 * B1: add or delete rows as appropriate
 */
	setrstyle = DEFAULT_RSTYLE;

	if (nrow < prev_nrow) {
	    /* delete rows */
	    k = min(r->TermWin.nscrolled, prev_nrow - nrow);
	    rxvt_scroll_text(r, 0, (int)prev_nrow - 1, k, 1);
	    for (p = nrow; p < prev_nrow; p++) {
		q = p + r->TermWin.saveLines;
		if (r->screen.text[q]) {
#ifdef DEBUG_STRICT
		    assert(r->screen.rend[q]);
#endif
		    free(r->screen.text[q]);
		    free(r->screen.rend[q]);
		}
		if (r->swap.text[p]) {
#ifdef DEBUG_STRICT
		    assert(r->swap.rend[p]);
#endif
		    free(r->swap.text[p]);
		    free(r->swap.rend[p]);
		}
#ifdef DEBUG_STRICT
		assert(r->drawn_text[p] && r->drawn_rend[p]);
#endif
		free(r->drawn_text[p]);
		free(r->drawn_rend[p]);
	    }
	    /* we have fewer rows so fix up cursor position */
	    MIN_IT(r->screen.cur.row, (int32_t)nrow - 1);
	    MIN_IT(r->swap.cur.row, (int32_t)nrow - 1);

	    rxvt_scr_reset_realloc(r);	/* realloc _last_ */

	} else if (nrow > prev_nrow) {
	    /* add rows */
	    rxvt_scr_reset_realloc(r);	/* realloc _first_ */

	    k = min(r->TermWin.nscrolled, nrow - prev_nrow);
	    for (p = prev_total_rows; p < total_rows; p++) {
		r->screen.tlen[p] = 0;
		r->screen.text[p] = NULL;
		r->screen.rend[p] = NULL;
	    }
	    for (p = prev_total_rows; p < total_rows - k; p++)
		rxvt_blank_screen_mem(r, r->screen.text, r->screen.rend,
				      p, setrstyle);
	    for (p = prev_nrow; p < nrow; p++) {
		r->swap.tlen[p] = 0;
		r->swap.text[p] = NULL;
		r->swap.rend[p] = NULL;
		r->drawn_text[p] = NULL;
		r->drawn_rend[p] = NULL;
		rxvt_blank_screen_mem(r, r->swap.text, r->swap.rend,
				      p, setrstyle);
		rxvt_blank_screen_mem(r, r->drawn_text, r->drawn_rend,
				      p, setrstyle);
	    }
	    if (k > 0) {
		rxvt_scroll_text(r, 0, (int)nrow - 1, -k, 1);
		r->screen.cur.row += k;
		r->screen.s_cur.row += k;
		r->TermWin.nscrolled -= k;
	    }
#ifdef DEBUG_STRICT
	    assert(r->screen.cur.row < r->TermWin.nrow);
	    assert(r->swap.cur.row < r->TermWin.nrow);
#else				/* drive with your eyes closed */
	    MIN_IT(r->screen.cur.row, nrow - 1);
	    MIN_IT(r->swap.cur.row, nrow - 1);
#endif
	}
/* B2: resize columns */
	if (ncol != prev_ncol) {
	    for (p = 0; p < total_rows; p++) {
		if (r->screen.text[p]) {
		    r->screen.text[p] = rxvt_realloc(r->screen.text[p],
						     ncol * sizeof(text_t));
		    r->screen.rend[p] = rxvt_realloc(r->screen.rend[p],
						     ncol * sizeof(rend_t));
		    MIN_IT(r->screen.tlen[p], (int16_t)ncol);
		    if (ncol > prev_ncol)
			rxvt_blank_line(&(r->screen.text[p][prev_ncol]),
					&(r->screen.rend[p][prev_ncol]),
					ncol - prev_ncol,
					setrstyle);
		}
	    }
	    for (p = 0; p < nrow; p++) {
		r->drawn_text[p] = rxvt_realloc(r->drawn_text[p],
						ncol * sizeof(text_t));
		r->drawn_rend[p] = rxvt_realloc(r->drawn_rend[p],
						ncol * sizeof(rend_t));
		if (r->swap.text[p]) {
		    r->swap.text[p] = rxvt_realloc(r->swap.text[p],
						   ncol * sizeof(text_t));
		    r->swap.rend[p] = rxvt_realloc(r->swap.rend[p],
						   ncol * sizeof(rend_t));
		    MIN_IT(r->swap.tlen[p], (int16_t)ncol);
		    if (ncol > prev_ncol)
			rxvt_blank_line(&(r->swap.text[p][prev_ncol]),
					&(r->swap.rend[p][prev_ncol]),
					ncol - prev_ncol, setrstyle);
		}
		if (ncol > prev_ncol)
		    rxvt_blank_line(&(r->drawn_text[p][prev_ncol]),
				    &(r->drawn_rend[p][prev_ncol]),
				    ncol - prev_ncol, setrstyle);
	    }
	    MIN_IT(r->screen.cur.col, (int16_t)ncol - 1);
	    MIN_IT(r->swap.cur.col, (int16_t)ncol - 1);
	}
	if (r->tabs)
	    free(r->tabs);
    }

    r->tabs = rxvt_malloc(ncol * sizeof(char));

    for (p = 0; p < ncol; p++)
	r->tabs[p] = (p % TABSIZE == 0) ? 1 : 0;

    r->h->prev_nrow = nrow;
    r->h->prev_ncol = ncol;

    rxvt_tt_winsize(r->cmd_fd, r->TermWin.ncol, r->TermWin.nrow);
}

/* INTPROTO */
void
rxvt_scr_reset_realloc(rxvt_t *r)
{
    u_int16_t       total_rows, nrow;

    nrow = r->TermWin.nrow;
    total_rows = nrow + r->TermWin.saveLines;
/* *INDENT-OFF* */
    r->screen.text = rxvt_realloc(r->screen.text, total_rows * sizeof(text_t *));
    r->buf_text    = rxvt_realloc(r->buf_text   , total_rows * sizeof(text_t *));
    r->drawn_text  = rxvt_realloc(r->drawn_text , nrow       * sizeof(text_t *));
    r->swap.text   = rxvt_realloc(r->swap.text  , nrow       * sizeof(text_t *));

    r->screen.tlen = rxvt_realloc(r->screen.tlen, total_rows * sizeof(int16_t));
    r->swap.tlen   = rxvt_realloc(r->swap.tlen  , total_rows * sizeof(int16_t));

    r->screen.rend = rxvt_realloc(r->screen.rend, total_rows * sizeof(rend_t *));
    r->buf_rend    = rxvt_realloc(r->buf_rend   , total_rows * sizeof(rend_t *));
    r->drawn_rend  = rxvt_realloc(r->drawn_rend , nrow       * sizeof(rend_t *));
    r->swap.rend   = rxvt_realloc(r->swap.rend  , nrow       * sizeof(rend_t *));
/* *INDENT-ON* */
}

/* ------------------------------------------------------------------------- */
/*
 * Free everything.  That way malloc debugging can find leakage.
 */
/* EXTPROTO */
void
rxvt_scr_release(rxvt_t *r)
{
    u_int16_t       total_rows;
    int             i;

    total_rows = r->TermWin.nrow + r->TermWin.saveLines;
    for (i = 0; i < total_rows; i++) {
	if (r->screen.text[i]) {	/* then so is r->screen.rend[i] */
	    free(r->screen.text[i]);
#ifdef DEBUG_STRICT
	    assert(r->screen.rend[i]);
#endif
	    free(r->screen.rend[i]);
	}
    }
    for (i = 0; i < r->TermWin.nrow; i++) {
	free(r->drawn_text[i]);
	free(r->drawn_rend[i]);
	free(r->swap.text[i]);
	free(r->swap.rend[i]);
    }
    free(r->screen.text);
    free(r->screen.tlen);
    free(r->screen.rend);
    free(r->drawn_text);
    free(r->drawn_rend);
    free(r->swap.text);
    free(r->swap.tlen);
    free(r->swap.rend);
    free(r->buf_text);
    free(r->buf_rend);
    free(r->tabs);

/* NULL these so if anything tries to use them, we'll know about it */
    r->screen.text = r->drawn_text = r->swap.text = NULL;
    r->screen.rend = r->drawn_rend = r->swap.rend = NULL;
    r->screen.tlen = r->swap.tlen = NULL;
    r->buf_text = NULL;
    r->buf_rend = NULL;
    r->tabs = NULL;
}

/* ------------------------------------------------------------------------- */
/*
 * Hard reset
 */
/* EXTPROTO */
void
rxvt_scr_poweron(rxvt_t *r)
{
    D_SCREEN((stderr, "rxvt_scr_poweron()"));

    rxvt_scr_release(r);
    r->h->prev_nrow = r->h->prev_ncol = 0;
    rxvt_scr_reset(r);

    rxvt_scr_clear(r);
    rxvt_scr_refresh(r, SLOW_REFRESH);
    rxvt_Gr_reset(r);
}

/* ------------------------------------------------------------------------- *
 *                         PROCESS SCREEN COMMANDS                           *
 * ------------------------------------------------------------------------- */
/*
 * Save and Restore cursor
 * XTERM_SEQ: Save cursor   : ESC 7
 * XTERM_SEQ: Restore cursor: ESC 8
 */
/* EXTPROTO */
void
rxvt_scr_cursor(rxvt_t *r, int mode)
{
    screen_t       *s;

    D_SCREEN((stderr, "rxvt_scr_cursor(%c)", mode));

#if NSCREENS && !defined(NO_SECONDARY_SCREEN_CURSOR)
    if (r->h->current_screen == SECONDARY)
	s = &(r->swap);
    else
#endif
	s = &(r->screen);
    switch (mode) {
    case SAVE:
	s->s_cur.row = s->cur.row;
	s->s_cur.col = s->cur.col;
	s->s_rstyle = r->h->rstyle;
	s->s_charset = s->charset;
	s->s_charset_char = r->h->charsets[s->charset];
	break;
    case RESTORE:
	r->h->want_refresh = 1;
	s->cur.row = s->s_cur.row;
	s->cur.col = s->s_cur.col;
	s->flags &= ~Screen_WrapNext;
	r->h->rstyle = s->s_rstyle;
	s->charset = s->s_charset;
	r->h->charsets[s->charset] = s->s_charset_char;
	rxvt_set_font_style(r);
	break;
    }
/* boundary check in case screen size changed between SAVE and RESTORE */
    MIN_IT(s->cur.row, r->TermWin.nrow - 1);
    MIN_IT(s->cur.col, r->TermWin.ncol - 1);
#ifdef DEBUG_STRICT
    assert(s->cur.row >= 0);
    assert(s->cur.col >= 0);
#else				/* drive with your eyes closed */
    MAX_IT(s->cur.row, 0);
    MAX_IT(s->cur.col, 0);
#endif
}

/* ------------------------------------------------------------------------- */
/*
 * Swap between primary and secondary screens
 * XTERM_SEQ: Primary screen  : ESC [ ? 4 7 h
 * XTERM_SEQ: Secondary screen: ESC [ ? 4 7 l
 */
/* EXTPROTO */
int
rxvt_scr_change_screen(rxvt_t *r, int scrn)
{
    int             i;
#if NSCREENS
    int             offset;
#endif

    r->h->want_refresh = 1;

    D_SCREEN((stderr, "rxvt_scr_change_screen(%d)", scrn));

    r->TermWin.view_start = 0;
    RESET_CHSTAT(r->h);

    if (r->h->current_screen == scrn)
	return r->h->current_screen;

    rxvt_selection_check(r, 2);	/* check for boundary cross */

    SWAP_IT(r->h->current_screen, scrn, int);
#if NSCREENS
    r->h->num_scr = 0;
    offset = r->TermWin.saveLines;
    for (i = r->h->prev_nrow; i--;) {
	SWAP_IT(r->screen.text[i + offset], r->swap.text[i], text_t *);
	SWAP_IT(r->screen.tlen[i + offset], r->swap.tlen[i], int16_t);
	SWAP_IT(r->screen.rend[i + offset], r->swap.rend[i], rend_t *);
    }
    SWAP_IT(r->screen.cur.row, r->swap.cur.row, int16_t);
    SWAP_IT(r->screen.cur.col, r->swap.cur.col, int16_t);
# ifdef DEBUG_STRICT
    assert((r->screen.cur.row >= 0) && (r->screen.cur.row < r->h->prev_nrow));
    assert((r->screen.cur.col >= 0) && (r->screen.cur.col < r->h->prev_ncol));
# else				/* drive with your eyes closed */
    MAX_IT(r->screen.cur.row, 0);
    MIN_IT(r->screen.cur.row, r->h->prev_nrow - 1);
    MAX_IT(r->screen.cur.col, 0);
    MIN_IT(r->screen.cur.col, r->h->prev_ncol - 1);
# endif
    SWAP_IT(r->screen.charset, r->swap.charset, int16_t);
    SWAP_IT(r->screen.flags, r->swap.flags, int);
    r->screen.flags |= Screen_VisibleCursor;
    r->swap.flags |= Screen_VisibleCursor;

    if (rxvt_Gr_Displayed(r)) {
	rxvt_Gr_scroll(r, 0);
	rxvt_Gr_ChangeScreen(r);
    }
#else
# ifdef SCROLL_ON_NO_SECONDARY
    if (rxvt_Gr_Displayed(r))
	rxvt_Gr_ClearScreen(r);
    if (r->h->current_screen == PRIMARY && !rxvt_Gr_Displayed(r))
	rxvt_scroll_text(r, 0, (r->h->prev_nrow - 1), r->h->prev_nrow, 0);
# endif
#endif
    return scrn;
}

/* ------------------------------------------------------------------------- */
/*
 * Change the colour for following text
 */
/* EXTPROTO */
void
rxvt_scr_color(rxvt_t *r, unsigned int color, int fgbg)
{
    color &= RS_fgMask;
    if (fgbg == Color_fg)
	r->h->rstyle = SET_FGCOLOR(r->h->rstyle, color);
    else 
	r->h->rstyle = SET_BGCOLOR(r->h->rstyle, color);
}

/* ------------------------------------------------------------------------- */
/*
 * Change the rendition style for following text
 */
/* EXTPROTO */
void
rxvt_scr_rendition(rxvt_t *r, int set, int style)
{
    if (set)
	r->h->rstyle |= style;
    else if (style == ~RS_None)
	r->h->rstyle = DEFAULT_RSTYLE | (r->h->rstyle & RS_fontMask);
    else
	r->h->rstyle &= ~style;
}

/* ------------------------------------------------------------------------- */
/*
 * Scroll text between <row1> and <row2> inclusive, by <count> lines
 * count positive ==> scroll up
 * count negative ==> scroll down
 * spec == 0 for normal routines
 */
/* EXTPROTO */
int
rxvt_scroll_text(rxvt_t *r, int row1, int row2, int count, int spec)
{
    int             i, j;
    long            nscrolled;

    if (count == 0 || (row1 > row2))
	return 0;

    r->h->want_refresh = 1;
    D_SCREEN((stderr, "rxvt_scroll_text(%d,%d,%d,%d): %s", row1, row2, count, spec, (r->h->current_screen == PRIMARY) ? "Primary" : "Secondary"));

    if ((count > 0) && (row1 == 0) && (r->h->current_screen == PRIMARY)) {
	nscrolled = (long)r->TermWin.nscrolled + (long)count;;
	if (nscrolled > (long)r->TermWin.saveLines)
	    r->TermWin.nscrolled = r->TermWin.saveLines;
	else
	    r->TermWin.nscrolled = (u_int16_t)nscrolled;
	if ((r->Options & Opt_scrollWithBuffer)
	    && r->TermWin.view_start != 0
	    && r->TermWin.view_start != r->TermWin.saveLines)
	    rxvt_scr_page(r, UP, count);
    } else if (!spec)
	row1 += r->TermWin.saveLines;
    row2 += r->TermWin.saveLines;

    if (r->selection.op && r->h->current_screen == r->selection.screen) {
	i = r->selection.beg.row + r->TermWin.saveLines;
	j = r->selection.end.row + r->TermWin.saveLines;
	if ((i < row1 && j > row1)
	    || (i < row2 && j > row2)
	    || (i - count < row1 && i >= row1)
	    || (i - count > row2 && i <= row2)
	    || (j - count < row1 && j >= row1)
	    || (j - count > row2 && j <= row2)) {
	    CLEAR_ALL_SELECTION(r);
	    r->selection.op = SELECTION_CLEAR;	/* XXX: too aggressive? */
	} else if (j >= row1 && j <= row2) {
	    /* move selected region too */
	    r->selection.beg.row -= count;
	    r->selection.end.row -= count;
	    r->selection.mark.row -= count;
	}
    }
    rxvt_selection_check(r, 0);	/* _after_ r->TermWin.nscrolled update */

    r->h->num_scr += count;
    j = count;
    if (count < 0)
	count = -count;
    i = row2 - row1 + 1;
    MIN_IT(count, i);

    if (j > 0) {
/* A: scroll up */

/* A1: Copy lines that will get clobbered by the rotation */
	for (i = 0, j = row1; i < count; i++, j++) {
	    r->buf_text[i] = r->screen.text[j];
	    r->buf_rend[i] = r->screen.rend[j];
	}
/* A2: Rotate lines */
	for (j = row1, i = j + count; i <= row2; i++, j++) {
	    r->screen.tlen[j] = r->screen.tlen[i];
	    r->screen.text[j] = r->screen.text[i];
	    r->screen.rend[j] = r->screen.rend[i];
	}
	j = row2 - count + 1, i = count;
    } else /* if (j < 0) */ {
/* B: scroll down */

/* B1: Copy lines that will get clobbered by the rotation */
	for (i = 0, j = row2; i < count; i++, j--) {
	    r->buf_text[i] = r->screen.text[j];
	    r->buf_rend[i] = r->screen.rend[j];
	}
/* B2: Rotate lines */
	for (j = row2, i = j - count; i >= row1; i--, j--) {
	    r->screen.tlen[j] = r->screen.tlen[i];
	    r->screen.text[j] = r->screen.text[i];
	    r->screen.rend[j] = r->screen.rend[i];
	}
	j = row1, i = count;
	count = -count;
    }

/* C: Resurrect lines */
    for (; i--; j++) {
	r->screen.tlen[j] = 0;
	r->screen.text[j] = r->buf_text[i];
	r->screen.rend[j] = r->buf_rend[i];
	if (!spec)		/* line length may not equal TermWin.ncol */
	    rxvt_blank_screen_mem(r, r->screen.text, r->screen.rend,
				  (unsigned int)j, r->h->rstyle);
    }

    if (rxvt_Gr_Displayed(r))
	rxvt_Gr_scroll(r, count);
    return count;
}

/* ------------------------------------------------------------------------- */
/*
 * Add text given in <str> of length <len> to screen struct
 */
/* EXTPROTO */
void
rxvt_scr_add_lines(rxvt_t *r, const unsigned char *str, int nlines, int len)
{
    unsigned char   checksel, clearsel;
    char            c;
    int             i, row, last_col;
    text_t         *stp;
    rend_t         *srp;
    struct rxvt_hidden *h = r->h;

    if (len <= 0)		/* sanity */
	return;

    h->want_refresh = 1;
    last_col = r->TermWin.ncol;

    D_SCREEN((stderr, "rxvt_scr_add_lines(%d,%d)", nlines, len));
    ZERO_SCROLLBACK(r);
    if (nlines > 0) {
	nlines += (r->screen.cur.row - r->screen.bscroll);
	if ((nlines > 0)
	    && (r->screen.tscroll == 0)
	    && (r->screen.bscroll == (r->TermWin.nrow - 1))) {
	    /* _at least_ this many lines need to be scrolled */
	    rxvt_scroll_text(r, r->screen.tscroll, r->screen.bscroll, nlines,
			     0);
	    r->screen.cur.row -= nlines;
	}
    }
#ifdef DEBUG_STRICT
    assert(r->screen.cur.col < last_col);
    assert((r->screen.cur.row < r->TermWin.nrow)
	   && (r->screen.cur.row >= -(int32_t)r->TermWin.nscrolled));
#else				/* drive with your eyes closed */
    MIN_IT(r->screen.cur.col, last_col - 1);
    MIN_IT(r->screen.cur.row, r->TermWin.nrow - 1);
    MAX_IT(r->screen.cur.row, -(int32_t)r->TermWin.nscrolled);
#endif
    row = r->screen.cur.row + r->TermWin.saveLines;

    checksel = (r->selection.op
		&& h->current_screen == r->selection.screen) ? 1 : 0;
    clearsel = 0;

    stp = r->screen.text[row];
    srp = r->screen.rend[row];

#ifdef MULTICHAR_SET
    if (h->lost_multi && r->screen.cur.col > 0
	&& IS_MULTI1(srp[r->screen.cur.col - 1])
	&& *str != '\n' && *str != '\r' && *str != '\t')
	h->chstat = WBYTE;
#endif

    for (i = 0; i < len;) {
	c = str[i++];
	switch (c) {
	case '\t':
	    rxvt_scr_tab(r, 1);
	    continue;
	case '\n':
	    if (r->screen.tlen[row] != -1)	/* XXX: think about this */
		MAX_IT(r->screen.tlen[row], r->screen.cur.col);
	    r->screen.flags &= ~Screen_WrapNext;
	    if (r->screen.cur.row == r->screen.bscroll)
		rxvt_scroll_text(r, r->screen.tscroll, r->screen.bscroll, 1, 0);
	    else if (r->screen.cur.row < (r->TermWin.nrow - 1))
		row = (++r->screen.cur.row) + r->TermWin.saveLines;
	    stp = r->screen.text[row];	/* _must_ refresh */
	    srp = r->screen.rend[row];	/* _must_ refresh */
	    RESET_CHSTAT(h);
	    continue;
	case '\r':
	    if (r->screen.tlen[row] != -1)	/* XXX: think about this */
		MAX_IT(r->screen.tlen[row], r->screen.cur.col);
	    r->screen.flags &= ~Screen_WrapNext;
	    r->screen.cur.col = 0;
	    RESET_CHSTAT(h);
	    continue;
	default:
#ifdef MULTICHAR_SET
	    if (r->encoding_method == NOENC) {
		if (c == 127)
		    continue;
		break;
	    }
	    h->rstyle &= ~RS_multiMask;
	    if (h->chstat == WBYTE) {
		h->rstyle |= RS_multi2;	/* multibyte 2nd byte */
		h->chstat = SBYTE;
		if ((r->encoding_method == EUCJ) || (r->encoding_method == GB))
		    c |= 0x80;	/* maybe overkill, but makes it selectable */
	    } else if (h->chstat == SBYTE) {
		if (h->multi_byte || (c & 0x80)) {	/* multibyte 1st byte */
		    h->rstyle |= RS_multi1;
		    h->chstat = WBYTE;
		    if ((r->encoding_method == EUCJ)
			|| (r->encoding_method == GB))
			c |= 0x80;	/* maybe overkill, but makes selectable */
		}
	    } else
#endif
	    if (c == 127)
		continue;	/* yummmm..... */
	    break;
	}

	if (checksel		/* see if we're writing within selection */
	    && !ROWCOL_IS_BEFORE(r->screen.cur, r->selection.beg)
	    && ROWCOL_IS_BEFORE(r->screen.cur, r->selection.end)) {
	    checksel = 0;
	    clearsel = 1;
	}
	if (r->screen.flags & Screen_WrapNext) {
	    r->screen.tlen[row] = -1;
	    if (r->screen.cur.row == r->screen.bscroll)
		rxvt_scroll_text(r, r->screen.tscroll, r->screen.bscroll, 1, 0);
	    else if (r->screen.cur.row < (r->TermWin.nrow - 1))
		row = (++r->screen.cur.row) + r->TermWin.saveLines;
	    stp = r->screen.text[row];	/* _must_ refresh */
	    srp = r->screen.rend[row];	/* _must_ refresh */
	    r->screen.cur.col = 0;
	    r->screen.flags &= ~Screen_WrapNext;
	}
	if (r->screen.flags & Screen_Insert)
	    rxvt_scr_insdel_chars(r, 1, INSERT);
#ifdef MULTICHAR_SET
	if (IS_MULTI1(h->rstyle)
	    && r->screen.cur.col > 0 && IS_MULTI1(srp[r->screen.cur.col - 1])) {
	    stp[r->screen.cur.col - 1] = ' ';
	    srp[r->screen.cur.col - 1] &= ~RS_multiMask;
	} else if (IS_MULTI2(h->rstyle)
		   && r->screen.cur.col < (last_col - 1)
		   && IS_MULTI2(srp[r->screen.cur.col + 1])) {
	    stp[r->screen.cur.col + 1] = ' ';
	    srp[r->screen.cur.col + 1] &= ~RS_multiMask;
	}
#endif
	stp[r->screen.cur.col] = c;
	srp[r->screen.cur.col] = h->rstyle;
	if (r->screen.cur.col < (last_col - 1))
	    r->screen.cur.col++;
	else {
	    r->screen.tlen[row] = last_col;
	    if (r->screen.flags & Screen_Autowrap)
		r->screen.flags |= Screen_WrapNext;
	}
    }
    if (r->screen.tlen[row] != -1)	/* XXX: think about this */
	MAX_IT(r->screen.tlen[row], r->screen.cur.col);

/*
 * If we wrote anywhere in the selected area, kill the selection
 * XXX: should we kill the mark too?  Possibly, but maybe that
 *      should be a similar check.
 */
    if (clearsel)
	CLEAR_SELECTION(r);

#ifdef DEBUG_STRICT
    assert(r->screen.cur.row >= 0);
#else				/* drive with your eyes closed */
    MAX_IT(r->screen.cur.row, 0);
#endif
}

/* ------------------------------------------------------------------------- */
/*
 * Process Backspace.  Move back the cursor back a position, wrap if have to
 * XTERM_SEQ: CTRL-H
 */
/* EXTPROTO */
void
rxvt_scr_backspace(rxvt_t *r)
{
    RESET_CHSTAT(r->h);
    r->h->want_refresh = 1;
    if (r->screen.cur.col == 0) {
	if (r->screen.cur.row > 0) {
#ifdef TERMCAP_HAS_BW
	    r->screen.cur.col = r->TermWin.ncol - 1;
	    r->screen.cur.row--;
	    return;
#endif
	}
    } else if ((r->screen.flags & Screen_WrapNext) == 0)
	rxvt_scr_gotorc(r, 0, -1, RELATIVE);
    r->screen.flags &= ~Screen_WrapNext;
}

/* ------------------------------------------------------------------------- */
/*
 * Process Horizontal Tab
 * count: +ve = forward; -ve = backwards
 * XTERM_SEQ: CTRL-I
 */
/* EXTPROTO */
void
rxvt_scr_tab(rxvt_t *r, int count)
{
    int             i, x;

    D_SCREEN((stderr, "rxvt_scr_tab(%d)", count));
    r->h->want_refresh = 1;
    RESET_CHSTAT(r->h);
    i = x = r->screen.cur.col;
    if (count == 0)
	return;
    else if (count > 0) {
	for (; ++i < r->TermWin.ncol; )
	    if (r->tabs[i]) {
		x = i;
		if (!--count)
		    break;
	    }
	if (count)
	    x = r->TermWin.ncol - 1;
    } else /* if (count < 0) */ {
	for (; --i >= 0; )
	    if (r->tabs[i]) {
		x = i;
		if (!++count)
		    break;
	    }
	if (count)
	    x = 0;
    }
    if (x != r->screen.cur.col)
	rxvt_scr_gotorc(r, 0, x, R_RELATIVE);
}

/* ------------------------------------------------------------------------- */
/*
 * Process DEC Back Index
 * XTERM_SEQ: ESC 6
 * Move cursor left in row.  If we're at the left boundary, shift everything
 * in that row right.  Clear left column.
 */
#ifndef NO_FRILLS
/* EXTPROTO */
void
rxvt_scr_backindex(rxvt_t *r)
{
    if (r->screen.cur.col > 0)
	rxvt_scr_gotorc(r, 0, -1, R_RELATIVE | C_RELATIVE);
    else {
	if (r->screen.tlen[r->screen.cur.row + r->TermWin.saveLines] == 0)
	    return;		/* um, yeah? */
	rxvt_scr_insdel_chars(r, 1, INSERT);
    }
}
#endif
/* ------------------------------------------------------------------------- */
/*
 * Process DEC Forward Index
 * XTERM_SEQ: ESC 9
 * Move cursor right in row.  If we're at the right boundary, shift everything
 * in that row left.  Clear right column.
 */
#ifndef NO_FRILLS
/* EXTPROTO */
void
rxvt_scr_forwardindex(rxvt_t *r)
{
    int             row;

    if (r->screen.cur.col < r->TermWin.ncol - 1)
	rxvt_scr_gotorc(r, 0, 1, R_RELATIVE | C_RELATIVE);
    else {
	row = r->screen.cur.row + r->TermWin.saveLines;
	if (r->screen.tlen[row] == 0)
	    return;		/* um, yeah? */
	else if (r->screen.tlen[row] == -1)
	    r->screen.tlen[row] = r->TermWin.ncol;
	rxvt_scr_gotorc(r, 0, 0, R_RELATIVE);
	rxvt_scr_insdel_chars(r, 1, DELETE);
	rxvt_scr_gotorc(r, 0, r->TermWin.ncol - 1, R_RELATIVE);
    }
}
#endif

/* ------------------------------------------------------------------------- */
/*
 * Goto Row/Column
 */
/* EXTPROTO */
void
rxvt_scr_gotorc(rxvt_t *r, int row, int col, int relative)
{
    r->h->want_refresh = 1;
    ZERO_SCROLLBACK(r);
    RESET_CHSTAT(r->h);
    if (rxvt_Gr_Displayed(r))
	rxvt_Gr_scroll(r, 0);

    D_SCREEN((stderr, "rxvt_scr_gotorc(r:%s%d,c:%s%d): from (r:%d,c:%d)", (relative & R_RELATIVE ? "+" : ""), row, (relative & C_RELATIVE ? "+" : ""), col, r->screen.cur.row, r->screen.cur.col));

    r->screen.cur.col = ((relative & C_RELATIVE) ? (r->screen.cur.col + col)
						 : col);
    MAX_IT(r->screen.cur.col, 0);
    MIN_IT(r->screen.cur.col, r->TermWin.ncol - 1);

    r->screen.flags &= ~Screen_WrapNext;
    if (relative & R_RELATIVE) {
	if (row > 0) {
	    if (r->screen.cur.row <= r->screen.bscroll
		&& (r->screen.cur.row + row) > r->screen.bscroll)
		r->screen.cur.row = r->screen.bscroll;
	    else
		r->screen.cur.row += row;
	} else if (row < 0) {
	    if (r->screen.cur.row >= r->screen.tscroll
		&& (r->screen.cur.row + row) < r->screen.tscroll)
		r->screen.cur.row = r->screen.tscroll;
	    else
		r->screen.cur.row += row;
	}
    } else {
	if (r->screen.flags & Screen_Relative) {	/* relative origin mode */
	    r->screen.cur.row = row + r->screen.tscroll;
	    MIN_IT(r->screen.cur.row, r->screen.bscroll);
	} else
	    r->screen.cur.row = row;
    }
    MAX_IT(r->screen.cur.row, 0);
    MIN_IT(r->screen.cur.row, r->TermWin.nrow - 1);
}

/* ------------------------------------------------------------------------- */
/*
 * direction  should be UP or DN
 */
/* EXTPROTO */
void
rxvt_scr_index(rxvt_t *r, enum page_dirn direction)
{
    int             dirn;

    r->h->want_refresh = 1;
    dirn = ((direction == UP) ? 1 : -1);
    D_SCREEN((stderr, "rxvt_scr_index(%d)", dirn));

    ZERO_SCROLLBACK(r);
    RESET_CHSTAT(r->h);
    if (rxvt_Gr_Displayed(r))
	rxvt_Gr_scroll(r, 0);

    r->screen.flags &= ~Screen_WrapNext;
    if ((r->screen.cur.row == r->screen.bscroll && direction == UP)
	|| (r->screen.cur.row == r->screen.tscroll && direction == DN))
	rxvt_scroll_text(r, r->screen.tscroll, r->screen.bscroll, dirn, 0);
    else
	r->screen.cur.row += dirn;
    MAX_IT(r->screen.cur.row, 0);
    MIN_IT(r->screen.cur.row, r->TermWin.nrow - 1);
    rxvt_selection_check(r, 0);
}

/* ------------------------------------------------------------------------- */
/*
 * Erase part or whole of a line
 * XTERM_SEQ: Clear line to right: ESC [ 0 K
 * XTERM_SEQ: Clear line to left : ESC [ 1 K
 * XTERM_SEQ: Clear whole line   : ESC [ 2 K
 */
/* EXTPROTO */
void
rxvt_scr_erase_line(rxvt_t *r, int mode)
{
    unsigned int    row, col, num;

    r->h->want_refresh = 1;
    D_SCREEN((stderr, "rxvt_scr_erase_line(%d) at screen row: %d", mode, r->screen.cur.row));
    ZERO_SCROLLBACK(r);
    RESET_CHSTAT(r->h);
    if (rxvt_Gr_Displayed(r))
	rxvt_Gr_scroll(r, 0);
    rxvt_selection_check(r, 1);

    r->screen.flags &= ~Screen_WrapNext;

    row = r->TermWin.saveLines + r->screen.cur.row;
    switch (mode) {
    case 0:			/* erase to end of line */
	col = r->screen.cur.col;
	num = r->TermWin.ncol - col;
	MIN_IT(r->screen.tlen[row], (int16_t)col);
	if (ROWCOL_IN_ROW_AT_OR_AFTER(r->selection.beg, r->screen.cur)
	    || ROWCOL_IN_ROW_AT_OR_AFTER(r->selection.end, r->screen.cur))
	    CLEAR_SELECTION(r);
	break;
    case 1:			/* erase to beginning of line */
	col = 0;
	num = r->screen.cur.col + 1;
	if (ROWCOL_IN_ROW_AT_OR_BEFORE(r->selection.beg, r->screen.cur)
	    || ROWCOL_IN_ROW_AT_OR_BEFORE(r->selection.end, r->screen.cur))
	    CLEAR_SELECTION(r);
	break;
    case 2:			/* erase whole line */
	col = 0;
	num = r->TermWin.ncol;
	r->screen.tlen[row] = 0;
	if (r->selection.beg.row <= r->screen.cur.row
	    && r->selection.end.row >= r->screen.cur.row)
	    CLEAR_SELECTION(r);
	break;
    default:
	return;
    }
    if (r->screen.text[row])
	rxvt_blank_line(&(r->screen.text[row][col]),
			&(r->screen.rend[row][col]), num, r->h->rstyle);
    else
	rxvt_blank_screen_mem(r, r->screen.text, r->screen.rend, row,
			      r->h->rstyle);
}

/* ------------------------------------------------------------------------- */
/*
 * Erase part of whole of the screen
 * XTERM_SEQ: Clear screen after cursor : ESC [ 0 J
 * XTERM_SEQ: Clear screen before cursor: ESC [ 1 J
 * XTERM_SEQ: Clear whole screen        : ESC [ 2 J
 */
/* EXTPROTO */
void
rxvt_scr_erase_screen(rxvt_t *r, int mode)
{
    int             num;
    int32_t         row, row_offset;
    rend_t          ren;
    XGCValues       gcvalue;

    r->h->want_refresh = 1;
    D_SCREEN((stderr, "rxvt_scr_erase_screen(%d) at screen row: %d", mode, r->screen.cur.row));
    ZERO_SCROLLBACK(r);
    RESET_CHSTAT(r->h);
    row_offset = (int32_t)r->TermWin.saveLines;

    switch (mode) {
    case 0:			/* erase to end of screen */
	rxvt_selection_check(r, 1);
	rxvt_scr_erase_line(r, 0);
	row = r->screen.cur.row + 1;	/* possible OOB */
	num = r->TermWin.nrow - row;
	break;
    case 1:			/* erase to beginning of screen */
	rxvt_selection_check(r, 3);
	rxvt_scr_erase_line(r, 1);
	row = 0;
	num = r->screen.cur.row;
	break;
    case 2:			/* erase whole screen */
	rxvt_selection_check(r, 3);
	rxvt_Gr_ClearScreen(r);
	row = 0;
	num = r->TermWin.nrow;
	break;
    default:
	return;
    }
    r->h->refresh_type |= REFRESH_BOUNDS;
    if (r->selection.op && r->h->current_screen == r->selection.screen
	&& ((r->selection.beg.row >= row && r->selection.beg.row <= row + num)
	    || (r->selection.end.row >= row
		&& r->selection.end.row <= row + num)))
	CLEAR_SELECTION(r);
    if (row >= r->TermWin.nrow)	/* Out Of Bounds */
	return;
    MIN_IT(num, (r->TermWin.nrow - row));
    if (r->h->rstyle & (RS_RVid | RS_Uline))
	ren = (rend_t) ~RS_None;
    else if (GET_BASEBG(r->h->rstyle) == Color_bg) {
	ren = DEFAULT_RSTYLE;
	CLEAR_ROWS(row, num);
    } else {
	ren = (r->h->rstyle & (RS_fgMask | RS_bgMask));
	gcvalue.foreground = r->PixColors[GET_BGCOLOR(r->h->rstyle)];
	XChangeGC(r->Xdisplay, r->TermWin.gc, GCForeground, &gcvalue);
	ERASE_ROWS(row, num);
	gcvalue.foreground = r->PixColors[Color_fg];
	XChangeGC(r->Xdisplay, r->TermWin.gc, GCForeground, &gcvalue);
    }
    for (; num--; row++) {
	rxvt_blank_screen_mem(r, r->screen.text, r->screen.rend,
			      (unsigned int)(row + row_offset), r->h->rstyle);
	r->screen.tlen[row + row_offset] = 0;
	rxvt_blank_line(r->drawn_text[row], r->drawn_rend[row],
			(unsigned int)r->TermWin.ncol, ren);
    }
}

/* ------------------------------------------------------------------------- */
/*
 * Fill the screen with `E's
 * XTERM_SEQ: Screen Alignment Test: ESC # 8
 */
/* EXTPROTO */
void
rxvt_scr_E(rxvt_t *r)
{
    int             i, j, k;
    rend_t         *r1, fs;

    r->h->want_refresh = 1;
    r->h->num_scr_allow = 0;
    ZERO_SCROLLBACK(r);
    RESET_CHSTAT(r->h);
    rxvt_selection_check(r, 3);

    fs = r->h->rstyle;
    for (k = r->TermWin.saveLines, i = r->TermWin.nrow; i--; k++) {
	r->screen.tlen[k] = r->TermWin.ncol;	/* make the `E's selectable */
	MEMSET(r->screen.text[k], 'E', r->TermWin.ncol);
	for (r1 = r->screen.rend[k], j = r->TermWin.ncol; j--; )
	    *r1++ = fs;
    }
}

/* ------------------------------------------------------------------------- */
/*
 * Insert/Delete <count> lines
 */
/* EXTPROTO */
void
rxvt_scr_insdel_lines(rxvt_t *r, int count, int insdel)
{
    int             end;

    ZERO_SCROLLBACK(r);
    RESET_CHSTAT(r->h);
    if (rxvt_Gr_Displayed(r))
	rxvt_Gr_scroll(r, 0);
    rxvt_selection_check(r, 1);

    if (r->screen.cur.row > r->screen.bscroll)
	return;

    end = r->screen.bscroll - r->screen.cur.row + 1;
    if (count > end) {
	if (insdel == DELETE)
	    return;
	else if (insdel == INSERT)
	    count = end;
    }
    r->screen.flags &= ~Screen_WrapNext;

    rxvt_scroll_text(r, r->screen.cur.row, r->screen.bscroll, insdel * count,
		     0);
}

/* ------------------------------------------------------------------------- */
/*
 * Insert/Delete <count> characters from the current position
 */
/* EXTPROTO */
void
rxvt_scr_insdel_chars(rxvt_t *r, int count, int insdel)
{
    int             col, row;
    rend_t          tr;
    text_t         *stp;
    rend_t         *srp;
    int16_t        *slp;

    r->h->want_refresh = 1;
    ZERO_SCROLLBACK(r);
#if 0
    RESET_CHSTAT(r->h);
#endif
    if (rxvt_Gr_Displayed(r))
	rxvt_Gr_scroll(r, 0);

    if (count <= 0)
	return;

    rxvt_selection_check(r, 1);
    MIN_IT(count, (r->TermWin.ncol - r->screen.cur.col));

    row = r->screen.cur.row + r->TermWin.saveLines;
    r->screen.flags &= ~Screen_WrapNext;

    stp = r->screen.text[row];
    srp = r->screen.rend[row];
    slp = &(r->screen.tlen[row]);
    switch (insdel) {
    case INSERT:
	for (col = r->TermWin.ncol - 1; (col - count) >= r->screen.cur.col;
	     col--) {
	    stp[col] = stp[col - count];
	    srp[col] = srp[col - count];
	}
	if (*slp != -1) {
	    *slp += count;
	    MIN_IT(*slp, r->TermWin.ncol);
	}
	if (r->selection.op && r->h->current_screen == r->selection.screen
	    && ROWCOL_IN_ROW_AT_OR_AFTER(r->selection.beg, r->screen.cur)) {
	    if (r->selection.end.row != r->screen.cur.row
		|| (r->selection.end.col + count >= r->TermWin.ncol))
		CLEAR_SELECTION(r);
	    else {		/* shift selection */
		r->selection.beg.col += count;
		r->selection.mark.col += count;	/* XXX: yes? */
		r->selection.end.col += count;
	    }
	}
	rxvt_blank_line(&(stp[r->screen.cur.col]), &(srp[r->screen.cur.col]),
			(unsigned int)count, r->h->rstyle);
	break;
    case ERASE:
	r->screen.cur.col += count;	/* don't worry if > r->TermWin.ncol */
	rxvt_selection_check(r, 1);
	r->screen.cur.col -= count;
	rxvt_blank_line(&(stp[r->screen.cur.col]), &(srp[r->screen.cur.col]),
			(unsigned int)count, r->h->rstyle);
	break;
    case DELETE:
	tr = srp[r->TermWin.ncol - 1]
	     & (RS_fgMask | RS_bgMask | RS_baseattrMask);
	for (col = r->screen.cur.col; (col + count) < r->TermWin.ncol; col++) {
	    stp[col] = stp[col + count];
	    srp[col] = srp[col + count];
	}
	rxvt_blank_line(&(stp[r->TermWin.ncol - count]),
			&(srp[r->TermWin.ncol - count]),
			(unsigned int)count, tr);
	if (*slp == -1)	/* break line continuation */
	    *slp = r->TermWin.ncol;
	*slp -= count;
	MAX_IT(*slp, 0);
	if (r->selection.op && r->h->current_screen == r->selection.screen
	    && ROWCOL_IN_ROW_AT_OR_AFTER(r->selection.beg, r->screen.cur)) {
	    if (r->selection.end.row != r->screen.cur.row
		|| (r->screen.cur.col >= r->selection.beg.col - count)
		|| r->selection.end.col >= r->TermWin.ncol)
		CLEAR_SELECTION(r);
	    else {
		/* shift selection */
		r->selection.beg.col -= count;
		r->selection.mark.col -= count;	/* XXX: yes? */
		r->selection.end.col -= count;
	    }
	}
	break;
    }
#if 0
    if (IS_MULTI2(srp[0])) {
	srp[0] &= ~RS_multiMask;
	stp[0] = ' ';
    }
    if (IS_MULTI1(srp[r->TermWin.ncol - 1])) {
	srp[r->TermWin.ncol - 1] &= ~RS_multiMask;
	stp[r->TermWin.ncol - 1] = ' ';
    }
#endif
}

/* ------------------------------------------------------------------------- */
/*
 * Set the scrolling region
 * XTERM_SEQ: Set region <top> - <bot> inclusive: ESC [ <top> ; <bot> r
 */
/* EXTPROTO */
void
rxvt_scr_scroll_region(rxvt_t *r, int top, int bot)
{
    MAX_IT(top, 0);
    MIN_IT(bot, r->TermWin.nrow - 1);
    if (top > bot)
	return;
    r->screen.tscroll = top;
    r->screen.bscroll = bot;
    rxvt_scr_gotorc(r, 0, 0, 0);
}

/* ------------------------------------------------------------------------- */
/*
 * Make the cursor visible/invisible
 * XTERM_SEQ: Make cursor visible  : ESC [ ? 25 h
 * XTERM_SEQ: Make cursor invisible: ESC [ ? 25 l
 */
/* EXTPROTO */
void
rxvt_scr_cursor_visible(rxvt_t *r, int mode)
{
    r->h->want_refresh = 1;
    if (mode)
	r->screen.flags |= Screen_VisibleCursor;
    else
	r->screen.flags &= ~Screen_VisibleCursor;
}

/* ------------------------------------------------------------------------- */
/*
 * Set/unset automatic wrapping
 * XTERM_SEQ: Set Wraparound  : ESC [ ? 7 h
 * XTERM_SEQ: Unset Wraparound: ESC [ ? 7 l
 */
/* EXTPROTO */
void
rxvt_scr_autowrap(rxvt_t *r, int mode)
{
    if (mode)
	r->screen.flags |= Screen_Autowrap;
    else
	r->screen.flags &= ~(Screen_Autowrap | Screen_WrapNext);
}

/* ------------------------------------------------------------------------- */
/*
 * Set/unset margin origin mode
 * Absolute mode: line numbers are counted relative to top margin of screen
 *      and the cursor can be moved outside the scrolling region.
 * Relative mode: line numbers are relative to top margin of scrolling region
 *      and the cursor cannot be moved outside.
 * XTERM_SEQ: Set Absolute: ESC [ ? 6 h
 * XTERM_SEQ: Set Relative: ESC [ ? 6 l
 */
/* EXTPROTO */
void
rxvt_scr_relative_origin(rxvt_t *r, int mode)
{
    if (mode)
	r->screen.flags |= Screen_Relative;
    else
	r->screen.flags &= ~Screen_Relative;
    rxvt_scr_gotorc(r, 0, 0, 0);
}

/* ------------------------------------------------------------------------- */
/*
 * Set insert/replace mode
 * XTERM_SEQ: Set Insert mode : ESC [ ? 4 h
 * XTERM_SEQ: Set Replace mode: ESC [ ? 4 l
 */
/* EXTPROTO */
void
rxvt_scr_insert_mode(rxvt_t *r, int mode)
{
    if (mode)
	r->screen.flags |= Screen_Insert;
    else
	r->screen.flags &= ~Screen_Insert;
}

/* ------------------------------------------------------------------------- */
/*
 * Set/Unset tabs
 * XTERM_SEQ: Set tab at current column  : ESC H
 * XTERM_SEQ: Clear tab at current column: ESC [ 0 g
 * XTERM_SEQ: Clear all tabs             : ESC [ 3 g
 */
/* EXTPROTO */
void
rxvt_scr_set_tab(rxvt_t *r, int mode)
{
    if (mode < 0)
	MEMSET(r->tabs, 0, r->TermWin.ncol * sizeof(char));
    else if (r->screen.cur.col < r->TermWin.ncol)
	r->tabs[r->screen.cur.col] = (mode ? 1 : 0);
}

/* ------------------------------------------------------------------------- */
/*
 * Set reverse/normal video
 * XTERM_SEQ: Reverse video: ESC [ ? 5 h
 * XTERM_SEQ: Normal video : ESC [ ? 5 l
 */
/* EXTPROTO */
void
rxvt_scr_rvideo_mode(rxvt_t *r, int mode)
{
    XGCValues       gcvalue;

    if (r->h->rvideo != mode) {
	r->h->rvideo = mode;
	SWAP_IT(r->PixColors[Color_fg], r->PixColors[Color_bg], Pixel);
#if defined(XPM_BACKGROUND)
	if (r->h->bgPixmap.pixmap == None)
#endif
#if defined(TRANSPARENT)
	    if (!(r->Options & Opt_transparent) || r->h->am_transparent == 0)
#endif
	    XSetWindowBackground(r->Xdisplay, r->TermWin.vt,
				 r->PixColors[Color_bg]);

	gcvalue.foreground = r->PixColors[Color_fg];
	gcvalue.background = r->PixColors[Color_bg];
	XChangeGC(r->Xdisplay, r->TermWin.gc, GCBackground | GCForeground,
		  &gcvalue);
	rxvt_scr_clear(r);
	rxvt_scr_touch(r, True);
    }
}

/* ------------------------------------------------------------------------- */
/*
 * Report current cursor position
 * XTERM_SEQ: Report position: ESC [ 6 n
 */
/* EXTPROTO */
void
rxvt_scr_report_position(rxvt_t *r)
{
    rxvt_tt_printf(r, "\033[%d;%dR", r->screen.cur.row + 1,
		   r->screen.cur.col + 1);
}

/* ------------------------------------------------------------------------- *
 *                                  FONTS                                    *
 * ------------------------------------------------------------------------- */

/*
 * Set font style
 */
/* INTPROTO */
void
rxvt_set_font_style(rxvt_t *r)
{
    r->h->rstyle &= ~RS_fontMask;
    switch (r->h->charsets[r->screen.charset]) {
    case '0':			/* DEC Special Character & Line Drawing Set */
	r->h->rstyle |= RS_acsFont;
	break;
    case 'A':			/* United Kingdom (UK) */
	r->h->rstyle |= RS_ukFont;
	break;
    case 'B':			/* United States (USASCII) */
	break;
    case '<':			/* Multinational character set */
	break;
    case '5':			/* Finnish character set */
	break;
    case 'C':			/* Finnish character set */
	break;
    case 'K':			/* German character set */
	break;
    }
}

/* ------------------------------------------------------------------------- */
/*
 * Choose a font
 * XTERM_SEQ: Invoke G0 character set: CTRL-O
 * XTERM_SEQ: Invoke G1 character set: CTRL-N
 * XTERM_SEQ: Invoke G2 character set: ESC N
 * XTERM_SEQ: Invoke G3 character set: ESC O
 */
/* EXTPROTO */
void
rxvt_scr_charset_choose(rxvt_t *r, int set)
{
    r->screen.charset = set;
    rxvt_set_font_style(r);
}

/* ------------------------------------------------------------------------- */
/*
 * Set a font
 * XTERM_SEQ: Set G0 character set: ESC ( <C>
 * XTERM_SEQ: Set G1 character set: ESC ) <C>
 * XTERM_SEQ: Set G2 character set: ESC * <C>
 * XTERM_SEQ: Set G3 character set: ESC + <C>
 * See set_font_style for possible values for <C>
 */
/* EXTPROTO */
void
rxvt_scr_charset_set(rxvt_t *r, int set, unsigned int ch)
{
#ifdef MULTICHAR_SET
    r->h->multi_byte = !!(set < 0);
    set = abs(set);
#endif
    r->h->charsets[set] = (unsigned char)ch;
    rxvt_set_font_style(r);
}

/* ------------------------------------------------------------------------- *
 *          MULTIPLE-CHARACTER FONT SET MANIPULATION FUNCTIONS               *
 * ------------------------------------------------------------------------- */
#ifdef MULTICHAR_SET
/* EXTPROTO */
void
rxvt_euc2jis(unsigned char *str, int len)
{
    int             i;

    for (i = 0; i < len; i++)
	str[i] &= 0x7F;
}

/* ------------------------------------------------------------------------- */
/* INTPROTO */
void
rxvt_sjis2jis(unsigned char *str, int len)
{
    int             i;
    unsigned char  *high, *low;

    for (i = 0; i < len; i += 2, str += 2) {
	high = str;
	low = str + 1;
	(*high) -= (*high > 0x9F ? 0xB1 : 0x71);
	*high = (*high) * 2 + 1;
	if (*low > 0x9E) {
	    *low -= 0x7E;
	    (*high)++;
	} else {
	    if (*low > 0x7E)
		(*low)--;
	    *low -= 0x1F;
	}
    }
}

/* INTPROTO */
void
rxvt_decodedummy(unsigned char *str __attribute__((unused)), int len __attribute__((unused)))
{
}

/* EXTPROTO */
void
rxvt_set_multichar_encoding(rxvt_t *r, const char *str)
{
    KNOWN_ENCODINGS *a;

    if (str && *str) {
	for (a = (KNOWN_ENCODINGS *)known_encodings; a->name; a++) {
	    if (STRCASECMP(str, a->name) == 0) {
		r->encoding_method = a->method;
		r->h->multichar_decode = a->func;
		break;
	    }
	}
    }
}
#endif				/* MULTICHAR_SET */

/* ------------------------------------------------------------------------- *
 *                           GRAPHICS COLOURS                                *
 * ------------------------------------------------------------------------- */

#ifdef RXVT_GRAPHICS
/* EXTPROTO */
int
rxvt_scr_get_fgcolor(rxvt_t *r)
{
    return GET_FGCOLOR(r->h->rstyle);
}

/* ------------------------------------------------------------------------- */
/* EXTPROTO */
int
rxvt_scr_get_bgcolor(rxvt_t *r)
{
    return GET_BGCOLOR(r->h->rstyle);
}
#endif

/* ------------------------------------------------------------------------- *
 *                        MAJOR SCREEN MANIPULATION                          *
 * ------------------------------------------------------------------------- */

/*
 * Refresh an area
 */
enum {
    PART_BEG = 0,
    PART_END,
    RC_COUNT
};

/* EXTPROTO */
void
rxvt_scr_expose(rxvt_t *r, int x, int y, int width, int height, Bool refresh)
{
    int             i;
    row_col_t       rc[RC_COUNT];

    if (r->drawn_text == NULL)	/* sanity check */
	return;

#ifdef DEBUG_STRICT
    x = max(x, (int)r->TermWin.int_bwidth);
    x = min(x, (int)r->TermWin.width);
    y = max(y, (int)r->TermWin.int_bwidth);
    y = min(y, (int)r->TermWin.height);
#endif

/* round down */
    rc[PART_BEG].col = Pixel2Col(x);
    rc[PART_BEG].row = Pixel2Row(y);
/* round up */
    rc[PART_END].col = Pixel2Width(x + width + r->TermWin.fwidth - 1);
    rc[PART_END].row = Pixel2Row(y + height + r->TermWin.fheight - 1);

/* sanity checks */
    for (i = PART_BEG; i < RC_COUNT; i++) {
	MIN_IT(rc[i].col, r->TermWin.ncol - 1);
	MIN_IT(rc[i].row, r->TermWin.nrow - 1);
    }

    D_SCREEN((stderr, "rxvt_scr_expose(x:%d, y:%d, w:%d, h:%d) area (c:%d,r:%d)-(c:%d,r:%d)", x, y, width, height, rc[PART_BEG].col, rc[PART_BEG].row, rc[PART_END].col, rc[PART_END].row));

    for (i = rc[PART_BEG].row; i <= rc[PART_END].row; i++)
	MEMSET(&(r->drawn_text[i][rc[PART_BEG].col]), 0,
	       rc[PART_END].col - rc[PART_BEG].col + 1);

    if (refresh)
	rxvt_scr_refresh(r, SLOW_REFRESH | REFRESH_BOUNDS);
}

/* ------------------------------------------------------------------------- */
/*
 * Refresh the entire screen
 */
/* EXTPROTO */
void
rxvt_scr_touch(rxvt_t *r, Bool refresh)
{
    rxvt_scr_expose(r, 0, 0, r->TermWin.width, r->TermWin.height, refresh);
}

/* ------------------------------------------------------------------------- */
/*
 * Move the display so that the line represented by scrollbar value Y is at
 * the top of the screen
 */
/* EXTPROTO */
int
rxvt_scr_move_to(rxvt_t *r, int y, int len)
{
    long            p = 0;
    u_int16_t       oldviewstart;

    oldviewstart = r->TermWin.view_start;
    if (y < len) {
	p = (r->TermWin.nrow + r->TermWin.nscrolled) * (len - y) / len;
	p -= (long)(r->TermWin.nrow - 1);
	p = max(p, 0);
    }
    r->TermWin.view_start = (u_int16_t)min(p, r->TermWin.nscrolled);
    D_SCREEN((stderr, "rxvt_scr_move_to(%d, %d) view_start:%d", y, len, r->TermWin.view_start));

    return rxvt_scr_changeview(r, oldviewstart);
}

/* ------------------------------------------------------------------------- */
/*
 * Page the screen up/down nlines
 * direction should be UP or DN
 */
/* EXTPROTO */
int
rxvt_scr_page(rxvt_t *r, enum page_dirn direction, int nlines)
{
    int             n;
    u_int16_t       oldviewstart;

    D_SCREEN((stderr, "rxvt_scr_page(%s, %d) view_start:%d", ((direction == UP) ? "UP" : "DN"), nlines, r->TermWin.view_start));
#ifdef DEBUG_STRICT
    assert((nlines >= 0) && (nlines <= r->TermWin.nrow));
#endif
    oldviewstart = r->TermWin.view_start;
    if (direction == UP) {
	n = r->TermWin.view_start + nlines;
	r->TermWin.view_start = min(n, r->TermWin.nscrolled);
    } else {
	n = r->TermWin.view_start - nlines;
	r->TermWin.view_start = max(n, 0);
    }
    return rxvt_scr_changeview(r, oldviewstart);
}

/* INTPROTO */
int
rxvt_scr_changeview(rxvt_t *r, u_int16_t oldviewstart)
{
    if (r->TermWin.view_start != oldviewstart) {
	r->h->want_refresh = 1;
	if (rxvt_Gr_Displayed(r))
	    rxvt_Gr_scroll(r, 0);
	r->h->num_scr -= (r->TermWin.view_start - oldviewstart);
    }
    return (int)(r->TermWin.view_start - oldviewstart);
}

/* ------------------------------------------------------------------------- */
/* EXTPROTO */
void
rxvt_scr_bell(rxvt_t *r)
{
#ifndef NO_BELL
# ifndef NO_MAPALERT
#  ifdef MAPALERT_OPTION
    if (r->Options & Opt_mapAlert)
#  endif
	XMapWindow(r->Xdisplay, r->TermWin.parent[0]);
# endif
    if (r->Options & Opt_visualBell) {
	rxvt_scr_rvideo_mode(r, !r->h->rvideo); /* refresh also done */
	rxvt_scr_rvideo_mode(r, !r->h->rvideo); /* refresh also done */
    } else
	XBell(r->Xdisplay, 0);
#endif
}

/* ------------------------------------------------------------------------- */
/* ARGSUSED */
/* EXTPROTO */
void
rxvt_scr_printscreen(rxvt_t *r, int fullhist)
{
#ifdef PRINTPIPE
    int             i, r1, nrows, row_offset;
    text_t         *t;
    FILE           *fd;

    if ((fd = rxvt_popen_printer(r)) == NULL)
	return;
    nrows = r->TermWin.nrow;
    row_offset = r->TermWin.saveLines;
    if (!fullhist)
	row_offset -= r->TermWin.view_start;
    else {
	nrows += r->TermWin.nscrolled;
	row_offset -= r->TermWin.nscrolled;
    }

    for (r1 = 0; r1 < nrows; r1++) {
	t = r->screen.text[r1 + row_offset];
	for (i = r->TermWin.ncol - 1; i >= 0; i--)
	    if (!isspace(t[i]))
		break;
	fprintf(fd, "%.*s\n", (i + 1), t);
    }
    rxvt_pclose_printer(fd);
#endif
}

/* ------------------------------------------------------------------------- */
/*
 * Refresh the screen
 * r->drawn_text/r->drawn_rend contain the screen information before the update.
 * r->screen.text/r->screen.rend contain what the screen will change to.
 */

#define DRAW_STRING(Func, x, y, str, len)				\
    Func(r, r->Xdisplay, drawBuffer, r->TermWin.gc, (x), (y), (str), (len))

#if defined (NO_BRIGHTCOLOR) || defined (VERYBOLD)
# define MONO_BOLD(x)		((x) & (RS_Bold|RS_Blink))
# define MONO_BOLD_FG(x, fg)	MONO_BOLD(x)
#else
# define MONO_BOLD(x)						\
    (((x) & (RS_Bold | RS_fgMask)) == (RS_Bold | Color_fg))
# define MONO_BOLD_FG(x, fg)	(((x) & RS_Bold) && (fg) == Color_fg)
#endif

#define FONT_WIDTH(X, Y)						\
    (X)->per_char[(Y) - (X)->min_char_or_byte2].width
#define FONT_RBEAR(X, Y)						\
    (X)->per_char[(Y) - (X)->min_char_or_byte2].rbearing
#define FONT_LBEAR(X, Y)						\
    (X)->per_char[(Y) - (X)->min_char_or_byte2].lbearing
#define IS_FONT_CHAR(X, Y)						\
    ((Y) >= (X)->min_char_or_byte2 && (Y) <= (X)->max_char_or_byte2)

#ifdef MULTICHAR_SET
static int
mfont_draw_string16(rxvt_t *r,
		    Display *display,
		    Drawable d,
		    GC gc,
		    int x,
		    int y,
		    XChar2b *str,
		    int len)
{
    while (len > 0) {
	XDrawString16(display, d, gc, x, y, str, 1);
	if (str->byte1 == '\0') {
	    x += r->TermWin.fwidth;
	}
	else {
	    x += r->TermWin.fwidth * 2;
	}
	str++;
	len--;
    }
    return 0;
}

static int
mfont_draw_image_string16(rxvt_t *r,
			  Display *display,
			  Drawable d,
			  GC gc,
			  int x,
			  int y,
			  XChar2b *str,
			  int len)
{
    while (len > 0) {
	XDrawImageString16(display, d, gc, x, y, str, 1);
	if (str->byte1 == '\0') {
	    x += r->TermWin.fwidth;
	}
	else {
	    x += r->TermWin.fwidth * 2;
	}
	str++;
	len--;
    }
    return 0;
}

static int
draw_string16(rxvt_t *r,
	      Display *display,
	      Drawable d,
	      GC gc,
	      int x,
	      int y,
	      XChar2b *str,
	      int len)
{
    XDrawString16(display, d, gc, x, y, str, len);
}

static int
draw_image_string16(rxvt_t *r,
		    Display *display,
		    Drawable d,
		    GC gc,
		    int x,
		    int y,
		    XChar2b *str,
		    int len)
{
    XDrawImageString16(display, d, gc, x, y, str, len);
}

#endif

static int
draw_string(rxvt_t *r,
	    Display *display,
	    Drawable d,
	    GC gc,
	    int x,
	    int y,
	    char *str,
	    int len)
{
    XDrawString(display, d, gc, x, y, str, len);
}

static int
draw_image_string(rxvt_t *r,
		  Display *display,
		  Drawable d,
		  GC gc,
		  int x,
		  int y,
		  char *str,
		  int len)
{
    XDrawImageString(display, d, gc, x, y, str, len);
}

/* EXTPROTO */
void
rxvt_scr_refresh(rxvt_t *r, unsigned char refresh_type)
{
    unsigned char   clearfirst,	/* first character writes before cell        */
		    clearlast,	/* last character writes beyond cell         */
		    must_clear,	/* use draw_string not draw_image_string     */
#ifndef NO_BOLDFONT
		    bfont,	/* we've changed font to bold font           */
#endif
		    rvid,	/* reverse video this position               */
		    wbyte;	/* we're in multibyte                        */
    char            morecur = 0;/*                                           */
#ifdef TTY_256COLOR
    u_int16_t	    fore, back;	/* desired foreground/background             */
#else
    unsigned char   fore, back;	/* desired foreground/background             */
#endif
    int16_t         col, row,	/* column/row we're processing               */
                    ocrow,	/* old cursor row                            */
		    len, wlen;	/* text length screen/buffer                 */
    int             i,		/* tmp                                       */
		    row_offset;	/* basic offset in screen structure          */
#ifndef NO_CURSORCOLOR
    rend_t	    cc1;	/* store colours at cursor position(s)       */
# ifdef MULTICHAR_SET
    rend_t          cc2;	/* store colours at cursor position(s)       */
# endif
#endif
    XGCValues       gcvalue;	/* Graphics Context values                   */
    rend_t         *drp, *srp;	/* drawn-rend-pointer, screen-rend-pointer   */
    text_t         *dtp, *stp;	/* drawn-text-pointer, screen-text-pointer   */
    char           *buffer;	/* local copy of r->h->buffer                */
    struct rxvt_hidden *h = r->h;
    int             (*draw_string_func) () = draw_string;
    int             (*draw_image_string_func) () = draw_image_string;

    if (refresh_type == NO_REFRESH || !r->TermWin.mapped)
	return;

/*
 * A: set up vars
 */
    clearfirst = clearlast = must_clear = wbyte = 0;
#ifndef NO_BOLDFONT
    bfont = 0;
#endif

    if (h->currmaxcol < r->TermWin.ncol) {
	h->currmaxcol = r->TermWin.ncol;
	h->buffer = rxvt_realloc(h->buffer, sizeof(char) * (h->currmaxcol + 1));
    }
    buffer = h->buffer;
    h->refresh_count = 0;

    row_offset = r->TermWin.saveLines - r->TermWin.view_start;
/*
 * always go back to the base font - it's much safer
 */
    XSetFont(r->Xdisplay, r->TermWin.gc, r->TermWin.font->fid);

    if ((refresh_type & REFRESH_BOUNDS)) {
	clearfirst = clearlast = 1;
	h->refresh_type &= ~REFRESH_BOUNDS;
    }
#if defined(XPM_BACKGROUND)
    must_clear |= (h->bgPixmap.pixmap != None);
#endif
#if defined(TRANSPARENT)
    must_clear |= ((r->Options & Opt_transparent) && h->am_transparent);
#endif
    ocrow = h->oldcursor.row; /* is there an old outline cursor on screen? */

    /* set base colours to avoid check in "single glyph writing" below */
    gcvalue.foreground = r->PixColors[Color_fg];
    gcvalue.background = r->PixColors[Color_bg];

/*
 * B: reverse any characters which are selected
 */
    rxvt_scr_reverse_selection(r);

/*
 * C: set the cursor character(s)
 */
    {
	unsigned char   setoldcursor;
	rend_t          ccol1,	/* Cursor colour       */
	                ccol2;	/* Cursor colour2      */

	if ((r->screen.flags & Screen_VisibleCursor) && r->TermWin.focus) {
	    srp = &(r->screen.rend[r->screen.cur.row + r->TermWin.saveLines]
				  [r->screen.cur.col]);
	    *srp ^= RS_RVid;
#ifndef NO_CURSORCOLOR
	    cc1 = *srp & (RS_fgMask | RS_bgMask);
	    if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_cursor))
		ccol1 = Color_cursor;
	    else
#ifdef CURSOR_COLOR_IS_RENDITION_COLOR
		ccol1 = GET_FGCOLOR(h->rstyle);
#else
		ccol1 = Color_fg;
#endif
	    if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_cursor))
		ccol2 = Color_cursor2;
	    else
#ifdef CURSOR_COLOR_IS_RENDITION_COLOR
		ccol2 = GET_BGCOLOR(h->rstyle);
#else
		ccol2 = Color_bg;
#endif
	    *srp = SET_FGCOLOR(*srp, ccol1);
	    *srp = SET_BGCOLOR(*srp, ccol2);
#endif
#ifdef MULTICHAR_SET
	    if (IS_MULTI1(*srp)) {
		if (r->screen.cur.col < r->TermWin.ncol - 2
		    && IS_MULTI2(*++srp))
		    morecur = 1;
	    } else if (IS_MULTI2(*srp)) {
		if (r->screen.cur.col > 0 && IS_MULTI1(*--srp))
		    morecur = -1;
	    }
	    if (morecur) {
		*srp ^= RS_RVid;
# ifndef NO_CURSORCOLOR
		cc2 = *srp & (RS_fgMask | RS_bgMask);
		*srp = SET_FGCOLOR(*srp, ccol1);
		*srp = SET_BGCOLOR(*srp, ccol2);
# endif
	    }
#endif
	}

	/* make sure no outline cursor is left around */
	setoldcursor = 0;
	if (ocrow != -1) {
	    if (r->screen.cur.row + r->TermWin.view_start != ocrow
		|| r->screen.cur.col != h->oldcursor.col) {
		if (ocrow < r->TermWin.nrow
		    && h->oldcursor.col < r->TermWin.ncol) {
		    r->drawn_rend[ocrow][h->oldcursor.col] ^= (RS_RVid | RS_Uline);
#ifdef MULTICHAR_SET
		    if (h->oldcursormulti) {
			col = h->oldcursor.col + h->oldcursormulti;
			if (col < r->TermWin.ncol)
			    r->drawn_rend[ocrow][col] ^= (RS_RVid | RS_Uline);
		    }
#endif
		}
		if (r->TermWin.focus
		    || !(r->screen.flags & Screen_VisibleCursor))
		    h->oldcursor.row = -1;
		else
		    setoldcursor = 1;
	    }
	} else if (!r->TermWin.focus)
	    setoldcursor = 1;
	if (setoldcursor) {
	    if (r->screen.cur.row + r->TermWin.view_start >= r->TermWin.nrow)
		h->oldcursor.row = -1;
	    else {
		h->oldcursor.row = r->screen.cur.row + r->TermWin.view_start;
		h->oldcursor.col = r->screen.cur.col;
#ifdef MULTICHAR_SET
		h->oldcursormulti = morecur;
#endif
	    }
	}
    }

#ifndef NO_SLOW_LINK_SUPPORT
/*
 * D: CopyArea pass - very useful for slower links
 *    This has been deliberately kept simple.
 */
    i = h->num_scr;
    if (refresh_type == FAST_REFRESH && h->num_scr_allow && i
	&& abs(i) < r->TermWin.nrow && !must_clear) {
	int16_t         nits;
	int             j;
	rend_t         *drp2;
	text_t         *dtp2;

	j = r->TermWin.nrow;
	wlen = len = -1;
	row = i > 0 ? 0 : j - 1;
	for (; j-- >= 0; row += (i > 0 ? 1 : -1)) {
	    if (row + i >= 0 && row + i < r->TermWin.nrow && row + i != ocrow) {
		stp = r->screen.text[row + row_offset];
		srp = r->screen.rend[row + row_offset];
		dtp = r->drawn_text[row];
		dtp2 = r->drawn_text[row + i];
		drp = r->drawn_rend[row];
		drp2 = r->drawn_rend[row + i];
		for (nits = 0, col = r->TermWin.ncol; col--; )
		    if (stp[col] != dtp2[col] || srp[col] != drp2[col])
			nits--;
		    else if (stp[col] != dtp[col] || srp[col] != drp[col])
			nits++;
		if (nits > 8) {	/* XXX: arbitrary choice */
		    for (col = r->TermWin.ncol; col--; ) {
			*dtp++ = *dtp2++;
			*drp++ = *drp2++;
		    }
		    if (len == -1)
			len = row;
		    wlen = row;
		    continue;
		}
	    }
	    if (len != -1) {
		/* also comes here at end if needed because of >= above */
		if (wlen < len)
		    SWAP_IT(wlen, len, int);
		D_SCREEN((stderr, "rxvt_scr_refresh(): XCopyArea: %d -> %d (height: %d)", len + i, len, wlen - len + 1));
		XCopyArea(r->Xdisplay, r->TermWin.vt, r->TermWin.vt,
			  r->TermWin.gc, 0, Row2Pixel(len + i),
			  (unsigned int)TermWin_TotalWidth(),
			  (unsigned int)Height2Pixel(wlen - len + 1),
			  0, Row2Pixel(len));
		len = -1;
	    }
	}
    }
#endif


/*
 * E: main pass across every character
 */
    for (row = 0; row < r->TermWin.nrow; row++) {
	unsigned char   clear_next = 0;
	int             j,
			xpixel,	 /* x offset for start of drawing (font) */
		        ypixel,	 /* y offset for start of drawing (font) */
		        ypixelc; /* y offset for top of drawing          */
	unsigned long   gcmask;	 /* Graphics Context mask                */
	const XFontStruct *wf;	/* which font are we in                      */

	stp = r->screen.text[row + row_offset];
	srp = r->screen.rend[row + row_offset];
	dtp = r->drawn_text[row];
	drp = r->drawn_rend[row];

#ifndef NO_PIXEL_DROPPING_AVOIDANCE
/*
 * E1: pixel dropping avoidance.  Do this before the main refresh on the line.
 *     Require a refresh where pixels may have been dropped into our
 *     area by a neighbour character which has now changed
 *     TODO: this could be integrated into E2 but might be too messy
 */
# ifdef NO_BOLDFONT
	wf = r->TermWin.font;
# endif
	for (col = 0; col < r->TermWin.ncol; col++) {
	    unsigned char   is_font_char, is_same_char;
	    text_t          t;

	    t = dtp[col];
	    is_same_char = (t == stp[col] && drp[col] == srp[col]);
	    if (!clear_next
		&& (is_same_char
		    || t == 0	/* screen cleared elsewhere */
		    || t == ' '))
		continue;

	    if (clear_next) {	/* previous char caused change here */
		clear_next = 0;
		dtp[col] = 0;
		if (is_same_char)	/* don't cascade into next char */
		    continue;
	    }
	    j = MONO_BOLD(drp[col]) ? 1 : 0;
# ifndef NO_BOLDFONT
	    wf = (j && r->TermWin.boldFont) ? r->TermWin.boldFont
					    : r->TermWin.font;
# endif
/*
 * TODO: consider if anything special needs to happen with:
 * #if defined(MULTICHAR_SET) && ! defined(NO_BOLDOVERSTRIKE_MULTI)
 */
	    is_font_char = (wf->per_char && IS_FONT_CHAR(wf, t)) ? 1 : 0;
	    if (!is_font_char || FONT_LBEAR(wf, t) < 0) {
		if (col == 0)
		    clearfirst = 1;
		else
		    dtp[col - 1] = 0;
	    }
	    if (!is_font_char
		|| (FONT_WIDTH(wf, t) < (FONT_RBEAR(wf, t) + j))) {
		if (col == r->TermWin.ncol - 1)
		    clearlast = 1;
		else
		    clear_next = 1;
	    }
	}
#endif				/* NO_PIXEL_DROPPING_AVOIDANCE */

/*
 * E2: OK, now the real pass
 */
	ypixelc = (int)Row2Pixel(row);
	ypixel = ypixelc + r->TermWin.fascent;

	for (col = 0; col < r->TermWin.ncol; col++) {
	    unsigned char   fontdiff,/* current font size != base font size */
			    fprop;   /* proportional font used              */
	    rend_t          rend;    /* rendition value                     */

	    /* compare new text with old - if exactly the same then continue */
	    rend = srp[col];	/* screen rendition (target rendtion) */
	    if (stp[col] == dtp[col]	/* Must match characters to skip. */
		&& (rend == drp[col]	/* Either rendition the same or   */
		    || (stp[col] == ' '	/* space w/ no background change  */
			&& GET_BGATTR(rend) == GET_BGATTR(drp[col])))) {
		if (!IS_MULTI1(rend))
		    continue;
#ifdef MULTICHAR_SET
		else {	/* first byte is Kanji so compare second bytes */
		    if (stp[col + 1] == dtp[col + 1]) {
		    /* assume no corrupt characters on the screen */
			col++;
			continue;
		    }
		}
#endif
	    }
	    /* redraw one or more characters */

	    fontdiff = r->TermWin.fontdiff;
	    len = 0;
	    buffer[len++] = dtp[col] = stp[col];
	    drp[col] = rend;
	    xpixel = Col2Pixel(col);

/*
 * Find out the longest string we can write out at once
 */
#ifndef NO_BOLDFONT
	    if (MONO_BOLD(rend) && r->TermWin.boldFont != NULL)
		fprop = (r->TermWin.propfont & PROPFONT_BOLD);
	    else
#endif
	    fprop = (r->TermWin.propfont & PROPFONT_NORMAL);
#ifdef MULTICHAR_SET
	    if (IS_MULTI1(rend)
		&& col < r->TermWin.ncol - 1 && IS_MULTI2(srp[col + 1])) {
		if (!wbyte && r->TermWin.mfont) {
		    wbyte = 1;
		    XSetFont(r->Xdisplay, r->TermWin.gc,
			     r->TermWin.mfont->fid);
		    fontdiff |= (r->TermWin.propfont & PROPFONT_MULTI) ? 1 : 0;
		    if (r->TermWin.propfont & PROPFONT_MULTI) {
			draw_string_func = mfont_draw_string16;
			draw_image_string_func = mfont_draw_image_string16;
		    } else {
			draw_string_func = draw_string16;
			draw_image_string_func = draw_image_string16;
		    }
		}
		if (r->TermWin.mfont == NULL) {
		    buffer[0] = buffer[1] = ' ';
		    len = 2;
		    col++;
		} else {
		    /* double stepping - we're in multibyte font mode */
		    for (; ++col < r->TermWin.ncol;) {
			/* XXX: could check sanity on 2nd byte */
			dtp[col] = stp[col];
			drp[col] = srp[col];
			buffer[len++] = stp[col];
			col++;
			if (fprop)	/* proportional multibyte font mode */
			    break;
			if ((col == r->TermWin.ncol) || (srp[col] != rend))
			    break;
			if ((stp[col] == dtp[col])
			    && (srp[col] == drp[col])
			    && (stp[col + 1] == dtp[col + 1]))
			    break;
			if (len == h->currmaxcol)
			    break;
			dtp[col] = stp[col];
			drp[col] = srp[col];
			buffer[len++] = stp[col];
		    }
		    col--;
		}
		if (buffer[0] & 0x80)
		    (h->multichar_decode)(buffer, len);
		wlen = len / 2;
	    } else {
		if (rend & RS_multi1) {
		    /* corrupt character - you're outta there */
		    rend &= ~RS_multiMask;
		    drp[col] = rend;	/* TODO check: may also want */
		    dtp[col] = ' ';	/* to poke into stp/srp      */
		    buffer[0] = ' ';
		}
		if (wbyte) {
		    wbyte = 0;
		    XSetFont(r->Xdisplay, r->TermWin.gc, r->TermWin.font->fid);
		    draw_string_func = draw_string;
		    draw_image_string_func = draw_image_string;
		}
#else
	    {
#endif
		if (!fprop) {
		    /* single stepping - `normal' mode */
		    for (i = 0; ++col < r->TermWin.ncol - 1;) {
			if (rend != srp[col])
			    break;
			buffer[len++] = stp[col];
			if ((stp[col] != dtp[col]) || (srp[col] != drp[col])) {
			    if (must_clear && (i++ > (len / 2)))
				break;
			    dtp[col] = stp[col];
			    drp[col] = srp[col];
			    i = 0;
			} else if (must_clear || (stp[col] != ' ' && ++i > 32))
			    break;
		    }
		    col--;	/* went one too far.  move back */
		    len -= i;	/* dump any matching trailing chars */
		}
		wlen = len;
	    }
	    buffer[len] = '\0';

/*
 * Determine the attributes for the string
 */
	    fore = GET_FGCOLOR(rend);
	    back = GET_BGCOLOR(rend);
	    rend = GET_ATTR(rend);

	    switch (rend & RS_fontMask) {
	    case RS_acsFont:
		for (i = 0; i < len; i++)
		    if (buffer[i] == 0x5f)
			buffer[i] = 0x7f;
		    else if (buffer[i] > 0x5f && buffer[i] < 0x7f)
			buffer[i] -= 0x5f;
		break;
	    case RS_ukFont:
		for (i = 0; i < len; i++)
		    if (buffer[i] == '#')
			buffer[i] = 0x1e;	/* pound sign */
		break;
	    }

	    rvid = (rend & RS_RVid) ? 1 : 0;
#ifdef OPTION_HC
	    if (!rvid && (rend & RS_Blink)) {
		if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_HC)
		    && r->PixColors[fore] != r->PixColors[Color_HC]
		    && r->PixColors[back] != r->PixColors[Color_HC])
		    back = Color_HC;
		else
		    rvid = !rvid;	/* fall back */
	    }
#endif
	    if (rvid) {
#ifdef TTY_256COLOR
		SWAP_IT(fore, back, u_int16_t);
#else
		SWAP_IT(fore, back, unsigned char);
#endif
#ifndef NO_BOLD_UNDERLINE_REVERSE
		if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_RV)
		    && r->PixColors[fore] != r->PixColors[Color_RV])
		    back = Color_RV;
#endif
	    }
	    gcmask = 0;
	    if (back != Color_bg) {
		gcvalue.background = r->PixColors[back];
		gcmask = GCBackground;
	    }
	    if (fore != Color_fg) {
		gcvalue.foreground = r->PixColors[fore];
		gcmask |= GCForeground;
	    }
#ifndef NO_BOLD_UNDERLINE_REVERSE
	    else if (rend & RS_Bold) {
		if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_BD)
		    && r->PixColors[fore] != r->PixColors[Color_BD]
		    && r->PixColors[back] != r->PixColors[Color_BD]) {
		    gcvalue.foreground = r->PixColors[Color_BD];
		    gcmask |= GCForeground;
# ifndef VERYBOLD
		    rend &= ~RS_Bold;	/* we've taken care of it */
# endif
		}
	    } else if (rend & RS_Uline) {
		if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_UL)
		    && r->PixColors[fore] != r->PixColors[Color_UL]
		    && r->PixColors[back] != r->PixColors[Color_UL]) {
		    gcvalue.foreground = r->PixColors[Color_UL];
		    gcmask |= GCForeground;
		    rend &= ~RS_Uline;	/* we've taken care of it */
		}
	    }
#endif
	    if (gcmask)
		XChangeGC(r->Xdisplay, r->TermWin.gc, gcmask, &gcvalue);
#ifndef NO_BOLDFONT
	    if (!wbyte && MONO_BOLD_FG(rend, fore)
		&& r->TermWin.boldFont != NULL) {
		bfont = 1;
		XSetFont(r->Xdisplay, r->TermWin.gc, r->TermWin.boldFont->fid);
		fontdiff |= (r->TermWin.propfont & PROPFONT_BOLD) ? 1 : 0;
		rend &= ~RS_Bold;	/* we've taken care of it */
	    } else if (bfont) {
		bfont = 0;
		if (!wbyte)
		    XSetFont(r->Xdisplay, r->TermWin.gc, r->TermWin.font->fid);
	    }
#endif
/*
 * Actually do the drawing of the string here
 */
	    if (back == Color_bg && must_clear) {
		CLEAR_CHARS(xpixel, ypixelc, len);
		for (i = 0; i < len; i++)	/* don't draw empty strings */
		    if (buffer[i] != ' ') {
			DRAW_STRING(draw_string, xpixel, ypixel, buffer, wlen);
			break;
		    }
	    } else if (fprop || fontdiff) {	/* single glyph writing */
		unsigned long   gctmp;

		gctmp = gcvalue.foreground;
		gcvalue.foreground = gcvalue.background;
		XChangeGC(r->Xdisplay, r->TermWin.gc, GCForeground, &gcvalue);
		XFillRectangle(r->Xdisplay, drawBuffer, r->TermWin.gc,
			       xpixel, ypixelc, (unsigned int)Width2Pixel(len),
			       (unsigned int)(Height2Pixel(1)
					      - r->TermWin.lineSpace));
		gcvalue.foreground = gctmp;
		XChangeGC(r->Xdisplay, r->TermWin.gc, GCForeground, &gcvalue);
		DRAW_STRING(draw_string_func, xpixel, ypixel, buffer, wlen);
	    } else
		DRAW_STRING(draw_image_string_func, xpixel, ypixel,
			    buffer, wlen);

#ifndef NO_BOLDOVERSTRIKE
# ifdef NO_BOLDOVERSTRIKE_MULTI
	    if (!wbyte)
# endif
		if (MONO_BOLD_FG(rend, fore))
		    DRAW_STRING(draw_string, xpixel + 1, ypixel, buffer, wlen);
#endif
	    if ((rend & RS_Uline) && (r->TermWin.font->descent > 1))
		XDrawLine(r->Xdisplay, drawBuffer, r->TermWin.gc,
			  xpixel, ypixel + 1,
			  xpixel + Width2Pixel(len) - 1, ypixel + 1);
	    if (gcmask) {	/* restore normal colours */
		gcvalue.foreground = r->PixColors[Color_fg];
		gcvalue.background = r->PixColors[Color_bg];
		XChangeGC(r->Xdisplay, r->TermWin.gc, gcmask, &gcvalue);
	    }
	}			/* for (col....) */
    }				/* for (row....) */

/*
 * G: cleanup cursor and display outline cursor in necessary
 */
    if (r->screen.flags & Screen_VisibleCursor) {
	if (r->TermWin.focus) {
	    srp = &(r->screen.rend[r->screen.cur.row + r->TermWin.saveLines]
				  [r->screen.cur.col]);
	    *srp ^= RS_RVid;
#ifndef NO_CURSORCOLOR
	    *srp = (*srp & ~(RS_fgMask | RS_bgMask)) | cc1;
#endif
#ifdef MULTICHAR_SET
	    if (morecur) {
		srp += morecur;
		*srp ^= RS_RVid;
# ifndef NO_CURSORCOLOR
		*srp = (*srp & ~(RS_fgMask | RS_bgMask)) | cc2;
# endif
	    }
#endif
	} else if (h->oldcursor.row >= 0) {
#ifndef NO_CURSORCOLOR
	    unsigned long   gcmask;	/* Graphics Context mask */

	    gcmask = 0;
	    if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_cursor)) {
		gcvalue.foreground = r->PixColors[Color_cursor];
		gcmask = GCForeground;
		XChangeGC(r->Xdisplay, r->TermWin.gc, gcmask, &gcvalue);
		gcvalue.foreground = r->PixColors[Color_fg];
	    }
#endif
	    XDrawRectangle(r->Xdisplay, drawBuffer, r->TermWin.gc,
			   Col2Pixel(h->oldcursor.col + morecur),
			   Row2Pixel(h->oldcursor.row),
			   (unsigned int)(Width2Pixel(1 + (morecur ? 1 : 0))
					  - 1),
			   (unsigned int)(Height2Pixel(1)
					  - r->TermWin.lineSpace - 1));
#ifndef NO_CURSORCOLOR
	    if (gcmask)		/* restore normal colours */
		XChangeGC(r->Xdisplay, r->TermWin.gc, gcmask, &gcvalue);
#endif
	}
    }
/*
 * H: cleanup selection
 */
    rxvt_scr_reverse_selection(r);

/*
 * I: other general cleanup
 */
    if (clearfirst && r->TermWin.int_bwidth)
	/* 
	 * clear the whole screen height, note that width == 0 is treated
	 * specially by XClearArea
	 */
	XClearArea(r->Xdisplay, r->TermWin.vt, 0, 0,
		   (unsigned int)r->TermWin.int_bwidth,
		   (unsigned int)TermWin_TotalHeight(), False);
    if (clearlast && r->TermWin.int_bwidth)
	/* 
	 * clear the whole screen height, note that width == 0 is treated
	 * specially by XClearArea
	 */
	XClearArea(r->Xdisplay, r->TermWin.vt,
		   r->TermWin.width + r->TermWin.int_bwidth, 0,
		   (unsigned int)r->TermWin.int_bwidth,
		   (unsigned int)TermWin_TotalHeight(), False);
    if (refresh_type & SMOOTH_REFRESH)
	XSync(r->Xdisplay, False);

    h->num_scr = 0;
    h->num_scr_allow = 1;
    h->want_refresh = 0;	/* screen is current */
}
/* ------------------------------------------------------------------------- */

/* EXTPROTO */
void
rxvt_scr_clear(rxvt_t *r)
{
    if (!r->TermWin.mapped)
	return;
    r->h->num_scr_allow = 0;
    r->h->want_refresh = 1;
#ifdef TRANSPARENT
    if ((r->Options & Opt_transparent) && (r->h->am_pixmap_trans == 0)) {
	int             i;

	if (!(r->Options & Opt_transparent_all))
	    i = 0;
	else
	    i = (int)(sizeof(r->TermWin.parent) / sizeof(Window));
	for (; i--;)
	    if (r->TermWin.parent[i] != None)
		XClearWindow(r->Xdisplay, r->TermWin.parent[i]);
    }
#endif
    XClearWindow(r->Xdisplay, r->TermWin.vt);
}

/* ------------------------------------------------------------------------- */
/* INTPROTO */
void
rxvt_scr_reverse_selection(rxvt_t *r)
{
    int             i, col, row, end_row;
    rend_t         *srp;

    if (r->selection.op && r->h->current_screen == r->selection.screen) {
	end_row = r->TermWin.saveLines - r->TermWin.view_start;
	i = r->selection.beg.row + r->TermWin.saveLines;
	row = r->selection.end.row + r->TermWin.saveLines;
	if (i >= end_row)
	    col = r->selection.beg.col;
	else {
	    col = 0;
	    i = end_row;
	}
	end_row += r->TermWin.nrow;
	for (; i < row && i < end_row; i++, col = 0)
	    for (srp = r->screen.rend[i]; col < r->TermWin.ncol; col++)
#ifndef OPTION_HC
		srp[col] ^= RS_RVid;
#else
		srp[col] ^= RS_Blink;
#endif
	if (i == row && i < end_row)
	    for (srp = r->screen.rend[i]; col < r->selection.end.col; col++)
#ifndef OPTION_HC
		srp[col] ^= RS_RVid;
#else
		srp[col] ^= RS_Blink;
#endif
    }
}

/* ------------------------------------------------------------------------- */
/*
 * Dump the whole scrollback and screen to the passed filedescriptor.  The
 * invoking routine must close the fd.
 */
#if 0
/* EXTPROTO */
void
rxvt_scr_dump(rxvt_t *r, int fd)
{
    int             row, wrote;
    unsigned int    width, towrite;
    char            r1[] = "\n";

    for (row = r->TermWin.saveLines - r->TermWin.nscrolled;
	 row < r->TermWin.saveLines + r->TermWin.nrow - 1; row++) {
	width = r->screen.tlen[row] >= 0 ? r->screen.tlen[row]
					 : r->TermWin.ncol;
	for (towrite = width; towrite; towrite -= wrote) {
	    wrote = write(fd, &(r->screen.text[row][width - towrite]),
			  towrite);
	    if (wrote < 0)
		return;		/* XXX: death, no report */
	}
	if (r->screen.tlen[row] >= 0)
	    if (write(fd, r1, 1) <= 0)
		return;	/* XXX: death, no report */
    }
}
#endif

/* ------------------------------------------------------------------------- *
 *                           CHARACTER SELECTION                             *
 * ------------------------------------------------------------------------- */

/*
 * -r->TermWin.nscrolled <= (selection row) <= r->TermWin.nrow - 1
 */
/* EXTPROTO */
void
rxvt_selection_check(rxvt_t *r, int check_more)
{
    row_col_t       pos;

    if (!r->selection.op)
	return;

    pos.row = pos.col = 0;
    if ((r->selection.beg.row < -(int32_t)r->TermWin.nscrolled)
	|| (r->selection.beg.row >= r->TermWin.nrow)
	|| (r->selection.mark.row < -(int32_t)r->TermWin.nscrolled)
	|| (r->selection.mark.row >= r->TermWin.nrow)
	|| (r->selection.end.row < -(int32_t)r->TermWin.nscrolled)
	|| (r->selection.end.row >= r->TermWin.nrow)
        || (check_more == 1
	    && r->h->current_screen == r->selection.screen
	    && !ROWCOL_IS_BEFORE(r->screen.cur, r->selection.beg)
	    && ROWCOL_IS_BEFORE(r->screen.cur, r->selection.end))
	|| (check_more == 2
	    && ROWCOL_IS_BEFORE(r->selection.beg, pos)
	    && ROWCOL_IS_AFTER(r->selection.end, pos))
	|| (check_more == 3
	    && ROWCOL_IS_AFTER(r->selection.end, pos))
	|| (check_more == 4	/* screen width change */
	    && (r->selection.beg.row != r->selection.end.row
		|| r->selection.end.col > r->TermWin.ncol)))
	CLEAR_SELECTION(r);
}

/* ------------------------------------------------------------------------- */
/*
 * Paste a selection direct to the command fd
 */
/* INTPROTO */
void
rxvt_PasteIt(rxvt_t *r, const unsigned char *data, unsigned int nitems)
{
    unsigned int    i, j, n;
    unsigned char  *ds = rxvt_malloc(PROP_SIZE);
    
/* convert normal newline chars into common keyboard Return key sequence */
    for (i = 0; i < nitems; i += PROP_SIZE) {
	n = min(nitems - i, PROP_SIZE);
	MEMCPY(ds, data + i, n);
	for (j = 0; j < n; j++)
	    if (ds[j] == '\n')
		ds[j] = '\r';
	rxvt_tt_write(r, ds, (int)n);
    }
    free(ds);
}

/* ------------------------------------------------------------------------- */
/*
 * Respond to a notification that a primary selection has been sent
 * EXT: SelectionNotify
 */
/* EXTPROTO */
int
rxvt_selection_paste(rxvt_t *r, Window win, Atom prop, Bool delete_prop)
{
    long            nread = 0;
    unsigned long   bytes_after;
    XTextProperty   ct;
#ifdef MULTICHAR_SET
    int             dummy_count;
    char          **cl;
#endif

    D_SELECT((stderr, "rxvt_selection_paste(%08lx, %lu, %d), wait=%2x", win, (unsigned long)prop, (int)delete_prop, r->h->selection_wait));

    if (prop == None) {		/* check for failed XConvertSelection */
#ifdef MULTICHAR_SET
	if ((r->h->selection_type & Sel_CompoundText)) {
	    int             selnum = r->h->selection_type & Sel_whereMask;

	    r->h->selection_type = 0;
	    if (selnum != Sel_direct)
		rxvt_selection_request_other(r, XA_STRING, selnum);
	}
#endif
	return 0;
    }
    for (;;) {
	if (XGetWindowProperty(r->Xdisplay, win, prop, (long)(nread / 4),
			       (long)(PROP_SIZE / 4), delete_prop,
			       AnyPropertyType, &ct.encoding, &ct.format,
			       &ct.nitems, &bytes_after,
			       &ct.value) != Success)
	    break;
	if (ct.encoding == 0) {
	    D_SELECT((stderr, "rxvt_selection_paste: property didn't exist!"));
	    break;
	}
	if (ct.value == NULL) {
	    D_SELECT((stderr, "rxvt_selection_paste: property shooting blanks!"));
	    continue;
	}
	if (ct.nitems == 0) {
	    D_SELECT((stderr, "rxvt_selection_paste: property empty - also INCR end"));
	    if (r->h->selection_wait == Sel_normal && nread == 0) {
	    /*
	     * pass through again trying CUT_BUFFER0 if we've come from
	     * XConvertSelection() but nothing was presented
	     */
		D_SELECT((stderr, "rxvt_selection_request: pasting CUT_BUFFER0"));
		rxvt_selection_paste(r, Xroot, XA_CUT_BUFFER0, False);
	    }
	    nread = -1;		/* discount any previous stuff */
	    break;
	}
	nread += ct.nitems;
#ifdef MULTICHAR_SET
	if (XmbTextPropertyToTextList(r->Xdisplay, &ct, &cl,
				      &dummy_count) == Success && cl) {
	    rxvt_PasteIt(r, cl[0], STRLEN(cl[0]));
	    XFreeStringList(cl);
	} else
#endif
	    rxvt_PasteIt(r, ct.value, (unsigned int)ct.nitems);
	if (bytes_after == 0)
	    break;
	XFree(ct.value);
    }
    if (ct.value)
	XFree(ct.value);
    if (r->h->selection_wait == Sel_normal)
	r->h->selection_wait = Sel_none;
    D_SELECT((stderr, "rxvt_selection_paste: bytes written: %ld", nread));
    return (int)nread;
}

/*
 * INCR support originally provided by Paul Sheer <psheer@obsidian.co.za>
 */
/* EXTPROTO */
void
rxvt_selection_property(rxvt_t *r, Window win, Atom prop)
{
    int             reget_time = 0;

    if (prop == None)
	return;
    D_SELECT((stderr, "rxvt_selection_property(%08lx, %lu)", win, (unsigned long)prop));
    if (r->h->selection_wait == Sel_normal) {
	int             a, afmt;
	Atom            atype;
	unsigned long   bytes_after, nitems;
	unsigned char  *s = NULL;

	a = XGetWindowProperty(r->Xdisplay, win, prop, 0L, 1L, False,
			       r->h->xa[XA_INCR], &atype, &afmt, &nitems,
			       &bytes_after, &s);
	if (s)
	    XFree(s);
	if (a != Success)
	    return;
#ifndef __CYGWIN32__
	if (atype == r->h->xa[XA_INCR]) {	/* start an INCR transfer */
	    D_SELECT((stderr, "rxvt_selection_property: INCR: starting transfer"));
	    XDeleteProperty(r->Xdisplay, win, prop);
	    XFlush(r->Xdisplay);
	    reget_time = 1;
	    r->h->selection_wait = Sel_incr;
	}
#endif
    } else if (r->h->selection_wait == Sel_incr) {
	reget_time = 1;
	if (rxvt_selection_paste(r, win, prop, True) == -1) {
	    D_SELECT((stderr, "rxvt_selection_property: INCR: clean end"));
	    r->h->selection_wait = Sel_none;
	    r->h->timeout[TIMEOUT_INCR].tv_sec = 0;	/* turn off timer */
	}
    }
    if (reget_time) {	/* received more data so reget time */
	(void)gettimeofday(&(r->h->timeout[TIMEOUT_INCR]), NULL);
	r->h->timeout[TIMEOUT_INCR].tv_sec += 10;	/* ten seconds wait */
    }
}
/* ------------------------------------------------------------------------- */
/*
 * Request the current selection: 
 * Order: > internal selection if available
 *        > PRIMARY, SECONDARY, CLIPBOARD if ownership is claimed (+)
 *        > CUT_BUFFER0
 * (+) if ownership is claimed but property is empty, rxvt_selection_paste()
 *     will auto fallback to CUT_BUFFER0
 * EXT: button 2 release
 */
/* EXTPROTO */
void
rxvt_selection_request(rxvt_t *r, Time tm, int x, int y)
{
    D_SELECT((stderr, "rxvt_selection_request(%lu, %d, %d)", tm, x, y));
    if (x < 0 || x >= r->TermWin.width || y < 0 || y >= r->TermWin.height)
	return;			/* outside window */

    if (r->selection.text != NULL) {	/* internal selection */
	D_SELECT((stderr, "rxvt_selection_request: pasting internal"));
	rxvt_PasteIt(r, r->selection.text, r->selection.len);
	return;
    } else {
	int             i;

	r->h->selection_request_time = tm;
	r->h->selection_wait = Sel_normal;
	for (i = Sel_Primary; i <= Sel_Clipboard; i++) {
#ifdef MULTICHAR_SET
	    r->h->selection_type = Sel_CompoundText;
#else
	    r->h->selection_type = 0;
#endif
	    if (rxvt_selection_request_other(r,
#ifdef MULTICHAR_SET
					     r->h->xa[XA_COMPOUND_TEXT],
#else
					     XA_STRING,
#endif
					     i))
		return;
	}
    }
    r->h->selection_wait = Sel_none;	/* don't loop in rxvt_selection_paste() */
    D_SELECT((stderr, "rxvt_selection_request: pasting CUT_BUFFER0"));
    rxvt_selection_paste(r, Xroot, XA_CUT_BUFFER0, False);
}

/* INTPROTO */
int
rxvt_selection_request_other(rxvt_t *r, Atom target, int selnum)
{
    Atom            sel;
#ifdef DEBUG_SELECT
    char           *debug_xa_names[] = { "PRIMARY", "SECONDARY", "CLIPBOARD" };
#endif

    r->h->selection_type |= selnum;
    if (selnum == Sel_Primary)
	sel = XA_PRIMARY;
    else if (selnum == Sel_Secondary)
	sel = XA_SECONDARY;
    else
	sel = r->h->xa[XA_CLIPBOARD];
    if (XGetSelectionOwner(r->Xdisplay, sel) != None) {
	D_SELECT((stderr, "rxvt_selection_request_other: pasting %s", debug_xa_names[selnum]));
	XConvertSelection(r->Xdisplay, sel, target, r->h->xa[XA_VT_SELECTION],
			  r->TermWin.vt, r->h->selection_request_time);
	return 1;
    }
    return 0;
}

/* ------------------------------------------------------------------------- */
/*
 * Clear all selected text
 * EXT: SelectionClear
 */
/* EXTPROTO */
void
rxvt_selection_clear(rxvt_t *r)
{
    D_SELECT((stderr, "rxvt_selection_clear()"));

    r->h->want_refresh = 1;
    if (r->selection.text)
	free(r->selection.text);
    r->selection.text = NULL;
    r->selection.len = 0;
    CLEAR_SELECTION(r);
}

/* ------------------------------------------------------------------------- */
/*
 * Copy a selection into the cut buffer
 * EXT: button 1 or 3 release
 */
/* EXTPROTO */
void
rxvt_selection_make(rxvt_t *r, Time tm)
{
    int             i, col, end_col, row, end_row;
    unsigned char  *new_selection_text;
    char           *str;
    text_t         *t;

    D_SELECT((stderr, "rxvt_selection_make(): r->selection.op=%d, r->selection.clicks=%d", r->selection.op, r->selection.clicks));
    switch (r->selection.op) {
    case SELECTION_CONT:
	break;
    case SELECTION_INIT:
	CLEAR_SELECTION(r);
    /* FALLTHROUGH */
    case SELECTION_BEGIN:
	r->selection.op = SELECTION_DONE;
    /* FALLTHROUGH */
    default:
	return;
    }
    r->selection.op = SELECTION_DONE;

    if (r->selection.clicks == 4)
	return;			/* nothing selected, go away */

    i = (r->selection.end.row - r->selection.beg.row + 1)
	* (r->TermWin.ncol + 1) + 1;
    str = rxvt_malloc(i * sizeof(char));

    new_selection_text = (unsigned char *)str;

    col = r->selection.beg.col;
    MAX_IT(col, 0);
    row = r->selection.beg.row + r->TermWin.saveLines;
    end_row = r->selection.end.row + r->TermWin.saveLines;
/*
 * A: rows before end row
 */
    for (; row < end_row; row++, col = 0) {
	t = &(r->screen.text[row][col]);
	if ((end_col = r->screen.tlen[row]) == -1)
	    end_col = r->TermWin.ncol;
	for (; col < end_col; col++)
	    *str++ = *t++;
	if (r->screen.tlen[row] != -1)
	    *str++ = '\n';
    }
/*
 * B: end row
 */
    t = &(r->screen.text[row][col]);
    end_col = r->screen.tlen[row];
    if (end_col == -1 || r->selection.end.col <= end_col)
	end_col = r->selection.end.col;
    MIN_IT(end_col, r->TermWin.ncol);	/* CHANGE */
    for (; col < end_col; col++)
	*str++ = *t++;
#ifndef NO_OLD_SELECTION
    if (r->selection_style == OLD_SELECT)
	if (end_col == r->TermWin.ncol)
	    *str++ = '\n';
#endif
#ifndef NO_NEW_SELECTION
    if (r->selection_style != OLD_SELECT)
	if (end_col != r->selection.end.col)
	    *str++ = '\n';
#endif
    *str = '\0';
    if ((i = STRLEN((char *)new_selection_text)) == 0) {
	free(new_selection_text);
	return;
    }
    r->selection.len = i;
    if (r->selection.text)
	free(r->selection.text);
    r->selection.text = new_selection_text;

    XSetSelectionOwner(r->Xdisplay, XA_PRIMARY, r->TermWin.vt, tm);
    if (XGetSelectionOwner(r->Xdisplay, XA_PRIMARY) != r->TermWin.vt)
	rxvt_print_error("can't get primary selection");
    XChangeProperty(r->Xdisplay, Xroot, XA_CUT_BUFFER0, XA_STRING, 8,
		    PropModeReplace, r->selection.text, (int)r->selection.len);
    r->h->selection_time = tm;
    D_SELECT((stderr, "rxvt_selection_make(): r->selection.len=%d", r->selection.len));
}

/* ------------------------------------------------------------------------- */
/*
 * Mark or select text based upon number of clicks: 1, 2, or 3
 * EXT: button 1 press
 */
/* EXTPROTO */
void
rxvt_selection_click(rxvt_t *r, int clicks, int x, int y)
{
    D_SELECT((stderr, "rxvt_selection_click(%d, %d, %d)", clicks, x, y));

    clicks = ((clicks - 1) % 3) + 1;
    r->selection.clicks = clicks;	/* save clicks so extend will work */

    rxvt_selection_start_colrow(r, Pixel2Col(x), Pixel2Row(y));
    if (clicks == 2 || clicks == 3)
	rxvt_selection_extend_colrow(r, r->selection.mark.col,
				     r->selection.mark.row
				     + r->TermWin.view_start,
				     0,	/* button 3     */
				     1,	/* button press */
				     0);	/* click change */
}

/* ------------------------------------------------------------------------- */
/*
 * Mark a selection at the specified col/row
 */
/* INTPROTO */
void
rxvt_selection_start_colrow(rxvt_t *r, int col, int row)
{
    r->h->want_refresh = 1;
    r->selection.mark.col = col;
    r->selection.mark.row = row - r->TermWin.view_start;
    MAX_IT(r->selection.mark.row, -(int32_t)r->TermWin.nscrolled);
    MIN_IT(r->selection.mark.row, r->TermWin.nrow - 1);
    MAX_IT(r->selection.mark.col, 0);
    MIN_IT(r->selection.mark.col, r->TermWin.ncol - 1);

    if (r->selection.op) {	/* clear the old selection */
	r->selection.beg.row = r->selection.end.row = r->selection.mark.row;
	r->selection.beg.col = r->selection.end.col = r->selection.mark.col;
    }
    r->selection.op = SELECTION_INIT;
    r->selection.screen = r->h->current_screen;
}

/* ------------------------------------------------------------------------- */
/*
 * Word select: select text for 2 clicks
 * We now only find out the boundary in one direction
 */

/* what do we want: spaces/tabs are delimiters or cutchars or non-cutchars */
#define DELIMIT_TEXT(x) \
    (((x) == ' ' || (x) == '\t') ? 2 : (STRCHR(r->h->rs[Rs_cutchars], (x)) != NULL))
#ifdef MULTICHAR_SET
# define DELIMIT_REND(x)	(((x) & RS_multiMask) ? 1 : 0)
#else
# define DELIMIT_REND(x)	1
#endif

/* INTPROTO */
void
rxvt_selection_delimit_word(rxvt_t *r, enum page_dirn dirn, const row_col_t *mark, row_col_t *ret)
{
    int             col, row, dirnadd, tcol, trow, w1, w2;
    row_col_t       bound;
    text_t         *stp;
    rend_t         *srp;

    if (dirn == UP) {
	bound.row = r->TermWin.saveLines - r->TermWin.nscrolled - 1;
	bound.col = 0;
	dirnadd = -1;
    } else {
	bound.row = r->TermWin.saveLines + r->TermWin.nrow;
	bound.col = r->TermWin.ncol - 1;
	dirnadd = 1;
    }
    row = mark->row + r->TermWin.saveLines;
    col = mark->col;
    MAX_IT(col, 0);
/* find the edge of a word */
    stp = &(r->screen.text[row][col]);
    w1 = DELIMIT_TEXT(*stp);

    if (r->selection_style != NEW_SELECT) {
	if (w1 == 1) {
	    stp += dirnadd;
	    if (DELIMIT_TEXT(*stp) == 1)
		goto Old_Word_Selection_You_Die;
	    col += dirnadd;
	}
	w1 = 0;
    }
    srp = (&r->screen.rend[row][col]);
    w2 = DELIMIT_REND(*srp);

    for (;;) {
	for (; col != bound.col; col += dirnadd) {
	    stp += dirnadd;
	    if (DELIMIT_TEXT(*stp) != w1)
		break;
	    srp += dirnadd;
	    if (DELIMIT_REND(*srp) != w2)
		break;
	}
	if ((col == bound.col) && (row != bound.row)) {
	    if (r->screen.tlen[(row - (dirn == UP ? 1 : 0))] == -1) {
		trow = row + dirnadd;
		tcol = dirn == UP ? r->TermWin.ncol - 1 : 0;
		if (r->screen.text[trow] == NULL)
		    break;
		stp = &(r->screen.text[trow][tcol]);
		srp = &(r->screen.rend[trow][tcol]);
		if (DELIMIT_TEXT(*stp) != w1 || DELIMIT_REND(*srp) != w2)
		    break;
		row = trow;
		col = tcol;
		continue;
	    }
	}
	break;
    }
  Old_Word_Selection_You_Die:
    D_SELECT((stderr, "rxvt_selection_delimit_word(%s,...) @ (r:%3d, c:%3d) has boundary (r:%3d, c:%3d)", (dirn == UP ? "up	" : "down"), mark->row, mark->col, row - r->TermWin.saveLines, col));

    if (dirn == DN)
	col++;			/* put us on one past the end */

/* Poke the values back in */
    ret->row = row - r->TermWin.saveLines;
    ret->col = col;
}

/* ------------------------------------------------------------------------- */
/*
 * Extend the selection to the specified x/y pixel location
 * EXT: button 3 press; button 1 or 3 drag
 * flag == 0 ==> button 1
 * flag == 1 ==> button 3 press
 * flag == 2 ==> button 3 motion
 */
/* EXTPROTO */
void
rxvt_selection_extend(rxvt_t *r, int x, int y, int flag)
{
    int             col, row;

    col = Pixel2Col(x);
    row = Pixel2Row(y);
    MAX_IT(row, 0);
    MIN_IT(row, r->TermWin.nrow - 1);
    MAX_IT(col, 0);
    MIN_IT(col, r->TermWin.ncol);
#ifndef NO_NEW_SELECTION
/*
 * If we're selecting characters (single click) then we must check first
 * if we are at the same place as the original mark.  If we are then
 * select nothing.  Otherwise, if we're to the right of the mark, you have to
 * be _past_ a character for it to be selected.
 */
    if (r->selection_style != OLD_SELECT) {
	if (((r->selection.clicks % 3) == 1) && !flag
	    && (col == r->selection.mark.col
		&& (row == r->selection.mark.row + r->TermWin.view_start))) {
	    /* select nothing */
	    r->selection.beg.row = r->selection.end.row = 0;
	    r->selection.beg.col = r->selection.end.col = 0;
	    r->selection.clicks = 4;
	    r->h->want_refresh = 1;
	    D_SELECT((stderr, "rxvt_selection_extend() r->selection.clicks = 4"));
	    return;
	}
    }
#endif
    if (r->selection.clicks == 4)
	r->selection.clicks = 1;
    rxvt_selection_extend_colrow(r, col, row, !!flag,	/* ? button 3      */
				 flag == 1 ? 1 : 0,	/* ? button press  */
				 0);	/* no click change */
}

#ifdef MULTICHAR_SET
/* INTPROTO */
void
rxvt_selection_adjust_kanji(rxvt_t *r)
{
    int             c1, r1;

    if (r->selection.beg.col > 0) {
	r1 = r->selection.beg.row + r->TermWin.saveLines;
	c1 = r->selection.beg.col;
	if (IS_MULTI2(r->screen.rend[r1][c1])
	    && IS_MULTI1(r->screen.rend[r1][c1 - 1]))
	    r->selection.beg.col--;
    }
    if (r->selection.end.col < r->TermWin.ncol) {
	r1 = r->selection.end.row + r->TermWin.saveLines;
	c1 = r->selection.end.col;
	if (IS_MULTI1(r->screen.rend[r1][c1 - 1])
	    && IS_MULTI2(r->screen.rend[r1][c1]))
	    r->selection.end.col++;
    }
}
#endif				/* MULTICHAR_SET */

/* ------------------------------------------------------------------------- */
/*
 * Extend the selection to the specified col/row
 */
/* INTPROTO */
void
rxvt_selection_extend_colrow(rxvt_t *r, int32_t col, int32_t row, int button3, int buttonpress, int clickchange)
{
    int16_t         ncol = r->TermWin.ncol;
    int             end_col;
    row_col_t       pos;
    enum {
	LEFT, RIGHT
    } closeto = RIGHT;

    D_SELECT((stderr, "rxvt_selection_extend_colrow(c:%d, r:%d, %d, %d) clicks:%d, op:%d", col, row, button3, buttonpress, r->selection.clicks, r->selection.op));
    D_SELECT((stderr, "rxvt_selection_extend_colrow() ENT  b:(r:%d,c:%d) m:(r:%d,c:%d), e:(r:%d,c:%d)", r->selection.beg.row, r->selection.beg.col, r->selection.mark.row, r->selection.mark.col, r->selection.end.row, r->selection.end.col));

    r->h->want_refresh = 1;
    switch (r->selection.op) {
    case SELECTION_INIT:
	CLEAR_SELECTION(r);
	r->selection.op = SELECTION_BEGIN;
    /* FALLTHROUGH */
    case SELECTION_BEGIN:
	if (row != r->selection.mark.row || col != r->selection.mark.col
	    || (!button3 && buttonpress))
	    r->selection.op = SELECTION_CONT;
	break;
    case SELECTION_DONE:
	r->selection.op = SELECTION_CONT;
    /* FALLTHROUGH */
    case SELECTION_CONT:
	break;
    case SELECTION_CLEAR:
	rxvt_selection_start_colrow(r, col, row);
    /* FALLTHROUGH */
    default:
	return;
    }
    if (r->selection.beg.col == r->selection.end.col
	&& r->selection.beg.col != r->selection.mark.col
	&& r->selection.beg.row == r->selection.end.row
	&& r->selection.beg.row != r->selection.mark.row) {
	r->selection.beg.col = r->selection.end.col = r->selection.mark.col;
	r->selection.beg.row = r->selection.end.row = r->selection.mark.row;
	D_SELECT((stderr, "rxvt_selection_extend_colrow() ENT2 b:(r:%d,c:%d) m:(r:%d,c:%d), e:(r:%d,c:%d)", r->selection.beg.row, r->selection.beg.col, r->selection.mark.row, r->selection.mark.col, r->selection.end.row, r->selection.end.col));
    }

    pos.col = col;
    pos.row = row;

    pos.row -= r->TermWin.view_start;	/* adjust for scroll */

#ifndef NO_OLD_SELECTION
/*
 * This mimics some of the selection behaviour of version 2.20 and before.
 * There are no ``selection modes'', button3 is always character extension.
 * Note: button3 drag is always available, c.f. v2.20
 * Selection always terminates (left or right as appropriate) at the mark.
 */
    if (r->selection_style == OLD_SELECT) {
	if (r->selection.clicks == 1 || button3) {
	    if (r->h->hate_those_clicks) {
		r->h->hate_those_clicks = 0;
		if (r->selection.clicks == 1) {
		    r->selection.beg.row = r->selection.mark.row;
		    r->selection.beg.col = r->selection.mark.col;
		} else {
		    r->selection.mark.row = r->selection.beg.row;
		    r->selection.mark.col = r->selection.beg.col;
		}
	    }
	    if (ROWCOL_IS_BEFORE(pos, r->selection.mark)) {
		r->selection.end.row = r->selection.mark.row;
		r->selection.end.col = r->selection.mark.col + 1;
		r->selection.beg.row = pos.row;
		r->selection.beg.col = pos.col;
	    } else {
		r->selection.beg.row = r->selection.mark.row;
		r->selection.beg.col = r->selection.mark.col;
		r->selection.end.row = pos.row;
		r->selection.end.col = pos.col + 1;
	    }
# ifdef MULTICHAR_SET
	    rxvt_selection_adjust_kanji(r);
# endif				/* MULTICHAR_SET */
	} else if (r->selection.clicks == 2) {
	    rxvt_selection_delimit_word(r, UP, &(r->selection.mark),
					&(r->selection.beg));
	    rxvt_selection_delimit_word(r, DN, &(r->selection.mark),
					&(r->selection.end));
	    r->h->hate_those_clicks = 1;
	} else if (r->selection.clicks == 3) {
	    r->selection.beg.row = r->selection.end.row = r->selection.mark.row;
	    r->selection.beg.col = 0;
	    r->selection.end.col = ncol;
	    r->h->hate_those_clicks = 1;
	}
	D_SELECT((stderr, "rxvt_selection_extend_colrow() EXIT b:(r:%d,c:%d) m:(r:%d,c:%d), e:(r:%d,c:%d)", r->selection.beg.row, r->selection.beg.col, r->selection.mark.row, r->selection.mark.col, r->selection.end.row, r->selection.end.col));
	return;
    }
#endif				/* ! NO_OLD_SELECTION */
#ifndef NO_NEW_SELECTION
/* selection_style must not be OLD_SELECT to get here */
/*
 * This is mainly xterm style selection with a couple of differences, mainly
 * in the way button3 drag extension works.
 * We're either doing: button1 drag; button3 press; or button3 drag
 *  a) button1 drag : select around a midpoint/word/line - that point/word/line
 *     is always at the left/right edge of the r->selection.
 *  b) button3 press: extend/contract character/word/line at whichever edge of
 *     the selection we are closest to.
 *  c) button3 drag : extend/contract character/word/line - we select around
 *     a point/word/line which is either the start or end of the selection
 *     and it was decided by whichever point/word/line was `fixed' at the
 *     time of the most recent button3 press
 */
    if (button3 && buttonpress) {	/* button3 press */
	/*
	 * first determine which edge of the selection we are closest to
	 */
	if (ROWCOL_IS_BEFORE(pos, r->selection.beg)
	    || (!ROWCOL_IS_AFTER(pos, r->selection.end)
		&& (((pos.col - r->selection.beg.col)
		     + ((pos.row - r->selection.beg.row) * ncol))
		    < ((r->selection.end.col - pos.col)
		       + ((r->selection.end.row - pos.row) * ncol)))))
	     closeto = LEFT;
	if (closeto == LEFT) {
	    r->selection.beg.row = pos.row;
	    r->selection.beg.col = pos.col;
	    r->selection.mark.row = r->selection.end.row;
	    r->selection.mark.col = r->selection.end.col
				    - (r->selection.clicks == 2);
	} else {
	    r->selection.end.row = pos.row;
	    r->selection.end.col = pos.col;
	    r->selection.mark.row = r->selection.beg.row;
	    r->selection.mark.col = r->selection.beg.col;
	}
    } else {			/* button1 drag or button3 drag */
	if (ROWCOL_IS_AFTER(r->selection.mark, pos)) {
	    if ((r->selection.mark.row == r->selection.end.row)
		&& (r->selection.mark.col == r->selection.end.col)
		&& clickchange && r->selection.clicks == 2)
		r->selection.mark.col--;
	    r->selection.beg.row = pos.row;
	    r->selection.beg.col = pos.col;
	    r->selection.end.row = r->selection.mark.row;
	    r->selection.end.col = r->selection.mark.col
				   + (r->selection.clicks == 2);
	} else {
	    r->selection.beg.row = r->selection.mark.row;
	    r->selection.beg.col = r->selection.mark.col;
	    r->selection.end.row = pos.row;
	    r->selection.end.col = pos.col;
	}
    }

    if (r->selection.clicks == 1) {
	end_col = r->screen.tlen[r->selection.beg.row + r->TermWin.saveLines];
	if (end_col != -1 && r->selection.beg.col > end_col) {
#if 1
	    r->selection.beg.col = ncol;
#else
	    if (r->selection.beg.row != r->selection.end.row)
		r->selection.beg.col = ncol;
	    else
		r->selection.beg.col = r->selection.mark.col;
#endif
	}
	end_col = r->screen.tlen[r->selection.end.row + r->TermWin.saveLines];
	if (end_col != -1 && r->selection.end.col > end_col)
	    r->selection.end.col = ncol;

# ifdef MULTICHAR_SET
	rxvt_selection_adjust_kanji(r);
# endif				/* MULTICHAR_SET */
    } else if (r->selection.clicks == 2) {
	if (ROWCOL_IS_AFTER(r->selection.end, r->selection.beg))
	    r->selection.end.col--;
	rxvt_selection_delimit_word(r, UP, &(r->selection.beg),
				    &(r->selection.beg));
	rxvt_selection_delimit_word(r, DN, &(r->selection.end),
				    &(r->selection.end));
    } else if (r->selection.clicks == 3) {
#ifndef NO_FRILLS
	if ((r->Options & Opt_tripleclickwords)) {
	    int             end_row;

	    rxvt_selection_delimit_word(r, UP, &(r->selection.beg),
					&(r->selection.beg));
	    end_row = r->screen.tlen[r->selection.mark.row
				     + r->TermWin.saveLines];
	    for (end_row = r->selection.mark.row; end_row < r->TermWin.nrow;
		 end_row++) {
		end_col = r->screen.tlen[end_row + r->TermWin.saveLines];
		if (end_col != -1) {
		    r->selection.end.row = end_row;
		    r->selection.end.col = end_col;
		    rxvt_selection_remove_trailing_spaces(r);
		    break;
		}
	    }
	} else
#endif
	{
	    if (ROWCOL_IS_AFTER(r->selection.mark, r->selection.beg))
		r->selection.mark.col++;
	    r->selection.beg.col = 0;
	    r->selection.end.col = ncol;
	}
    }
    if (button3 && buttonpress) {	/* mark may need to be changed */
	if (closeto == LEFT) {
	    r->selection.mark.row = r->selection.end.row;
	    r->selection.mark.col = r->selection.end.col
				    - (r->selection.clicks == 2);
	} else {
	    r->selection.mark.row = r->selection.beg.row;
	    r->selection.mark.col = r->selection.beg.col;
	}
    }
    D_SELECT((stderr, "rxvt_selection_extend_colrow() EXIT b:(r:%d,c:%d) m:(r:%d,c:%d), e:(r:%d,c:%d)", r->selection.beg.row, r->selection.beg.col, r->selection.mark.row, r->selection.mark.col, r->selection.end.row, r->selection.end.col));
#endif				/* ! NO_NEW_SELECTION */
}

#ifndef NO_FRILLS
/* INTPROTO */
void
rxvt_selection_remove_trailing_spaces(rxvt_t *r)
{
    int32_t         end_col, end_row;
    text_t         *stp; 

    end_col = r->selection.end.col;
    end_row = r->selection.end.row;
    for ( ; end_row >= r->selection.beg.row; ) {
	stp = r->screen.text[end_row + r->TermWin.saveLines];
	while (--end_col >= 0) {
	    if (stp[end_col] != ' ' && stp[end_col] != '\t')
		break;
	}
	if (end_col >= 0
	    || r->screen.tlen[end_row - 1 + r->TermWin.saveLines] != -1) {
	    r->selection.end.col = end_col + 1;
	    r->selection.end.row = end_row;
	    break;
	}
	end_row--;
	end_col = r->TermWin.ncol;
    }
    if (r->selection.mark.row > r->selection.end.row) {
	r->selection.mark.row = r->selection.end.row;
	r->selection.mark.col = r->selection.end.col;
    } else if (r->selection.mark.row == r->selection.end.row
	       && r->selection.mark.col > r->selection.end.col)
	r->selection.mark.col = r->selection.end.col;
}
#endif

/* ------------------------------------------------------------------------- */
/*
 * Double click on button 3 when already selected
 * EXT: button 3 double click
 */
/* EXTPROTO */
void
rxvt_selection_rotate(rxvt_t *r, int x, int y)
{
    r->selection.clicks = r->selection.clicks % 3 + 1;
    rxvt_selection_extend_colrow(r, Pixel2Col(x), Pixel2Row(y), 1, 0, 1);
}

/* ------------------------------------------------------------------------- */
/*
 * Respond to a request for our current selection
 * EXT: SelectionRequest
 */
/* EXTPROTO */
void
rxvt_selection_send(rxvt_t *r, const XSelectionRequestEvent *rq)
{
    XSelectionEvent ev;
#ifdef USE_XIM
    Atom            target_list[4];
#else
    Atom            target_list[3];
#endif
    Atom            target;
    XTextProperty   ct;
    XICCEncodingStyle style;
    char           *cl[2], dummy[1];

    ev.type = SelectionNotify;
    ev.property = None;
    ev.display = rq->display;
    ev.requestor = rq->requestor;
    ev.selection = rq->selection;
    ev.target = rq->target;
    ev.time = rq->time;

    if (rq->target == r->h->xa[XA_TARGETS]) {
	target_list[0] = (Atom) r->h->xa[XA_TARGETS];
	target_list[1] = (Atom) XA_STRING;
	target_list[2] = (Atom) r->h->xa[XA_TEXT];
#ifdef USE_XIM
	target_list[3] = (Atom) r->h->xa[XA_COMPOUND_TEXT];
#endif
	XChangeProperty(r->Xdisplay, rq->requestor, rq->property, XA_ATOM,
			32, PropModeReplace, (unsigned char *)target_list,
			(sizeof(target_list) / sizeof(target_list[0])));
	ev.property = rq->property;
    } else if (rq->target == r->h->xa[XA_MULTIPLE]) {
	/* TODO: Handle MULTIPLE */
    } else if (rq->target == r->h->xa[XA_TIMESTAMP] && r->selection.text) {
	XChangeProperty(r->Xdisplay, rq->requestor, rq->property, XA_INTEGER,
			(8 * sizeof(Time)), PropModeReplace,
			(unsigned char *)&r->h->selection_time, 1);
	ev.property = rq->property;
    } else if (rq->target == XA_STRING
	       || rq->target == r->h->xa[XA_COMPOUND_TEXT]
	       || rq->target == r->h->xa[XA_TEXT]) {
#ifdef USE_XIM
	short           freect = 0;
#endif
	int             selectlen;

#ifdef USE_XIM
	if (rq->target != XA_STRING) {
	    target = r->h->xa[XA_COMPOUND_TEXT];
	    style = (rq->target == r->h->xa[XA_COMPOUND_TEXT])
		    ? XCompoundTextStyle : XStdICCTextStyle;
	} else
#endif
	{
	    target = XA_STRING;
	    style = XStringStyle;
	}
	if (r->selection.text) {
	    cl[0] = (char *)r->selection.text;
	    selectlen = r->selection.len;
	} else {
	    cl[0] = dummy;
	    *dummy = '\0';
	    selectlen = 0;
	}
#ifdef USE_XIM
	if (XmbTextListToTextProperty(r->Xdisplay, cl, 1, style, &ct)
	    == Success)		/* if we failed to convert then send it raw */
	    freect = 1;
	else
#endif
	{
	    ct.value = (unsigned char *)cl[0];
	    ct.nitems = selectlen;
	}
	XChangeProperty(r->Xdisplay, rq->requestor, rq->property,
			target, 8, PropModeReplace,
			ct.value, (int)ct.nitems);
	ev.property = rq->property;
#ifdef USE_XIM
	if (freect)
	    XFree(ct.value);
#endif
    }
    XSendEvent(r->Xdisplay, rq->requestor, False, 0L, (XEvent *)&ev);
}

/* ------------------------------------------------------------------------- *
 *                              MOUSE ROUTINES                               *
 * ------------------------------------------------------------------------- */

/*
 * return col/row values corresponding to x/y pixel values
 */
/* EXTPROTO */
void
rxvt_pixel_position(rxvt_t *r, int *x, int *y)
{
    *x = Pixel2Col(*x);
/* MAX_IT(*x, 0); MIN_IT(*x, r->TermWin.ncol - 1); */
    *y = Pixel2Row(*y);
/* MAX_IT(*y, 0); MIN_IT(*y, r->TermWin.nrow - 1); */
}
/* ------------------------------------------------------------------------- */
#ifdef USE_XIM
/* EXTPROTO */
void
rxvt_setPosition(rxvt_t *r, XPoint *pos)
{
    XWindowAttributes xwa;

    XGetWindowAttributes(r->Xdisplay, r->TermWin.vt, &xwa);
    pos->x = Col2Pixel(r->screen.cur.col) + xwa.x;
    pos->y = Height2Pixel((r->screen.cur.row + 1)) + xwa.y
	     - r->TermWin.lineSpace;
}
#endif
/* ------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------- *
 *                              DEBUG ROUTINES                               *
 * ------------------------------------------------------------------------- */
#if 0
/* INTPROTO */
void
rxvt_debug_colors(void)
{
    int             color;
    const char     *name[] = {
	"fg", "bg",
	"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"
    };

    fprintf(stderr, "Color ( ");
    if (r->h->rstyle & RS_RVid)
	fprintf(stderr, "rvid ");
    if (r->h->rstyle & RS_Bold)
	fprintf(stderr, "bold ");
    if (r->h->rstyle & RS_Blink)
	fprintf(stderr, "blink ");
    if (r->h->rstyle & RS_Uline)
	fprintf(stderr, "uline ");
    fprintf(stderr, "): ");

    color = GET_FGCOLOR(r->h->rstyle);
#ifndef NO_BRIGHTCOLOR
    if (color >= minBrightCOLOR && color <= maxBrightCOLOR) {
	color -= (minBrightCOLOR - minCOLOR);
	fprintf(stderr, "bright ");
    }
#endif
    fprintf(stderr, "%s on ", name[color]);

    color = GET_BGCOLOR(r->h->rstyle);
#ifndef NO_BRIGHTCOLOR
    if (color >= minBrightCOLOR && color <= maxBrightCOLOR) {
	color -= (minBrightCOLOR - minCOLOR);
	fprintf(stderr, "bright ");
    }
#endif
    fprintf(stderr, "%s\n", name[color]);
}
#endif