Text.c 108 KB
Newer Older
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 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164
/* $Xorg: Text.c,v 1.4 2001/02/09 02:03:46 xorgcvs Exp $ */

/***********************************************************

Copyright 1987, 1988, 1994, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.


Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/

/*
 * Copyright (c) 1998 by The XFree86 Project, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Except as contained in this notice, the name of the XFree86 Project shall
 * not be used in advertising or otherwise to promote the sale, use or other
 * dealings in this Software without prior written authorization from the
 * XFree86 Project.
 */

/* $XFree86: xc/lib/Xaw/Text.c,v 3.53tsi Exp $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xatom.h>
#include <X11/Xfuncs.h>
#include <X11/Xutil.h>
#include <X11/Xmu/Misc.h>
#include <X11/Xmu/SysUtil.h>
#include <X11/Xmu/Xmu.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Xaw/MultiSinkP.h>
#include <X11/Xaw/TextP.h>
#include <X11/Xaw/TextSrcP.h>
#include <X11/Xaw/TextSinkP.h>
#include <X11/Xaw/Scrollbar.h>
#include <X11/Xaw/XawImP.h>
#include <X11/Xaw/XawInit.h>
#include "Private.h"
#include "XawI18n.h"

#ifndef MAX_LEN_CT
#define MAX_LEN_CT	6	/* for sequence: ESC $ ( A \xx \xx */
#endif

unsigned long FMT8BIT = 0L;
unsigned long XawFmt8Bit = 0L;
unsigned long XawFmtWide = 0L;

#define SinkClearToBG		_XawTextSinkClearToBackground

#define SrcScan			XawTextSourceScan
#define SrcRead			XawTextSourceRead
#define SrcReplace		XawTextSourceReplace
#define SrcSearch		XawTextSourceSearch
#define SrcCvtSel		XawTextSourceConvertSelection
#define SrcSetSelection		XawTextSourceSetSelection

#define MULTI_CLICK_TIME	500L

#define SRC_CHANGE_NONE		0
#define SRC_CHANGE_AFTER	1
#define SRC_CHANGE_BEFORE	2
#define SRC_CHANGE_OVERLAP	3

#define Superclass (&simpleClassRec)

/*
 * Compute a the maximum length of a cut buffer that we can pass at any
 * time.  The 64 allows for the overhead of the Change Property request.
 */
#define MAX_CUT_LEN(dpy)  (XMaxRequestSize(dpy) - 64)

#define	ClearWindow(ctx)						     \
     _XawTextNeedsUpdating((ctx),					     \
			   (ctx)->text.lt.top,				     \
			   (ctx)->text.lt.info[ctx->text.lt.lines].position)

/*
 * Class Methods
 */
static void XawTextClassInitialize(void);
static void XawTextInitialize(Widget, Widget, ArgList, Cardinal*);
static void XawTextRealize(Widget, XtValueMask*, XSetWindowAttributes*);
static void XawTextDestroy(Widget);
static void XawTextResize(Widget);
static void XawTextExpose(Widget, XEvent*, Region);
static Boolean XawTextSetValues(Widget, Widget, Widget, ArgList, Cardinal*);
static void XawTextGetValuesHook(Widget, ArgList, Cardinal*);
static Bool XawTextChangeSensitive(Widget);

/*
 * Prototypes
 */
static XawTextPosition _BuildLineTable(TextWidget, XawTextPosition, int);
static void _CreateCutBuffers(Display*);
static Boolean TextConvertSelection(Widget, Atom*, Atom*, Atom*, XtPointer*,
				unsigned long*, int*);
static int CountLines(TextWidget, XawTextPosition, XawTextPosition);
static void CreateHScrollBar(TextWidget);
static void CreateVScrollBar(TextWidget);
static void CvtStringToScrollMode(XrmValuePtr, Cardinal*,
				  XrmValuePtr, XrmValuePtr);
static Boolean CvtScrollModeToString(Display*, XrmValue*, Cardinal*,
				     XrmValue*, XrmValue*, XtPointer*);
static void CvtStringToWrapMode(XrmValuePtr, Cardinal*,
				XrmValuePtr, XrmValuePtr);
static Boolean CvtWrapModeToString(Display*, XrmValue*, Cardinal*,
				   XrmValue*, XrmValue*, XtPointer*);
static Boolean CvtStringToJustifyMode(Display*, XrmValue*, Cardinal*,
				      XrmValue*, XrmValue*, XtPointer*);
static Boolean CvtJustifyModeToString(Display*, XrmValue*, Cardinal*,
				      XrmValue*, XrmValue*, XtPointer*);
static void DestroyHScrollBar(TextWidget);
static void DestroyVScrollBar(TextWidget);
#ifndef OLDXAW
static void DisplayText(Widget, XawTextPosition, XawTextPosition);
#endif
static void OldDisplayText(Widget, XawTextPosition, XawTextPosition);
static void DisplayTextWindow(Widget);
static void DoCopyArea(TextWidget, int, int, unsigned int, unsigned int,
		       int, int);
static void DoSelection(TextWidget, XawTextPosition, Time, Bool);
static void ExtendSelection(TextWidget, XawTextPosition, Bool);
static XawTextPosition FindGoodPosition(TextWidget, XawTextPosition);
static void FlushUpdate(TextWidget);
static int GetCutBufferNumber(Atom);
static int GetMaxTextWidth(TextWidget);
static unsigned int GetWidestLine(TextWidget);
static void HScroll(Widget, XtPointer, XtPointer);
static void HJump(Widget, XtPointer, XtPointer);
static void InsertCursor(Widget, XawTextInsertState);
static Bool LineAndXYForPosition(TextWidget, XawTextPosition, int*,
				 int*, int*);
static int LineForPosition(TextWidget, XawTextPosition);
static void TextLoseSelection(Widget, Atom*);
static Bool MatchSelection(Atom, XawTextSelection*);
static void ModifySelection(TextWidget, XawTextPosition, XawTextPosition);
static XawTextPosition PositionForXY(TextWidget, int, int);
static void PositionHScrollBar(TextWidget);
static void PositionVScrollBar(TextWidget);
#ifndef OLDXAW
static int ResolveColumnNumber(TextWidget);
static int ResolveLineNumber(TextWidget);
#endif
static void _SetSelection(TextWidget, XawTextPosition, XawTextPosition,
			  Atom*, Cardinal);
static void TextSinkResize(Widget);
static void UpdateTextInRectangle(TextWidget, XRectangle*);
static void UpdateTextInLine(TextWidget, int, int, int);
static void VScroll(Widget, XtPointer, XtPointer);
static void VJump(Widget, XtPointer, XtPointer);

/*
 * External
 */
void _XawTextAlterSelection(TextWidget,
			    XawTextSelectionMode, XawTextSelectionAction,
			    String*, Cardinal*);
void _XawTextCheckResize(TextWidget);
void _XawTextClearAndCenterDisplay(TextWidget);
void _XawTextExecuteUpdate(TextWidget);
char *_XawTextGetText(TextWidget, XawTextPosition, XawTextPosition);
void _XawTextPrepareToUpdate(TextWidget);
int _XawTextReplace(TextWidget, XawTextPosition, XawTextPosition,
		    XawTextBlock*);
Atom *_XawTextSelectionList(TextWidget, String*, Cardinal);
void _XawTextSetScrollBars(TextWidget);
void _XawTextSetSelection(TextWidget, XawTextPosition, XawTextPosition,
			  String*, Cardinal);
void _XawTextVScroll(TextWidget, int);
void XawTextScroll(TextWidget, int, int);
void _XawTextSetSource(Widget, Widget, XawTextPosition, XawTextPosition);
#ifndef OLDXAW
void _XawTextSetLineAndColumnNumber(TextWidget, Bool);
#endif
void _XawTextSourceChanged(Widget, XawTextPosition, XawTextPosition,
			   XawTextBlock*, int);

/* Not used by other modules, but were extern on previous versions
 * of the library
 */
void _XawTextShowPosition(TextWidget);

/*
 * From TextAction.c
 */
extern void _XawTextZapSelection(TextWidget, XEvent*, Bool);

/*
 * From TextSrc.c
 */
void _XawSourceAddText(Widget, Widget);
void _XawSourceRemoveText(Widget, Widget, Bool);
Bool _XawTextSourceNewLineAtEOF(Widget);

/*
 * From TextSink.c
 */
void _XawTextSinkClearToBackground(Widget, int, int, unsigned, unsigned);
void _XawTextSinkDisplayText(Widget, int, int, XawTextPosition, XawTextPosition,
			     Bool);

/****************************************************************
 *
 * Full class record constant
 *
 ****************************************************************/
/*
 * From TextTr.c
 */
extern char _XawDefaultTextTranslations[];

static XawTextSelectType defaultSelectTypes[] = {
  XawselectPosition,  XawselectAlphaNumeric, XawselectWord, XawselectLine,
  XawselectParagraph, XawselectAll,	     XawselectNull,
};

static XPointer defaultSelectTypesPtr = (XPointer)defaultSelectTypes;
static Dimension defWidth = 100;
static Dimension defHeight = DEFAULT_TEXT_HEIGHT;

#define offset(field) XtOffsetOf(TextRec, field)
static XtResource resources[] = {
  {
    XtNwidth,
    XtCWidth,
    XtRDimension,
    sizeof(Dimension),
    offset(core.width),
    XtRDimension,
    (XtPointer)&defWidth
  },
  {
    XtNcursor,
    XtCCursor,
    XtRCursor,
    sizeof(Cursor),
    offset(simple.cursor),
    XtRString,
    "xterm"
  },
  {
    XtNheight,
    XtCHeight,
    XtRDimension,
    sizeof(Dimension),
    offset(core.height),
    XtRDimension,
    (XtPointer)&defHeight
  },
  {
    XtNdisplayPosition,
    XtCTextPosition,
    XtRInt,
    sizeof(XawTextPosition), 
    offset(text.lt.top),
    XtRImmediate,
    (XtPointer)0
  },
  {
    XtNinsertPosition,
    XtCTextPosition,
    XtRInt,
    sizeof(XawTextPosition),
    offset(text.insertPos),
    XtRImmediate,
    (XtPointer)0
  },
  {
    XtNleftMargin,
    XtCMargin,
    XtRPosition,
    sizeof(Position),
    offset(text.r_margin.left),
    XtRImmediate,
    (XtPointer)2
  },
  {
    XtNrightMargin,
    XtCMargin,
    XtRPosition,
    sizeof(Position),
    offset(text.r_margin.right),
    XtRImmediate,
    (XtPointer)4
  },
  {
    XtNtopMargin,
    XtCMargin,
    XtRPosition,
    sizeof(Position),
    offset(text.r_margin.top),
    XtRImmediate,
    (XtPointer)2
  },
  {
    XtNbottomMargin,
    XtCMargin,
    XtRPosition,
    sizeof(Position),
    offset(text.r_margin.bottom),
    XtRImmediate,
    (XtPointer)2
  },
  {
    XtNselectTypes,
    XtCSelectTypes,
    XtRPointer,
    sizeof(XawTextSelectType*),
    offset(text.sarray),
    XtRPointer,
    (XtPointer)&defaultSelectTypesPtr
  },
  {
    XtNtextSource,
    XtCTextSource,
    XtRWidget,
    sizeof(Widget),
    offset(text.source),
    XtRImmediate,
    NULL
  },
  {
    XtNtextSink,
    XtCTextSink,
    XtRWidget,
    sizeof(Widget),
    offset(text.sink),
    XtRImmediate,
    NULL
  },
  {
    XtNdisplayCaret,
    XtCOutput,
    XtRBoolean,
    sizeof(Boolean),
    offset(text.display_caret),
    XtRImmediate,
    (XtPointer)True
  },
  {
    XtNscrollVertical,
    XtCScroll,
    XtRScrollMode,
    sizeof(XawTextScrollMode),
    offset(text.scroll_vert),
    XtRImmediate,
    (XtPointer)False
  },
  {
    XtNscrollHorizontal,
    XtCScroll,
    XtRScrollMode,
    sizeof(XawTextScrollMode),
    offset(text.scroll_horiz),
    XtRImmediate,
    (XtPointer)False
  },
  {
    XtNwrap,
    XtCWrap,
    XtRWrapMode,
    sizeof(XawTextWrapMode),
    offset(text.wrap),
    XtRImmediate,
    (XtPointer)XawtextWrapNever
  },
  {
    XtNautoFill,
    XtCAutoFill,
    XtRBoolean,
    sizeof(Boolean),
    offset(text.auto_fill),
    XtRImmediate,
    (XtPointer)False
  },
#ifndef OLDXAW
  {
    XtNpositionCallback,
    XtCCallback,
    XtRCallback,
    sizeof(XtPointer),
    offset(text.position_callbacks),
    XtRCallback,
    NULL
  },
  {
    XtNleftColumn,
    XtCColumn,
    XtRShort,
    sizeof(short),
    offset(text.left_column),
    XtRImmediate,
    (XtPointer)0
  },
  {
    XtNrightColumn,
    XtCColumn,
    XtRShort,
    sizeof(short),
    offset(text.right_column),
    XtRImmediate,
    (XtPointer)0
  },
  {
    XtNjustifyMode,
    XtCJustifyMode,
    XtRJustifyMode,
    sizeof(XawTextJustifyMode),
    offset(text.justify),
    XtRImmediate,
    (XtPointer)XawjustifyLeft
  },
#endif /* OLDXAW */
};
#undef offset

#define done(address, type) \
	{ toVal->size = sizeof(type); toVal->addr = (XPointer)address; }

static XrmQuark QWrapNever, QWrapLine, QWrapWord;
#ifndef notdef
static XrmQuark QScrollNever, QScrollWhenNeeded, QScrollAlways;
#endif
static XrmQuark QJustifyLeft, QJustifyRight, QJustifyCenter, QJustifyFull;

/*ARGSUSED*/
static void
CvtStringToScrollMode(XrmValuePtr args, Cardinal *num_args,
		      XrmValuePtr fromVal, XrmValuePtr toVal)
{
    static XawTextScrollMode scrollMode = XawtextScrollNever;
    XrmQuark q;
    char name[32];

    XmuNCopyISOLatin1Lowered(name, (char *)fromVal->addr, sizeof(name));
    q = XrmStringToQuark(name);

    if (q == QScrollNever || q == QScrollWhenNeeded)
	scrollMode = XawtextScrollNever;
    else if (q == QScrollAlways)
	scrollMode = XawtextScrollAlways;
    else if (strcmp(name, "true") == 0 || strcmp(name, "1") == 0)
	scrollMode = XawtextScrollAlways;
    else if (strcmp(name, "false") == 0 || strcmp(name, "0") == 0)
	scrollMode = XawtextScrollNever;
    else
	XtStringConversionWarning((char *)fromVal->addr, XtRScrollMode);

    done(&scrollMode, XawTextScrollMode);
}

/*ARGSUSED*/
static Boolean
CvtScrollModeToString(Display *dpy, XrmValue *args, Cardinal *num_args,
		      XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
{
    static char *buffer;
    Cardinal size;

    switch (*(XawTextScrollMode *)fromVal->addr) {
	case XawtextScrollNever:
	case XawtextScrollWhenNeeded:
	    buffer = XtEtextScrollNever;
	    break;
	case XawtextScrollAlways:
	    buffer = XtEtextScrollAlways;
	    break;
	default:
	    XawTypeToStringWarning(dpy, XtRScrollMode);
	    toVal->addr = NULL;
	    toVal->size = 0;
	    return (False);
    }
    size = strlen(buffer) + 1;
    if (toVal->addr != NULL) {
	if (toVal->size < size)	{
	    toVal->size = size;
	    return (False);
	}
	strcpy((char *)toVal->addr, buffer);
    }
    else
	toVal->addr = (XPointer)buffer;
    toVal->size = sizeof(String);

    return (True);
}

/*ARGSUSED*/
static void
CvtStringToWrapMode(XrmValuePtr args, Cardinal *num_args,
		    XrmValuePtr fromVal, XrmValuePtr toVal)
{
    static XawTextWrapMode wrapMode = XawtextWrapNever;
    XrmQuark q;
    char lowerName[6];

    XmuNCopyISOLatin1Lowered(lowerName, (char *)fromVal->addr,
			     sizeof(lowerName));
    q = XrmStringToQuark(lowerName);

    if (q == QWrapNever)
	wrapMode = XawtextWrapNever;
    else if (q == QWrapLine)
	wrapMode = XawtextWrapLine;
    else if (q == QWrapWord)
	wrapMode = XawtextWrapWord;
    else
	XtStringConversionWarning((char *)fromVal->addr, XtRWrapMode);

    done(&wrapMode, XawTextWrapMode);
}

/*ARGSUSED*/
static Boolean
CvtWrapModeToString(Display *dpy, XrmValue *args, Cardinal *num_args,
		    XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
{
    static char *buffer;
    Cardinal size;

    switch (*(XawTextWrapMode *)fromVal->addr) {
	case XawtextWrapNever:
	    buffer = XtEtextWrapNever;
	    break;
	case XawtextWrapLine:
	    buffer = XtEtextWrapLine;
	    break;
	case XawtextWrapWord:
	    buffer = XtEtextWrapWord;
	    break;
	default:
	    XawTypeToStringWarning(dpy, XtRWrapMode);
	    toVal->addr = NULL;
	    toVal->size = 0;
	    return (False);
    }
    size = strlen(buffer) + 1;
    if (toVal->addr != NULL) {
	if (toVal->size < size)	{
	    toVal->size = size;
	    return (False);
	}
	strcpy((char *)toVal->addr, buffer);
    }
    else
	toVal->addr = (XPointer)buffer;
    toVal->size = sizeof(String);

    return (True);
}

/*ARGSUSED*/
static Boolean
CvtStringToJustifyMode(Display *dpy, XrmValue *args, Cardinal *num_args,
		       XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
{
    XawTextJustifyMode justify;
    XrmQuark q;
    char lowerName[8];

    XmuNCopyISOLatin1Lowered(lowerName, (char *)fromVal->addr,
			   sizeof(lowerName));
    q = XrmStringToQuark(lowerName);

    if (q == QJustifyLeft)
	justify = XawjustifyLeft;
    else if (q == QJustifyRight)
	justify = XawjustifyRight;
    else if (q == QJustifyCenter)
	justify = XawjustifyCenter;
    else if(q ==  QJustifyFull)
	justify = XawjustifyFull;
    else {
	XtStringConversionWarning((char *)fromVal->addr, XtRJustifyMode);
	return (False);
    }

    toVal->size = sizeof(XawTextJustifyMode);
    *(XawTextJustifyMode *)(toVal->addr) = justify;

    return (True);
}


/*ARGSUSED*/
static Boolean
CvtJustifyModeToString(Display *dpy, XrmValue *args, Cardinal *num_args,
		       XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
{
    static char *buffer;
    Cardinal size;

    switch (*(XawTextJustifyMode *)fromVal->addr) {
	case XawjustifyLeft:
	    buffer = XtEtextJustifyLeft;
	    break;
	case XawjustifyRight:
	    buffer = XtEtextJustifyRight;
	    break;
	case XawjustifyCenter:
	    buffer = XtEtextJustifyCenter;
	    break;
	case XawjustifyFull:
	    buffer = XtEtextJustifyFull;
	    break;
	default:
	    XawTypeToStringWarning(dpy, XtRJustifyMode);
	    toVal->addr = NULL;
	    toVal->size = 0;
	    return (False);
    }
    size = strlen(buffer) + 1;
    if (toVal->addr != NULL) {
	if (toVal->size < size)	{
	    toVal->size = size;
	    return (False);
	}
	strcpy((char *)toVal->addr, buffer);
    }
    else
	toVal->addr = (XPointer)buffer;
    toVal->size = sizeof(String);

    return (True);
}

#undef done

static void
XawTextClassInitialize(void)
{
    if (!XawFmt8Bit)
	FMT8BIT = XawFmt8Bit = XrmPermStringToQuark("FMT8BIT");
    if (!XawFmtWide)
	XawFmtWide = XrmPermStringToQuark("FMTWIDE");

    XawInitializeWidgetSet();

    textClassRec.core_class.num_actions = _XawTextActionsTableCount;
  
    QWrapNever	= XrmPermStringToQuark(XtEtextWrapNever);
    QWrapLine	= XrmPermStringToQuark(XtEtextWrapLine);
    QWrapWord	= XrmPermStringToQuark(XtEtextWrapWord);
    XtAddConverter(XtRString, XtRWrapMode, CvtStringToWrapMode, NULL, 0);
    XtSetTypeConverter(XtRWrapMode, XtRString, CvtWrapModeToString,
		       NULL, 0, XtCacheNone, NULL);
    QScrollNever = XrmPermStringToQuark(XtEtextScrollNever);
    QScrollWhenNeeded = XrmPermStringToQuark(XtEtextScrollWhenNeeded);
    QScrollAlways = XrmPermStringToQuark(XtEtextScrollAlways);
    XtAddConverter(XtRString, XtRScrollMode, CvtStringToScrollMode,
		   NULL, 0);
    XtSetTypeConverter(XtRScrollMode, XtRString, CvtScrollModeToString,
		       NULL, 0, XtCacheNone, NULL);
    QJustifyLeft   = XrmPermStringToQuark(XtEtextJustifyLeft);
    QJustifyRight  = XrmPermStringToQuark(XtEtextJustifyRight);
    QJustifyCenter = XrmPermStringToQuark(XtEtextJustifyCenter);
    QJustifyFull   = XrmPermStringToQuark(XtEtextJustifyFull);
    XtSetTypeConverter(XtRString, XtRJustifyMode, CvtStringToJustifyMode,
		       NULL, 0, XtCacheNone, NULL);
    XtSetTypeConverter(XtRJustifyMode, XtRString, CvtJustifyModeToString,
		       NULL, 0, XtCacheNone, NULL);
}

/*
 * Function:
 *	PositionHScrollBar
 *
 * Parameters:
 *	ctx - text widget
 *
 * Description:
 *	Positions the Horizontal scrollbar.
 */
static void
PositionHScrollBar(TextWidget ctx)
{
    Widget hbar = ctx->text.hbar, vbar = ctx->text.vbar;
    Position x, y;
    Dimension width, height;

    if (ctx->text.hbar == NULL)
	return;

    if (vbar != NULL)
	x = XtWidth(vbar);
    else
	x = -XtBorderWidth(hbar);
    y = XtHeight(ctx) - XtHeight(hbar) - XtBorderWidth(hbar);
    if (vbar != NULL) {
	width = XtWidth(ctx) - XtWidth(vbar) - XtBorderWidth(vbar);
	if (width > XtWidth(ctx))
	    width = XtWidth(ctx);
    }
    else
	width = XtWidth(ctx);
    height = XtHeight(hbar);

    XtConfigureWidget(hbar, x, y, width, height, XtBorderWidth(hbar));
}

/*
 * Function:
 *	PositionVScrollBar
 *
 * Parameters:
 *	ctx - text widget
 *
 * Description:
 *	Positions the Vertical scrollbar.
 */
static void
PositionVScrollBar(TextWidget ctx)
{
    Widget vbar = ctx->text.vbar;
    Position x, y;
    Dimension width, height;

    if (vbar == NULL)
	return;

    x = y = -XtBorderWidth(vbar);
    height = XtHeight(ctx);
    width = XtWidth(vbar);

    XtConfigureWidget(vbar, x, y, width, height, XtBorderWidth(vbar));
}

static void
CreateVScrollBar(TextWidget ctx)
{
    Widget vbar;

    if (ctx->text.vbar != NULL)
	return;

    ctx->text.vbar = vbar =
	XtCreateWidget("vScrollbar", scrollbarWidgetClass, (Widget)ctx, NULL, 0);
    XtAddCallback(vbar, XtNscrollProc, VScroll, (XtPointer)ctx);
    XtAddCallback(vbar, XtNjumpProc, VJump, (XtPointer)ctx);

    ctx->text.r_margin.left += XtWidth(vbar) + XtBorderWidth(vbar);
    ctx->text.left_margin = ctx->text.margin.left = ctx->text.r_margin.left;

    PositionVScrollBar(ctx);
    PositionHScrollBar(ctx);
    TextSinkResize(ctx->text.sink);

    if (XtIsRealized((Widget)ctx)) {
	XtRealizeWidget(vbar);
	XtMapWidget(vbar);
    }
    XtSetKeyboardFocus(vbar, (Widget)ctx);
}

/*
 * Function:
 *	DestroyVScrollBar
 *
 * Parameters:
 *	ctx - parent text widget
 *
 * Description:
 *	Removes vertical ScrollBar.
 */
static void
DestroyVScrollBar(TextWidget ctx)
{
    Widget vbar = ctx->text.vbar;

    if (vbar == NULL)
	return;

    ctx->text.r_margin.left -= XtWidth(vbar) + XtBorderWidth(vbar);
    ctx->text.left_margin = ctx->text.margin.left = ctx->text.r_margin.left;

    XtDestroyWidget(vbar);
    ctx->text.vbar = NULL;
    if (!ctx->core.being_destroyed) {
	PositionHScrollBar(ctx);
	TextSinkResize(ctx->text.sink);
    }
}

static void
CreateHScrollBar(TextWidget ctx)
{
    Arg args[1];
    Widget hbar;
    int bottom;

    if (ctx->text.hbar != NULL)
	return;

    XtSetArg(args[0], XtNorientation, XtorientHorizontal);
    ctx->text.hbar = hbar =
	XtCreateWidget("hScrollbar", scrollbarWidgetClass, (Widget)ctx, args, 1);
    XtAddCallback(hbar, XtNscrollProc, HScroll, (XtPointer)ctx);
    XtAddCallback(hbar, XtNjumpProc, HJump, (XtPointer)ctx);

    bottom = ctx->text.r_margin.bottom + XtHeight(hbar) + XtBorderWidth(hbar);

    ctx->text.margin.bottom = ctx->text.r_margin.bottom = bottom;

    PositionHScrollBar(ctx);
    TextSinkResize(ctx->text.sink);

    if (XtIsRealized((Widget)ctx)) {
	XtRealizeWidget(hbar);
	XtMapWidget(hbar);
    }
    XtSetKeyboardFocus(hbar, (Widget)ctx);
}

/*
 * Function:
 *	DestroyHScrollBar
 *
 * Parameters:
 *	ctx - parent text widget
 *
 * Description:
 *	Removes horizontal ScrollBar.
 */
static void
DestroyHScrollBar(TextWidget ctx)
{
    Widget hbar = ctx->text.hbar;

    if (hbar == NULL)
	return;

    ctx->text.r_margin.bottom -= XtHeight(hbar) + XtBorderWidth(hbar);
    ctx->text.margin.bottom = ctx->text.r_margin.bottom;

    XtDestroyWidget(hbar);
    ctx->text.hbar = NULL;
    if (!ctx->core.being_destroyed)
	TextSinkResize(ctx->text.sink);
}

/*ARGSUSED*/
static void
XawTextInitialize(Widget request, Widget cnew,
		  ArgList args, Cardinal *num_args)
{
    TextWidget ctx = (TextWidget)cnew;

    ctx->text.lt.lines = 0;
    ctx->text.lt.info = (XawTextLineTableEntry *)
	XtCalloc(1, sizeof(XawTextLineTableEntry));
#ifndef OLDXAW
    ctx->text.lt.base_line = 1;
#endif
    (void)bzero(&ctx->text.origSel, sizeof(XawTextSelection));
    (void)bzero(&ctx->text.s, sizeof(XawTextSelection));
    ctx->text.s.type = XawselectPosition;
    ctx->text.salt = NULL;
    ctx->text.hbar = ctx->text.vbar = NULL;
    ctx->text.lasttime = 0;
    ctx->text.time = 0;
    ctx->text.showposition = True;
    ctx->text.lastPos = ctx->text.source != NULL ?
			XawTextGetLastPosition(ctx) : 0;
    ctx->text.file_insert = NULL;
    ctx->text.search = NULL;
    ctx->text.update = XmuNewScanline(0, 0, 0);
    ctx->text.gc = XtGetGC(cnew, 0, 0);
    ctx->text.hasfocus = False;
    ctx->text.margin = ctx->text.r_margin; /* Strucure copy */
    ctx->text.left_margin = ctx->text.r_margin.left;
    ctx->text.update_disabled = False;
    ctx->text.clear_to_eol = True;
    ctx->text.old_insert = -1;
    ctx->text.mult = 1;
    ctx->text.salt2 = NULL;
    ctx->text.from_left = -1;

#ifndef OLDXAW
    ctx->text.numeric = False;
    ctx->text.selection_state = False;
    ctx->text.kill_ring = 0;

    ctx->text.line_number = -1;
    ctx->text.column_number = -1;
    ctx->text.source_changed = SRC_CHANGE_NONE;

    ctx->text.kill_ring_ptr = NULL;
    ctx->text.overwrite = False;
#endif

    if (XtHeight(ctx) == DEFAULT_TEXT_HEIGHT) {
	XtHeight(ctx) = VMargins(ctx);
	if (ctx->text.sink != NULL)
	    XtHeight(ctx) += XawTextSinkMaxHeight(ctx->text.sink, 1);
    }

    if (ctx->text.scroll_vert == XawtextScrollAlways)
	CreateVScrollBar(ctx);
    if (ctx->text.scroll_horiz == XawtextScrollAlways)
	CreateHScrollBar(ctx);

#ifndef OLDXAW
    if (ctx->text.left_column < 0)
	ctx->text.left_column = 0;
    if (ctx->text.right_column < 0)
	ctx->text.right_column = 0;
#endif
}

static void
XawTextRealize(Widget w, XtValueMask *mask, XSetWindowAttributes *attr)
{
    TextWidget ctx = (TextWidget)w;

    (*textClassRec.core_class.superclass->core_class.realize)(w, mask, attr);
  
    if (ctx->text.hbar != NULL) {
	XtRealizeWidget(ctx->text.hbar);
	XtMapWidget(ctx->text.hbar);
    }

    if (ctx->text.vbar != NULL) {
	XtRealizeWidget(ctx->text.vbar);
	XtMapWidget(ctx->text.vbar);
    }

    _XawTextBuildLineTable(ctx, ctx->text.lt.top, True);

#ifndef OLDXAW
    _XawTextSetLineAndColumnNumber(ctx, True);
#endif
}

/* Utility routines for support of Text */
static void
_CreateCutBuffers(Display *d)
{
    static struct _DisplayRec {
	struct _DisplayRec *next;
	Display *dpy;
    } *dpy_list = NULL;
    struct _DisplayRec *dpy_ptr;

    for (dpy_ptr = dpy_list; dpy_ptr != NULL; dpy_ptr = dpy_ptr->next)
	if (dpy_ptr->dpy == d)
	    return;

    dpy_ptr = XtNew(struct _DisplayRec);
    dpy_ptr->next = dpy_list;
    dpy_ptr->dpy = d;
    dpy_list = dpy_ptr;

#define Create(buffer) \
  XChangeProperty(d, RootWindow(d, 0), buffer, XA_STRING, 8, \
		  PropModeAppend, NULL, 0);

    Create(XA_CUT_BUFFER0);
    Create(XA_CUT_BUFFER1);
    Create(XA_CUT_BUFFER2);
    Create(XA_CUT_BUFFER3);
    Create(XA_CUT_BUFFER4);
    Create(XA_CUT_BUFFER5);
    Create(XA_CUT_BUFFER6);
    Create(XA_CUT_BUFFER7);

#undef Create
}

/*
 * Procedure to manage insert cursor visibility for editable text.  It uses
 * the value of ctx->insertPos and an implicit argument. In the event that
 * position is immediately preceded by an eol graphic, then the insert cursor
 * is displayed at the beginning of the next line.
 */
static void
InsertCursor(Widget w, XawTextInsertState state)
{
    TextWidget ctx = (TextWidget)w;
    int x, y;
    int line;

    if (ctx->text.lt.lines < 1)
	return;

    if (ctx->text.display_caret &&
	LineAndXYForPosition(ctx, ctx->text.insertPos, &line, &x, &y)) {
	if (line < ctx->text.lt.lines)
	    y += (ctx->text.lt.info[line + 1].y - ctx->text.lt.info[line].y) + 1;
	else
	    y += (ctx->text.lt.info[line].y - ctx->text.lt.info[line - 1].y) + 1;

	XawTextSinkInsertCursor(ctx->text.sink, x, y, state);
    }

    /* Keep Input Method up to speed  */
    if (ctx->simple.international) {
	Arg list[1];

	XtSetArg(list[0], XtNinsertPosition, ctx->text.insertPos);
	_XawImSetValues(w, list, 1);
    }
}

/*
 * Procedure to register a span of text that is no longer valid on the display
 * It is used to avoid a number of small, and potentially overlapping, screen
 * updates.
*/
void
_XawTextNeedsUpdating(TextWidget ctx,
		      XawTextPosition left, XawTextPosition right)
{
    XmuSegment segment;

    if (left >= right)
	return;

    segment.x1 = (int)left;
    segment.x2 = (int)right;
    (void)XmuScanlineOrSegment(ctx->text.update, &segment);
}

/*
 * Procedure to read a span of text in Ascii form. This is purely a hack and
 * we probably need to add a function to sources to provide this functionality.
 * [note: this is really a private procedure but is used in multiple modules].
 */
char *
_XawTextGetText(TextWidget ctx, XawTextPosition left, XawTextPosition right)
{
    char *result, *tempResult;
    XawTextBlock text;
    int bytes;

    if (XawTextFormat(ctx, XawFmt8Bit))
	bytes = sizeof(unsigned char);
    else if (XawTextFormat(ctx, XawFmtWide)) 
	bytes = sizeof(wchar_t);
    else /* if there is another fomat, add here */
	bytes = 1;

    /* leave space for ZERO */
    tempResult = result = XtMalloc((unsigned)(right - left + ONE) * bytes);

    while (left < right) {
	left = SrcRead(ctx->text.source, left, &text, (int)(right - left));
	if (!text.length)
	    break;
	memmove(tempResult, text.ptr, (unsigned)(text.length * bytes));
	tempResult += text.length * bytes;
    }

    if (bytes == sizeof(wchar_t))
	*((wchar_t*)tempResult) = (wchar_t)0;
    else
	*tempResult = '\0';

    return (result);
}

/* Like _XawTextGetText, but enforces ICCCM STRING type encoding.  This
 * routine is currently used to put just the ASCII chars in the selection
 * into a cut buffer.
 */
char *
_XawTextGetSTRING(TextWidget ctx, XawTextPosition left, XawTextPosition right)
{
    unsigned char *s;
    unsigned char c;
    long i, j, n;
    wchar_t *ws, wc;

    /* allow ESC in accordance with ICCCM */
    if (XawTextFormat(ctx, XawFmtWide)) {
	MultiSinkObject sink = (MultiSinkObject)ctx->text.sink;
	ws = (wchar_t *)_XawTextGetText(ctx, left, right);
	n = wcslen(ws);
	for (j = 0, i = 0; j < n; j++) {
	    wc = ws[j];
	    if (XwcTextEscapement (sink->multi_sink.fontset, &wc, 1)
		|| (wc == _Xaw_atowc(XawTAB)) || (wc == _Xaw_atowc(XawLF))
		|| (wc == _Xaw_atowc(XawESC)))
		ws[i++] = wc;
	}
	ws[i] = (wchar_t)0;
	return ((char *)ws);
    }
    else {
	s = (unsigned char *)_XawTextGetText(ctx, left, right);
	/* only HT and NL control chars are allowed, strip out others */
	n = strlen((char *)s);
	i = 0;
	for (j = 0; j < n; j++)	{
	    c = s[j];
	    if (((c >= 0x20) && c <= 0x7f)
		||(c >= 0xa0) || (c == XawTAB) || (c == XawLF)
		|| (c == XawESC)) {
		s[i] = c;
		i++;
	    }
	}
	s[i] = 0;

	return ((char *)s);
    }
}

/* 
 * This routine maps an x and y position in a window that is displaying text
 * into the corresponding position in the source.
 */
static XawTextPosition
PositionForXY(TextWidget ctx, int x, int y)
{
    int fromx, line, width, height;
    XawTextPosition position;

    if (ctx->text.lt.lines == 0)
	return (0);

    for (line = 0; line < ctx->text.lt.lines - 1; line++) {
	if (y <= ctx->text.lt.info[line + 1].y)
	    break;
    }
    position = ctx->text.lt.info[line].position;
    if (position >= ctx->text.lastPos)
	return (ctx->text.lastPos);
    fromx = ctx->text.left_margin;
    XawTextSinkFindPosition(ctx->text.sink, position, fromx, x - fromx,
			    False, &position, &width, &height);

    if (position > ctx->text.lastPos)
	return (ctx->text.lastPos);

    if (position >= ctx->text.lt.info[line + 1].position)
	position = SrcScan(ctx->text.source, ctx->text.lt.info[line + 1].position,
			   XawstPositions, XawsdLeft, 1, True);

    return (position);
}

/*
 * This routine maps a source position in to the corresponding line number
 * of the text that is displayed in the window.
 */
static int
LineForPosition(TextWidget ctx, XawTextPosition position)
{
    int line;

    for (line = 0; line < ctx->text.lt.lines; line++)
	if (position < ctx->text.lt.info[line + 1].position)
	    break;

    return (line);
}

/*
 * This routine maps a source position into the corresponding line number
 * and the x, y coordinates of the text that is displayed in the window.
 */
static Bool
LineAndXYForPosition(TextWidget ctx, XawTextPosition pos,
		     int *line, int *x, int *y)
{
    XawTextPosition linePos, endPos;
    Boolean visible;
    int realW, realH;

    *line = 0;
    *x = ctx->text.left_margin;
    *y = ctx->text.margin.top + 1;
    if ((visible = IsPositionVisible(ctx, pos)) != False) {
	*line = LineForPosition(ctx, pos);
	*y = ctx->text.lt.info[*line].y;
	linePos = ctx->text.lt.info[*line].position;
	XawTextSinkFindDistance(ctx->text.sink, linePos,
				*x, pos, &realW, &endPos, &realH);
	*x += realW;
    }

    return (visible);
}

/*
 * This routine builds a line table. It does this by starting at the
 * specified position and measuring text to determine the staring position
 * of each line to be displayed. It also determines and saves in the
 * linetable all the required metrics for displaying a given line (e.g.
 * x offset, y offset, line length, etc.).
 */
void
_XawTextBuildLineTable(TextWidget ctx, XawTextPosition position,
		       _XtBoolean force_rebuild)
{
    Dimension height = 0;
    int lines = 0;
    Cardinal size;

    if ((int)XtHeight(ctx) > VMargins(ctx)) {
	height = XtHeight(ctx) - VMargins(ctx);
	lines = XawTextSinkMaxLines(ctx->text.sink, height);
    }
    size = sizeof(XawTextLineTableEntry) * (lines + 1);

    if (lines != ctx->text.lt.lines || ctx->text.lt.info == NULL) {
	ctx->text.lt.info = (XawTextLineTableEntry *)
	    XtRealloc((char *)ctx->text.lt.info, size);
	ctx->text.lt.lines = lines;
	force_rebuild = True;
    }

    if (force_rebuild) {
	(void)bzero((char *)ctx->text.lt.info, size);
	/* force a text update in the first text line if it is visible */
	ctx->text.lt.info[0].position = (XawTextPosition)-1;
    }
    if (position != ctx->text.lt.info[0].position) {
	(void)_BuildLineTable(ctx, position, 0);
	ctx->text.clear_to_eol = True;
    }
}

/*
 * We may need to resize the line table here, since there maybe lines with
 * different fonts (that can be shorter or taller than the default one)
 */
static XawTextPosition
_BuildLineTable(TextWidget ctx, XawTextPosition position, int line)
{
    XawTextLineTableEntry *lt = ctx->text.lt.info + line;
    XawTextPosition end, update_from = -1;
    Position y;
    int wwidth, width, height;
#ifndef OLDXAW
    Widget src = ctx->text.source;
#endif
    int max_y = (int)XtHeight(ctx) - (int)ctx->text.margin.bottom;

    if (ctx->text.wrap == XawtextWrapNever)
	wwidth = 0x7fffffff;
    else
	wwidth = GetMaxTextWidth(ctx);

    /* XXX y may change, due to font size changes. See later */
    y = line == 0 ? ctx->text.margin.top : lt->y;

#ifndef OLDXAW
    if (ctx->text.lt.base_line < 0) {
	if (line == 0)
	    ctx->text.lt.top = position;
    }
    else if (line == 0) {
	XawTextPosition pos = ctx->text.lt.top;
	int base_line = ctx->text.lt.base_line;

	if (position == 0)
	    base_line = 1;
	else if (ctx->text.lt.base_line == 0 ||
		 ctx->text.source_changed == SRC_CHANGE_OVERLAP) {
	    pos = 0;
	    base_line = 1;

	    while (pos < position) {
		pos = SrcScan(src, pos, XawstEOL, XawsdRight, 1, True);
		if (pos <= position) {
		    ++base_line;
		    if (pos == ctx->text.lastPos) {
			base_line -= !_XawTextSourceNewLineAtEOF(src);
			break;
		    }
		}
	    }
	}
	else if (ctx->text.wrap == XawtextWrapNever
		 && IsPositionVisible(ctx, position))
	    base_line += LineForPosition(ctx, position);
	else if (pos < position) {
	    while (pos < position) {
		pos = SrcScan(src, pos, XawstEOL, XawsdRight, 1, True);
		if (pos <= position) {
		    ++base_line;
		    if (pos == ctx->text.lastPos) {
			base_line -= !_XawTextSourceNewLineAtEOF(src);
			break;
		    }
		}
	    }
	}
	else if (pos > position) {
	    while (pos > position) {
		pos = SrcScan(src, pos, XawstEOL, XawsdLeft, 1, False);
		if (--pos >= position)
		    --base_line;
	    }
	}

	ctx->text.lt.top = position;
	ctx->text.lt.base_line = base_line;
    }
#else
    if (line == 0)
	ctx->text.lt.top = position;
#endif

    /* CONSTCOND */
    while (True) {
	XawTextSinkFindPosition(ctx->text.sink, position, ctx->text.left_margin,
				wwidth, ctx->text.wrap == XawtextWrapWord,
				&end, &width, &height);

	if (lt->position != position) {
	    _XawTextNeedsUpdating(ctx, position,
				  end <= position ? position + 1 : end);
	    ctx->text.clear_to_eol = True;
	    lt->position = position;
	}
	if (lt->y != y) {
	    if (update_from < 0)
		update_from = line == 0 ?
		    ctx->text.lt.info[0].position :
		    ctx->text.lt.info[line - 1].position;
	    lt->y = y;
	    ctx->text.clear_to_eol = True;
	}
	if (lt->textWidth != width) {
	    if (lt->textWidth > width)
		ctx->text.clear_to_eol = True;
	    lt->textWidth = width;
	}
	y += height;

	if (end > ctx->text.lastPos) {
	    position = end;
	    ctx->text.clear_to_eol = True;
	    _XawTextNeedsUpdating(ctx, end, end + ctx->text.lt.lines - line);
	    while (line++ < ctx->text.lt.lines) {
		if (line > 1 && y > max_y) {
		    ctx->text.lt.lines = line - 1;
		    break;
		}
		++lt;
		if (lt->y != y) {
		    if (update_from < 0)
			update_from = line < 2 ?
			    ctx->text.lt.info[0].position :
			    ctx->text.lt.info[line - 2].position;
		    lt->y = y;
		}
		lt->position = ++position;
		lt->textWidth = 0;
		y += height;
	    }
	    if (update_from >= 0)
		_XawTextNeedsUpdating(ctx, update_from,
				      ctx->text.lt.info[ctx->text.lt.lines].position);
	    _XawTextSetScrollBars(ctx);

	    return (ctx->text.lastPos);
	}

	if (line && y > max_y)
	    /* will return in the next loop */
	    ctx->text.lt.lines = line;

	if (++line > ctx->text.lt.lines && y < max_y) {
	/* grow the line table */
	    ctx->text.lt.info = (XawTextLineTableEntry *)
		XtRealloc((char *)ctx->text.lt.info,
			  sizeof(XawTextLineTableEntry) * (line + 1));
	    lt = ctx->text.lt.info + line;
	    bzero(lt, sizeof(XawTextLineTableEntry));
	    ++ctx->text.lt.lines;
	}
	else
	    ++lt;
	if (position == end)
	    ++position;
	else
	    position = end;

	if (line > ctx->text.lt.lines) {
	    if (update_from >= 0)
		_XawTextNeedsUpdating(ctx, update_from,
				      ctx->text.lt.info[ctx->text.lt.lines].position);
	    _XawTextSetScrollBars(ctx);

	    return (position);
	}
    }
    /*NOTREACHED*/
}

/*
 * Function:
 *	GetWidestLine
 *
 * Parameters:
 *	ctx - text widget
 *
 * Description:
 *	  Returns the width (in pixels) of the widest line that
 *		     is currently visable.
 *
 * Returns:
 *	The width of the widest line
 */
static unsigned int
GetWidestLine(TextWidget ctx)
{
    int i;
    unsigned int widest;
    XawTextLineTablePtr lt = &(ctx->text.lt);

    for (i = 0, widest = 0; i < lt->lines; i++)
	if (widest < lt->info[i].textWidth)
	    widest = lt->info[i].textWidth;

    return (widest);
}

/*
 * This routine is used by Text to notify an associated scrollbar of the
 * correct metrics (position and shown fraction) for the text being currently
 * displayed in the window.
 */
void
_XawTextSetScrollBars(TextWidget ctx)
{
    float first, last, denom, widest;

    if (ctx->text.scroll_vert == XawtextScrollAlways) {
	if (ctx->text.lastPos == 0)
	    first = 0.0;
	else
	    first = ctx->text.lt.top / (float)ctx->text.lastPos;

	if (ctx->text.lt.info[ctx->text.lt.lines].position < ctx->text.lastPos)
	    last = ctx->text.lt.info[ctx->text.lt.lines].position /
		   (float)ctx->text.lastPos;
	else
	    last = 1.0;

	XawScrollbarSetThumb(ctx->text.vbar, first, last - first);
    }

    if (ctx->text.scroll_horiz == XawtextScrollAlways) {
	denom = GetWidestLine(ctx);
	if (denom <= 0)
	    denom = (int)XtWidth(ctx) - RHMargins(ctx);
	if (denom <= 0)
	    denom = 1;
	widest = ((int)XtWidth(ctx) - RHMargins(ctx)) / denom;
	first = ctx->text.r_margin.left - ctx->text.left_margin;
	first /= denom;

	XawScrollbarSetThumb(ctx->text.hbar, first, widest);
    }
}

static void
DoCopyArea(TextWidget ctx, int src_x, int src_y,
	   unsigned int width, unsigned int height, int dst_x, int dst_y)
{
    int x1, y1, x2, y2;

    x1 = ctx->text.r_margin.left;
    y1 = ctx->text.r_margin.top;
    x2 = XtWidth(ctx) - ctx->text.r_margin.right;
    y2 = XtHeight(ctx) - ctx->text.r_margin.bottom;

    if (x1 >= x2 || y1 >= y2)
	return;

    src_x = XawMax(x1, XawMin(src_x, x2));
    src_y = XawMax(y1, XawMin(src_y, y2));
    dst_x = XawMax(x1, XawMin(dst_x, x2));
    dst_y = XawMax(y1, XawMin(dst_y, y2));
    width = XawMax(0, XawMin(x2 - dst_x, (int)width));
    height = XawMax(0, XawMin(y2 - dst_y, (int)height));

    XCopyArea(XtDisplay(ctx), XtWindow(ctx), XtWindow(ctx), ctx->text.gc,
	      src_x, src_y, width, height, dst_x, dst_y);
}

/*
 * Function:
 *	XawTextScroll
 *
 * Parameters:
 *	ctx	- text widget
 *	vlines	- number of lines to scroll vertically
 *	hpixels	- number of pixels to scroll horizontally
 *
 * Description:
 *	Generic function for scrolling the text window.
 *	Allows vertical and horizontal scroll at the same time.
 */
void
XawTextScroll(TextWidget ctx, int vlines, int hpixels)
{
    XawTextPosition top, tmp, update_from, update_to;
    XawTextLineTable *lt;
    Arg arglist[1];
    int y0, y1, y2, count, dim, wwidth, lines = ctx->text.lt.lines;
    int vwidth, vheight;	/* visible width and height */
    Bool scroll;

    vwidth = (int)XtWidth(ctx) - RHMargins(ctx);
    vheight = (int)XtHeight(ctx) - RVMargins(ctx);
    lt = &ctx->text.lt;

    if (!lt || vwidth <= 0 || vheight <= 0)
	return;

    if ((scroll = ctx->core.background_pixmap == XtUnspecifiedPixmap) == True) {
	dim = lt->info[1].y - lt->info[0].y;
	for (count = 1; count < lt->lines - 1; count++)
	    if (lt->info[count + 1].y - lt->info[count].y != dim) {
		scroll = False;
		break;
	    }
    }

    wwidth = GetMaxTextWidth(ctx);

    /*
     * Do the horizontall scrolling
     */
    if (hpixels < 0 && ctx->text.left_margin - hpixels > ctx->text.r_margin.left)
	hpixels = ctx->text.left_margin - ctx->text.r_margin.left;
    ctx->text.left_margin -= hpixels;

    update_from = lt->top;	/* remember the old value */
    /*
     *	 Checks the requested number of lines and calculates the top
     * of the line table
     */
    if (vlines < 0) {		  /* VScroll Up */
	if (IsPositionVisible(ctx, 0))
	    vlines = 0;
	else if (ctx->text.wrap != XawtextWrapNever) {
	    XawTextPosition end;
	    int n_lines = 0;

	    count = -vlines;
	    end = lt->top;
	    while (n_lines < count) {
		top = SrcScan(ctx->text.source, end, XawstEOL,
			      XawsdLeft, 2, False);
		n_lines += CountLines(ctx, top, end);
		end = top;
	    }

	    while (count++ < n_lines) {
		tmp = top;
		XawTextSinkFindPosition(ctx->text.sink, top,
					ctx->text.left_margin,
					wwidth,ctx->text.wrap == XawtextWrapWord,
					&top, &dim, &dim);
		if (tmp == top)
		    ++top;
	    }
	}
	else
	    top = SrcScan(ctx->text.source, lt->top, XawstEOL,
			  XawsdLeft, -vlines + 1, False);
	if (-vlines >= ctx->text.lt.lines)
	  scroll = False;
     }
    else if (vlines > 0) {	  /* VScroll Down */
	if (LineForPosition(ctx, ctx->text.lastPos) == 0)
	    vlines = 0;
	if (vlines < lt->lines)
	    top = XawMin(lt->info[vlines].position, ctx->text.lastPos);
	else if (ctx->text.wrap == XawtextWrapNever)
	    top = SrcScan(ctx->text.source,
			  SrcScan(ctx->text.source, lt->top,
				  XawstEOL, XawsdRight, vlines,
				  True),
			  XawstEOL, XawsdLeft, 1, False);
	else {
	    top = lt->top;
	    count = 0;
	    while (count++ < vlines) {
		tmp = top;
		XawTextSinkFindPosition(ctx->text.sink, top,
					ctx->text.left_margin,
					wwidth, ctx->text.wrap == XawtextWrapWord,
					&top, &dim, &dim);
		if (tmp == top)
		    ++top;
	    }
	}
	if (vlines >= ctx->text.lt.lines
	    || lt->info[vlines].position >= ctx->text.lastPos)
	    scroll = False;
    }

    if (!vlines) {
	if (hpixels) {
	    ClearWindow(ctx);
	    ctx->text.clear_to_eol = True;
	}
	_XawTextSetScrollBars(ctx);
	return;
    }

    /* Flushes any pending updates. Normally, there may be a call to
     * XawTextUnsetSelection not yet updated.
     */
    if (!hpixels && scroll) {
	ctx->text.clear_to_eol = True;
	FlushUpdate(ctx);
    }

    /*
     * Rebuild the line table, doing the vertical scroll
     */
    (void)_BuildLineTable(ctx, top, 0);
    lt = &ctx->text.lt;
    if (scroll) {
	for (count = 0; count < lt->lines - 1; count++)
	    if (lt->info[count + 1].y - lt->info[count].y != dim) {
		scroll = False;
		break;
	    }
    }

    XtSetArg(arglist[0], XtNinsertPosition, lt->top + lt->lines);
    _XawImSetValues((Widget)ctx, arglist, 1);

    if (hpixels || !scroll || lines != lt->lines)
	return;

    /* _BuildLineTable updates everything if the top position changes.
     * It is not required here.
     */
    (void)XmuScanlineXor(ctx->text.update, ctx->text.update);
    if (vlines < 0 && IsPositionVisible(ctx, 0))
	vlines = -LineForPosition(ctx, update_from);

    y0 = ctx->text.r_margin.top;
    if (vlines < 0) {
	update_from = lt->top;
	update_to = lt->info[-vlines + 1].position - 1;
	y1 = lt->info[lt->lines + vlines].y;
	y2 = lt->info[-vlines].y;
	DoCopyArea(ctx, ctx->text.r_margin.left, y0, vwidth,
		   y1 - y0,
		   ctx->text.r_margin.left, y2);
    }
    else {
	update_from = lt->info[lt->lines - vlines].position;
	update_to = lt->info[lt->lines].position;
	y1 = lt->info[lt->lines - vlines].y;
	y2 = lt->info[vlines].y;
	DoCopyArea(ctx, ctx->text.r_margin.left, y2,
		   vwidth, lt->info[lt->lines].y - y2,
		   ctx->text.r_margin.left, y0);
    }
    _XawTextNeedsUpdating(ctx, update_from, update_to);
    ctx->text.clear_to_eol = True;
}

/*
 * The routine will scroll the displayed text by lines.  If the arg  is
 * positive, move up; otherwise, move down. [note: this is really a private
 * procedure but is used in multiple modules].
 */
void
_XawTextVScroll(TextWidget ctx, int n)
{
  XawTextScroll(ctx, n, 0);
}

/*ARGSUSED*/
static void
HScroll(Widget w, XtPointer closure, XtPointer callData)
{
    TextWidget ctx = (TextWidget)closure;
    long pixels = (long)callData;

    if (pixels > 0) {
	long max;

	max = (int)GetWidestLine(ctx) + ctx->text.left_margin -
	      ctx->text.r_margin.left;
	max = XawMax(0, max);
	pixels = XawMin(pixels, max);
    }

    if (pixels) {
	_XawTextPrepareToUpdate(ctx);
	XawTextScroll(ctx, 0, pixels);
	_XawTextExecuteUpdate(ctx);
    }
}

/*ARGSUSED*/
static void
HJump(Widget w, XtPointer closure, XtPointer callData)
{
    TextWidget ctx = (TextWidget)closure;
    float percent = *(float *)callData;
    long pixels;

    pixels = ctx->text.left_margin -
	     (ctx->text.r_margin.left - (int)(percent * GetWidestLine(ctx)));

    HScroll(w, (XtPointer)ctx, (XtPointer)pixels);
}

/*
 * Function:
 *	UpdateTextInLine
 *
 * Parameters:
 *	ctx  - text widget
 *	line - line to update
 *	x1   - left pixel
 *	x2   - right pixel
 *
 * Description:
 *	Updates the text in the given line and pixel interval
 */
static void
UpdateTextInLine(TextWidget ctx, int line, int x1, int x2)
{
    XawTextLineTableEntry *lt = ctx->text.lt.info + line;
    XawTextPosition left, right;
    int from_x, width, height;

    if (lt->position >= ctx->text.lastPos
	|| ctx->text.left_margin > x2
	|| (int)lt->textWidth + ctx->text.left_margin < x1) {
	/* Mark line to be cleared */
	if (ctx->text.clear_to_eol)
	    _XawTextNeedsUpdating(ctx, lt->position, lt->position + 1);
	return;
    }

    from_x = ctx->text.left_margin;
    XawTextSinkFindPosition(ctx->text.sink, lt->position,
			    from_x, x1 - from_x,
			    False, &left, &width, &height);
    if (line == ctx->text.lt.lines)
	right = -1;
    else if (x2 >= lt->textWidth - from_x)
	right = lt[1].position - 1;
    else {
	from_x += width;
	XawTextSinkFindPosition(ctx->text.sink, left,
				from_x, x2 - from_x,
				False, &right, &width, &height);
    }

    if ((right < 0) || (right + 1 <= lt[1].position))
	++right;

    /* Mark text interval to be repainted */
    _XawTextNeedsUpdating(ctx, left, right);
}

/*
 * The routine will scroll the displayed text by pixels.  If the calldata is
 * positive, move up; otherwise, move down.
 */
/*ARGSUSED*/
static void
VScroll(Widget w, XtPointer closure, XtPointer callData)
{
    TextWidget ctx = (TextWidget)closure;
    long height, lines = (long)callData;

    height = XtHeight(ctx) - VMargins(ctx);
    if (height < 1)
	height = 1;
    lines = (lines * ctx->text.lt.lines) / height;
    _XawTextPrepareToUpdate(ctx);
    XawTextScroll(ctx, lines, 0);
    _XawTextExecuteUpdate(ctx);
}

/*ARGSUSED*/
static void
VJump(Widget w, XtPointer closure, XtPointer callData)
{
    float percent = *(float *)callData;
    TextWidget ctx = (TextWidget)closure;
    XawTextPosition top, last, position, tmp;
    XawTextLineTable *lt = &(ctx->text.lt);
    int dim, vlines = 0, wwidth = GetMaxTextWidth(ctx);
    Bool scroll = True;

    position = percent * ctx->text.lastPos;
    top = lt->top;

    if (!lt->lines || (position >= lt->top && position < lt->info[1].position)) {
	_XawTextSetScrollBars(ctx);
	return;
    }

#ifndef OLDXAW
    ctx->text.lt.base_line = -1;
#endif

    if (position > lt->top) {	  /* VScroll Up */
	if (position > lt->top && position < lt->info[lt->lines].position)
	    vlines = LineForPosition(ctx, position);
	else {
	    scroll = False;
	    top = SrcScan(ctx->text.source, position, XawstEOL,
			  XawsdLeft, 1, False);
	    if (ctx->text.wrap != XawtextWrapNever) {
		last = top;
		while (last < position) {
		    tmp = last;
		    XawTextSinkFindPosition(ctx->text.sink, last,
					    ctx->text.left_margin, wwidth,
					    ctx->text.wrap == XawtextWrapWord,
					    &last, &dim, &dim);
		    if (last == tmp)
			++last;
		    if (last < position)
			top = last;
		}
	    }
	}
    }
    else {		  /* VScroll Down */
	/*
	 * Calculates the number of lines
	 */
	while (top > position) {
	    last = top;
	    top = SrcScan(ctx->text.source, top, XawstEOL,
			  XawsdLeft, 2, False);
	    vlines -= CountLines(ctx, top, last);
	    if (-vlines >= ctx->text.lt.lines) {
		scroll = False;
		top = SrcScan(ctx->text.source, position, XawstEOL,
			      XawsdLeft, 1, False);
		break;
	      }
	  }
	/*
	 * Normalize
	 */
	if (ctx->text.wrap != XawtextWrapNever) {
	    last = top;
	    while (last < position) {
		tmp = last;
		XawTextSinkFindPosition(ctx->text.sink, last,
					ctx->text.left_margin,
					wwidth,
					ctx->text.wrap == XawtextWrapWord,
					&last, &dim, &dim);
		if (last == tmp)
		    ++last;
		if (last < position)
		    top = last;
		++vlines;
	    }
	}
    }

    if (vlines || !scroll) {
	_XawTextPrepareToUpdate(ctx);
	if (scroll)
	    XawTextScroll(ctx, vlines, 0);
	else
	    _BuildLineTable(ctx, top, 0);
	_XawTextExecuteUpdate(ctx);
    }
}

static Bool
MatchSelection(Atom selection, XawTextSelection *s)
{
    Atom *match;
    int count;

    for (count = 0, match = s->selections; count < s->atom_count;
       match++, count++)
    if (*match == selection)
	return (True);

  return (False);
}

static Boolean
TextConvertSelection(Widget w, Atom *selection, Atom *target, Atom *type,
		 XtPointer *value, unsigned long *length, int *format)
{
    Display *d = XtDisplay(w);
    TextWidget ctx = (TextWidget)w;
    Widget src = ctx->text.source;
    XawTextEditType edit_mode;
    Arg args[1];
    XawTextSelectionSalt *salt = NULL;
    XawTextSelection *s;

    if (*target == XA_TARGETS(d)) {
	Atom *targetP, *std_targets;
	unsigned long std_length;

	if (SrcCvtSel(src, selection, target, type, value, length, format))
	    return (True);

	XtSetArg(args[0], XtNeditType, &edit_mode);
	XtGetValues(src, args, ONE);

	XmuConvertStandardSelection(w, ctx->text.time, selection,
				    target, type, (XPointer*)&std_targets,
				    &std_length, format);

	*length = 7 + (edit_mode == XawtextEdit) + std_length;
	*value = XtMalloc((unsigned)sizeof(Atom)*(*length));
	targetP = *(Atom**)value;
	*targetP++ = XA_STRING;
	*targetP++ = XA_TEXT(d);
	*targetP++ = XA_UTF8_STRING(d);
	*targetP++ = XA_COMPOUND_TEXT(d);
	*targetP++ = XA_LENGTH(d);
	*targetP++ = XA_LIST_LENGTH(d);
	*targetP++ = XA_CHARACTER_POSITION(d);
	if (edit_mode == XawtextEdit) {
	    *targetP++ = XA_DELETE(d);
	}
	(void)memmove((char*)targetP, (char*)std_targets,
		      sizeof(Atom) * std_length);
	XtFree((char*)std_targets);
	*type = XA_ATOM;
	*format = 32;
	return (True);
    }

    if (SrcCvtSel(src, selection, target, type, value, length, format))
	return (True);

    if (MatchSelection(*selection, &ctx->text.s))
	s = &ctx->text.s;
    else {
	for (salt = ctx->text.salt; salt; salt = salt->next)
	    if (MatchSelection(*selection, &salt->s))
		break;
	if (!salt)
	    return (False);
	s = &salt->s;
    }
    if (*target == XA_STRING
	|| *target == XA_TEXT(d)
	|| *target == XA_UTF8_STRING(d)
	|| *target == XA_COMPOUND_TEXT(d)) {
	if (*target == XA_TEXT(d)) {
	    if (XawTextFormat(ctx, XawFmtWide))
		*type = XA_COMPOUND_TEXT(d);
	    else
		*type = XA_STRING;
	}
	else
	    *type = *target;
	/* 
	 * If salt is True, the salt->contents stores CT string,
	 * its length is measured in bytes.
	 * Refer to _XawTextSaltAwaySelection().
	 *
	 * by Li Yuhong, Mar. 20, 1991.
	 */
	if (!salt) {
	    *value = _XawTextGetSTRING(ctx, s->left, s->right);
	    if (XawTextFormat(ctx, XawFmtWide)) {
		XTextProperty textprop;
		if (XwcTextListToTextProperty(d, (wchar_t **)value, 1,
					      XCompoundTextStyle, &textprop)
		    <  Success) {
		    XtFree((char *)*value);
		    return (False);
		}
	 	XtFree((char *)*value);
		*value = (XtPointer)textprop.value;
		*length = textprop.nitems;
	    }
	    else
	        *length = strlen((char *)*value);
	}
	else {
	    *value = XtMalloc((salt->length + 1) * sizeof(unsigned char));
	    strcpy ((char *)*value, salt->contents);
	    *length = salt->length;
	}
	/* Got *value,*length, now in COMPOUND_TEXT format. */
	if (XawTextFormat(ctx, XawFmtWide) && *type == XA_STRING) {
	    XTextProperty textprop;
	    wchar_t **wlist;
	    int count;

	    textprop.encoding = XA_COMPOUND_TEXT(d);
	    textprop.value = (unsigned char *)*value;
	    textprop.nitems = strlen(*value);
	    textprop.format = 8;
	    if (XwcTextPropertyToTextList(d, &textprop, &wlist, &count)
		 < Success
		|| count < 1) {
		XtFree((char *)*value);
		return (False);
	    }
	    XtFree((char *)*value);
	    if (XwcTextListToTextProperty(d, wlist, 1, XStringStyle, &textprop)
		 < Success) {
		XwcFreeStringList((wchar_t**) wlist);
		return (False);
	    }
	    *value = (XtPointer)textprop.value;
	    *length = textprop.nitems;
	    XwcFreeStringList(wlist);
	} else if (*type == XA_UTF8_STRING(d)) {
	    XTextProperty textprop;
	    char **list;
	    int count;

	    textprop.encoding = XA_COMPOUND_TEXT(d);
	    textprop.value = (unsigned char *)*value;
	    textprop.nitems = strlen(*value);
	    textprop.format = 8;
	    if (Xutf8TextPropertyToTextList(d, &textprop, &list, &count)
		 < Success
		|| count < 1) {
		XtFree((char *)*value);
		return (False);
	    }
	    XtFree((char *)*value);
	    *value = *list;
	    *length = strlen(*list);
	    XFree(list);
	}
	*format = 8;
	return (True);
    }

    if ((*target == XA_LIST_LENGTH(d)) || (*target == XA_LENGTH(d))) {
	long * temp;

	temp = (long *)XtMalloc((unsigned)sizeof(long));
	if (*target == XA_LIST_LENGTH(d))
	    *temp = 1L;
	else			/* *target == XA_LENGTH(d) */
	    *temp = (long) (s->right - s->left);

	*value = (XPointer)temp;
	*type = XA_INTEGER;
	*length = 1L;
	*format = 32;
	return (True);
    }

    if (*target == XA_CHARACTER_POSITION(d)) {
	long * temp;

	temp = (long *)XtMalloc((unsigned)(2 * sizeof(long)));
	temp[0] = (long)(s->left + 1);
	temp[1] = s->right;
	*value = (XPointer)temp;
	*type = XA_SPAN(d);
	*length = 2L;
	*format = 32;
	return (True);
    }

    if (*target == XA_DELETE(d)) {
	if (!salt)
	    _XawTextZapSelection(ctx, NULL, True);
	*value = NULL;
	*type = XA_NULL(d);
	*length = 0;
	*format = 32;
	return (True);
    }

    if (XmuConvertStandardSelection(w, ctx->text.time, selection, target, type,
				    (XPointer *)value, length, format))
	return (True);

    /* else */
    return (False);
}

/*
 * Function:
 *	GetCutBuffferNumber
 *
 * Parameters:
 *	atom - atom to check
 *
 * Description:
 *	Returns the number of the cut buffer.
 *
 * Returns:
 *	The number of the cut buffer representing this atom or NOT_A_CUT_BUFFER
 */
#define NOT_A_CUT_BUFFER -1
static int
GetCutBufferNumber(Atom atom)
{
    if (atom == XA_CUT_BUFFER0) return (0);
    if (atom == XA_CUT_BUFFER1) return (1);
    if (atom == XA_CUT_BUFFER2) return (2);
    if (atom == XA_CUT_BUFFER3) return (3);
    if (atom == XA_CUT_BUFFER4) return (4);
    if (atom == XA_CUT_BUFFER5) return (5);
    if (atom == XA_CUT_BUFFER6) return (6);
    if (atom == XA_CUT_BUFFER7) return (7);
    return (NOT_A_CUT_BUFFER);
}

static void
TextLoseSelection(Widget w, Atom *selection)
{
    TextWidget ctx = (TextWidget)w;
    Atom *atomP;
    int i;
    XawTextSelectionSalt*salt, *prevSalt, *nextSalt;

    atomP = ctx->text.s.selections;
    for (i = 0 ; i < ctx->text.s.atom_count; i++, atomP++)
	if ((*selection == *atomP)
	    || (GetCutBufferNumber(*atomP) != NOT_A_CUT_BUFFER))
	    *atomP = (Atom)0;

    while (ctx->text.s.atom_count
	   && ctx->text.s.selections[ctx->text.s.atom_count - 1] == 0)
	ctx->text.s.atom_count--;

    /*
     * Must walk the selection list in opposite order from UnsetSelection
     */
    atomP = ctx->text.s.selections;
    for (i = 0 ; i < ctx->text.s.atom_count; i++, atomP++)
	if (*atomP == (Atom)0) {
	    *atomP = ctx->text.s.selections[--ctx->text.s.atom_count];
	    while (ctx->text.s.atom_count
		   && ctx->text.s.selections[ctx->text.s.atom_count-1] == 0)
		ctx->text.s.atom_count--;
	}

    if (ctx->text.s.atom_count == 0)
	ModifySelection(ctx, ctx->text.insertPos, ctx->text.insertPos);

    prevSalt = 0;
    for (salt = ctx->text.salt; salt; salt = nextSalt) {
	atomP = salt->s.selections;
	nextSalt = salt->next;
	for (i = 0 ; i < salt->s.atom_count; i++, atomP++)
	    if (*selection == *atomP)
		*atomP = (Atom)0;

	while (salt->s.atom_count
	       && salt->s.selections[salt->s.atom_count-1] == 0)
	    salt->s.atom_count--;
    	
	/*
	 * Must walk the selection list in opposite order from UnsetSelection
	 */
	atomP = salt->s.selections;
	for (i = 0 ; i < salt->s.atom_count; i++, atomP++)
	    if (*atomP == (Atom)0) {
		*atomP = salt->s.selections[--salt->s.atom_count];
		while (salt->s.atom_count
		       && salt->s.selections[salt->s.atom_count-1] == 0)
		    salt->s.atom_count--;
	    }

	if (salt->s.atom_count == 0) {
	    XtFree ((char *) salt->s.selections);
	    XtFree (salt->contents);
	    if (prevSalt)
		prevSalt->next = nextSalt;
	    else
		ctx->text.salt = nextSalt;
	    XtFree((char *)salt);
	}
	else
	    prevSalt = salt;
    }
}

void
_XawTextSaltAwaySelection(TextWidget ctx, Atom *selections, int num_atoms)
{
    XawTextSelectionSalt *salt;
    int i, j;

    for (i = 0; i < num_atoms; i++)
	TextLoseSelection((Widget)ctx, selections + i);
    if (num_atoms == 0)
	return;
    salt = (XawTextSelectionSalt *)
	XtMalloc((unsigned)sizeof(XawTextSelectionSalt));
    if (!salt)
	return;
    salt->s.selections = (Atom *)XtMalloc((unsigned)(num_atoms * sizeof(Atom)));
    if (!salt->s.selections) {
	XtFree((char *)salt);
	return;
    }
    salt->s.left = ctx->text.s.left;
    salt->s.right = ctx->text.s.right;
    salt->s.type = ctx->text.s.type;
    salt->contents = _XawTextGetSTRING(ctx, ctx->text.s.left, ctx->text.s.right);
    if (XawTextFormat(ctx, XawFmtWide)) {
	XTextProperty textprop;
	if (XwcTextListToTextProperty(XtDisplay((Widget)ctx),
				      (wchar_t**)(&(salt->contents)), 1,
				      XCompoundTextStyle,
				      &textprop) < Success) {
	    XtFree(salt->contents);
	    salt->length = 0;
	    return;
	}
	XtFree(salt->contents);
	salt->contents = (char *)textprop.value;
	salt->length = textprop.nitems;
    }
    else
	salt->length = strlen (salt->contents);
    salt->next = ctx->text.salt;
    ctx->text.salt = salt;
    j = 0;
    for (i = 0; i < num_atoms; i++) {
	if (GetCutBufferNumber (selections[i]) == NOT_A_CUT_BUFFER) {
	    salt->s.selections[j++] = selections[i];
	    XtOwnSelection((Widget)ctx, selections[i], ctx->text.time,
			   TextConvertSelection, TextLoseSelection, NULL);
	}
    }
    salt->s.atom_count = j;
}

static void
_SetSelection(TextWidget ctx, XawTextPosition left, XawTextPosition right,
	      Atom *selections, Cardinal count)
{
#ifndef OLDXAW
    Cardinal i;
    XawTextPosition pos;
    TextSrcObject src = (TextSrcObject)ctx->text.source;

    for (i = 0; i < src->textSrc.num_text; i++) {
	TextWidget tw = (TextWidget)src->textSrc.text[i];
	Bool needs_updating = tw->text.old_insert < 0;
	Bool showposition = tw->text.showposition;

	if (needs_updating) {
	    tw->text.showposition = False;
	    _XawTextPrepareToUpdate(tw);
	}
#else
	TextWidget tw = ctx;
	XawTextPosition pos;
#endif /* OLDXAW */

	if (left < tw->text.s.left) {
	    pos = Min(right, tw->text.s.left);
	    _XawTextNeedsUpdating(tw, left, pos);
	}
	if (left > tw->text.s.left) {
	    pos = Min(left, tw->text.s.right);
	    _XawTextNeedsUpdating(tw, tw->text.s.left, pos);
	}
	if (right < tw->text.s.right) {
	    pos = Max(right, tw->text.s.left);
	    _XawTextNeedsUpdating(tw, pos, tw->text.s.right);
	}
	if (right > tw->text.s.right) {
	    pos = Max(left, tw->text.s.right);
	    _XawTextNeedsUpdating(tw, pos, right);
	}

	tw->text.s.left = left;
	tw->text.s.right = right;

#ifndef OLDXAW
	if (needs_updating) {
	    _XawTextExecuteUpdate(tw);
	    tw->text.showposition = showposition;
	}
    }
#endif /* OLDXAW */

    SrcSetSelection(ctx->text.source, left, right,
		    (count == 0) ? None : selections[0]);

    if (left < right) {
	Widget w = (Widget)ctx;
	int buffer;

	while (count) {
	    Atom selection = selections[--count];

	    /*
	     * If this is a cut buffer
	     */
	    if ((buffer = GetCutBufferNumber(selection)) != NOT_A_CUT_BUFFER) {
		unsigned char *ptr, *tptr;
		unsigned int amount, max_len = MAX_CUT_LEN(XtDisplay(w));
		unsigned long len;

		tptr= ptr= (unsigned char *)_XawTextGetSTRING(ctx,
							      ctx->text.s.left,
							      ctx->text.s.right);
		if (XawTextFormat(ctx, XawFmtWide)) {
		    /*
		     * Only XA_STRING(Latin 1) is allowed in CUT_BUFFER,
		     * so we get it from wchar string, then free the wchar string
		     */
		    XTextProperty textprop;

		    if (XwcTextListToTextProperty(XtDisplay(w), (wchar_t**)&ptr,
						  1, XStringStyle, &textprop)
			<  Success){
			XtFree((char *)ptr);
			return;
		    }
		    XtFree((char *)ptr);
		    tptr = ptr = textprop.value;
		}
		if (buffer == 0) {
		    _CreateCutBuffers(XtDisplay(w));
		    XRotateBuffers(XtDisplay(w), 1);
		}
		amount = Min ((len = strlen((char *)ptr)), max_len);
		XChangeProperty(XtDisplay(w), RootWindow(XtDisplay(w), 0),
				selection, XA_STRING, 8, PropModeReplace,
				ptr, amount);

		while (len > max_len) {
		    len -= max_len;
		    tptr += max_len;
		    amount = Min (len, max_len);
		    XChangeProperty(XtDisplay(w), RootWindow(XtDisplay(w), 0),
				    selection, XA_STRING, 8, PropModeAppend,
				    tptr, amount);
		}
		XtFree ((char *)ptr);
	    }
	    else		/* This is a real selection */
		XtOwnSelection(w, selection, ctx->text.time, TextConvertSelection,
			       TextLoseSelection, NULL);
	}
    }
    else
	XawTextUnsetSelection((Widget)ctx);
}

#ifndef OLDXAW
void
_XawTextSetLineAndColumnNumber(TextWidget ctx, Bool force)
{
    int line_number, column_number;

    if (ctx->text.old_insert != ctx->text.insertPos &&
	ctx->text.lt.base_line < 0) {
	ctx->text.lt.base_line = 0;
	(void)_BuildLineTable(ctx, ctx->text.lt.top, 0);
    }

    line_number = ResolveLineNumber(ctx);
    column_number = ResolveColumnNumber(ctx);

    if (force || (ctx->text.column_number != column_number
		  || ctx->text.line_number != line_number)) {
	XawTextPositionInfo info;

	ctx->text.line_number = info.line_number = line_number;
	ctx->text.column_number = info.column_number = column_number;
	info.insert_position = ctx->text.insertPos;
	info.last_position = ctx->text.lastPos;
	info.overwrite_mode = ctx->text.overwrite;

	XtCallCallbacks((Widget)ctx, XtNpositionCallback, (XtPointer)&info);
    }
}

static int
ResolveColumnNumber(TextWidget ctx)
{
    Widget src = ctx->text.source;
    short column_number = 0;
    XawTextPosition position;
    XawTextBlock block;
    unsigned long format = _XawTextFormat(ctx);
    TextSinkObject sink = (TextSinkObject)ctx->text.sink;
    short *char_tabs = sink->text_sink.char_tabs;
    int tab_count = sink->text_sink.tab_count;
    int tab_index = 0, tab_column = 0, tab_base = 0;

    if (ctx->text.lt.base_line < 1)
	return (ctx->text.column_number);

    position = SrcScan(src, ctx->text.insertPos, XawstEOL, XawsdLeft, 1, False);
    XawTextSourceRead(src, position, &block, ctx->text.insertPos - position);

    for (; position < ctx->text.insertPos; position++) {
	if (position - block.firstPos >= block.length)
	    XawTextSourceRead(src, position, &block, ctx->text.insertPos - position);
	if ((format == XawFmt8Bit && block.ptr[position - block.firstPos] == '\t') ||
	    (format == XawFmtWide && ((wchar_t*)block.ptr)[position - block.firstPos] == _Xaw_atowc(XawTAB))) {
	    while (tab_base + tab_column <= column_number) {
		if (tab_count) {
		    for (; tab_index < tab_count; ++tab_index)
			if (tab_base + char_tabs[tab_index] > column_number) {
			    tab_column = char_tabs[tab_index];
			    break;
			}
		    if (tab_index >= tab_count) {
			tab_base += char_tabs[tab_count - 1];
			tab_column = tab_index = 0;
		    }
		}
		else
		    tab_column += DEFAULT_TAB_SIZE;
	    }
	    column_number = tab_base + tab_column;
	}
	else
	    ++column_number;
	if (column_number >= 16384) {
	    column_number = 16383;
	    break;
	}
    }

    return (column_number);
}
#endif /* OLDXAW */

void
_XawTextSourceChanged(Widget w, XawTextPosition left, XawTextPosition right,
		      XawTextBlock *block, int lines)
{
    TextWidget ctx = (TextWidget)w;
    Widget src = ctx->text.source;
    XawTextPosition update_from, update_to, top;
    Boolean update_disabled;
    int delta, line, line_from;

    if (left < ctx->text.old_insert) {
	XawTextPosition old_insert = ctx->text.old_insert;

	if (right < ctx->text.old_insert)
	    old_insert -= right - left;
	else
	    old_insert = left;

	ctx->text.insertPos = old_insert + block->length;
    }
#ifndef OLDXAW
    if (left <= ctx->text.lt.top) {
	if (left + block->length - (right - left) < ctx->text.lt.top) {
	    ctx->text.source_changed = SRC_CHANGE_BEFORE;
	    ctx->text.lt.base_line += lines;
	}
	else
	    ctx->text.source_changed = SRC_CHANGE_OVERLAP;
    }
    else
	ctx->text.source_changed = SRC_CHANGE_AFTER;
#endif

    update_from = left;
    update_to = left + block->length;
    update_to = SrcScan(src, update_to, XawstEOL, XawsdRight, 1, False);
    delta = block->length - (right - left);
    if (delta < 0)
	ctx->text.clear_to_eol = True;
    if (update_to == update_from)
	++update_to;
    update_disabled = ctx->text.update_disabled;
    ctx->text.update_disabled = True;
    ctx->text.lastPos = XawTextGetLastPosition(ctx);
    top = ctx->text.lt.info[0].position;

    XawTextUnsetSelection((Widget)ctx);

    if (delta) {
	int i;
	XmuSegment *seg;

	for (seg = ctx->text.update->segment; seg; seg = seg->next) {
	    if (seg->x1 > (int)left)
		break;
	    else if (seg->x2 > (int)left) {
		seg->x2 += delta;
		seg = seg->next;
		break;
	    }
	}
	for (; seg; seg = seg->next) {
	    seg->x1 += delta;
	    seg->x2 += delta;
	}
	XmuOptimizeScanline(ctx->text.update);

	for (i = 0; i <= ctx->text.lt.lines; i++)
	    if (ctx->text.lt.info[i].position > left)
		break;
	for (; i <= ctx->text.lt.lines; i++)
	    ctx->text.lt.info[i].position += delta;
    }

    if (top != ctx->text.lt.info[0].position) {
	line_from = line = 0;
	ctx->text.lt.top = top = SrcScan(src, ctx->text.lt.info[0].position,
					 XawstEOL, XawsdLeft, 1, False);
	update_from = top;
    }
    else {
	line_from = line = LineForPosition(ctx, update_from + delta);
	top = ctx->text.lt.info[line].position;
    }

    if (line > 0 && ctx->text.wrap == XawtextWrapWord) {
	--line;
	top = ctx->text.lt.info[line].position;
    }

    (void)_BuildLineTable(ctx, top, line);

    if (ctx->text.wrap == XawtextWrapWord) {
	if (line_from != LineForPosition(ctx, update_from)
	    || line_from != LineForPosition(ctx, update_to)) {
	    ctx->text.clear_to_eol = True;
	    update_from = SrcScan(src, update_from,
				  XawstWhiteSpace, XawsdLeft, 1, True);
	    if (update_to >= ctx->text.lastPos)
	    /* this is not an error, it just tells _BuildLineTable to
	     * clear to the bottom of the window. The value of update_to
	     * should not be > ctx->text.lastPos.
	     */
		++update_to;
	}
    }
    else if (!ctx->text.clear_to_eol) {
	if (LineForPosition(ctx, update_from)
	    != LineForPosition(ctx, update_to))
	    ctx->text.clear_to_eol = True;
    }

    _XawTextNeedsUpdating(ctx, update_from, update_to);
    ctx->text.update_disabled = update_disabled;
}

/*
 * Function:
 *	_XawTextReplace
 *
 * Parameters:
 *	ctx   - text widget
 *	left  - left offset
 *	right - right offset
 *	block - text block
 *
 * Description:
 *	  Replaces the text between left and right by the text in block.
 *	  Does all the required calculations of offsets, and rebuild the
 *	the line table, from the insertion point (or previous line, if
 *	wrap mode is 'word').
 *
 * Returns:
 *	XawEditDone     - success
 *	any other value - error code
 */
int
_XawTextReplace(TextWidget ctx, XawTextPosition left, XawTextPosition right,
		XawTextBlock *block)
{
    Arg args[1];
    Widget src;
    XawTextEditType edit_mode;

    if (left == right && block->length == 0)
	return (XawEditDone);

    src = ctx->text.source;
    XtSetArg(args[0], XtNeditType, &edit_mode);
    XtGetValues(src, args, 1);

    if (edit_mode == XawtextAppend) {
	if (block->length == 0)
	    return (XawEditError);
	ctx->text.insertPos = ctx->text.lastPos;
    }

#ifndef OLDXAW
    return (SrcReplace(src, left, right, block));
#else
    if (SrcReplace(src, left, right, block) == XawEditDone) {
	_XawTextSourceChanged((Widget)ctx, left, right, block, 0);

	return (XawEditDone);
    }

    return (XawEditError);
#endif
}

/*
 * This routine will display text between two arbitrary source positions.
 * In the event that this span contains highlighted text for the selection,
 * only that portion will be displayed highlighted.
 */
static void
OldDisplayText(Widget w, XawTextPosition left, XawTextPosition right)
{
    static XmuSegment segment;
    static XmuScanline next;
    static XmuScanline scanline = {0, &segment, &next};
    static XmuArea area = {&scanline};

    TextWidget ctx = (TextWidget)w;
    int x, y, line;
    XawTextPosition start, end, last, final;
    XmuScanline *scan;
    XmuSegment *seg;
    XmuArea *clip = NULL;
    Bool cleol = ctx->text.clear_to_eol;
    Bool has_selection = ctx->text.s.right > ctx->text.s.left;

    left = left < ctx->text.lt.top ? ctx->text.lt.top : left;

    if (left > right || !LineAndXYForPosition(ctx, left, &line, &x, &y))
	return;

    last = XawTextGetLastPosition(ctx);
    segment.x2 = (int)XtWidth(ctx) - ctx->text.r_margin.right;

    if (cleol)
	clip = XmuCreateArea();

    for (start = left; start < right && line < ctx->text.lt.lines; line++) {
	if ((end = ctx->text.lt.info[line + 1].position) > right)
	    end = right;

	final = end;
	if (end > last)
	    end = last;

	if (end > start) {
	    if (!has_selection
		|| (start >= ctx->text.s.right || end <= ctx->text.s.left))
		_XawTextSinkDisplayText(ctx->text.sink, x, y, start, end, False);
	    else if (start >= ctx->text.s.left && end <= ctx->text.s.right)
		_XawTextSinkDisplayText(ctx->text.sink, x, y, start, end, True);
	    else {
		OldDisplayText(w, start, ctx->text.s.left);
		OldDisplayText(w, Max(start, ctx->text.s.left),
			       Min(end, ctx->text.s.right));
		OldDisplayText(w, ctx->text.s.right, end);
	    }
	}

	x = ctx->text.left_margin;
	if (cleol) {
	    segment.x1 = ctx->text.lt.info[line].textWidth + x;
	    if (XmuValidSegment(&segment)) {
		scanline.y = y;
		next.y = ctx->text.lt.info[line + 1].y;
		XmuAreaOr(clip, &area);
	    }
	}

	start = final;
	y = ctx->text.lt.info[line + 1].y;
    }

    if (cleol)  {
	for (scan = clip->scanline; scan && scan->next; scan = scan->next)
	    for (seg = scan->segment; seg; seg = seg->next)
		SinkClearToBG(ctx->text.sink,
			      seg->x1, scan->y,
			      seg->x2 - seg->x1, scan->next->y - scan->y);
	XmuDestroyArea(clip);
    }
}

#ifndef OLDXAW
/*ARGSUSED*/
static void
DisplayText(Widget w, XawTextPosition left, XawTextPosition right)
{
    static XmuSegment segment;
    static XmuScanline next;
    static XmuScanline scanline = {0, &segment, &next};
    static XmuArea area = {&scanline};

    TextWidget ctx = (TextWidget)w;
    int y, line;
    XawTextPosition from, to, lastPos;
    Bool cleol = ctx->text.clear_to_eol;
    Bool has_selection = ctx->text.s.right > ctx->text.s.left;
    XawTextPaintList *paint_list;

    left = left < ctx->text.lt.top ? ctx->text.lt.top : left;

    if (left > right || !IsPositionVisible(ctx, left))
	return;

    line = LineForPosition(ctx, left);
    y = ctx->text.lt.info[line].y;
    segment.x2 = (int)XtWidth(ctx) - ctx->text.r_margin.right;
    lastPos = XawTextGetLastPosition(ctx);

    paint_list = ((TextSinkObject)ctx->text.sink)->text_sink.paint;

    for (from = left; from < right && line < ctx->text.lt.lines; line++) {
	if ((to = ctx->text.lt.info[line + 1].position) > right)
	    to = right;

	if (to > lastPos)
	    to = lastPos;

	if (from < to) {
	    if (!has_selection
		|| (from >= ctx->text.s.right || to <= ctx->text.s.left))
		XawTextSinkPreparePaint(ctx->text.sink, y, line, from, to, False);
	    else if (from >= ctx->text.s.left && to <= ctx->text.s.right)
		XawTextSinkPreparePaint(ctx->text.sink, y, line, from, to, True);
	    else {
		XawTextSinkPreparePaint(ctx->text.sink, y, line, from,
					ctx->text.s.left, False);
		XawTextSinkPreparePaint(ctx->text.sink, y, line,
					XawMax(from, ctx->text.s.left),
					XawMin(to, ctx->text.s.right), True);
		XawTextSinkPreparePaint(ctx->text.sink, y, line,
					ctx->text.s.right, to, False);
	    }
	}

	if (cleol) {
	    segment.x1 = ctx->text.lt.info[line].textWidth + ctx->text.left_margin;
	    if (XmuValidSegment(&segment)) {
		scanline.y = y;
		next.y = ctx->text.lt.info[line + 1].y;
		XmuAreaOr(paint_list->clip, &area);
	    }
	}
	y = ctx->text.lt.info[line + 1].y;
	from = to;
    }

    /* clear to the bottom of the window */
    if (cleol && line >= ctx->text.lt.lines) {
	segment.x1 = ctx->text.left_margin;
	if (XmuValidSegment(&segment)) {
	    scanline.y = y;
	    next.y = (int)XtHeight(ctx) - (int)ctx->text.margin.bottom;
	    XmuAreaOr(paint_list->clip, &area);
	}
    }
}
#endif

/*
 * This routine implements multi-click selection in a hardwired manner.
 * It supports multi-click entity cycling (char, word, line, file) and mouse
 * motion adjustment of the selected entitie (i.e. select a word then, with
 * button still down, adjust wich word you really meant by moving the mouse).
 * [NOTE: This routine is to be replaced by a set of procedures that
 * will allows clients to implements a wide class of draw through and
 * multi-click selection user interfaces.]
 */
static void
DoSelection(TextWidget ctx, XawTextPosition pos, Time time, Bool motion)
{
    XawTextPosition newLeft, newRight;
    XawTextSelectType newType, *sarray;
    Widget src = ctx->text.source;

    if (motion)
	newType = ctx->text.s.type;
    else {
	if ((abs((long) time - (long) ctx->text.lasttime) < MULTI_CLICK_TIME)
	    && (pos >= ctx->text.s.left && pos <= ctx->text.s.right)) {
	    sarray = ctx->text.sarray;
	    for (; *sarray != XawselectNull && *sarray != ctx->text.s.type;
		 sarray++)
		;
	    if (*sarray == XawselectNull)
		newType = *(ctx->text.sarray);
	    else {
		newType = *(sarray + 1);
		if (newType == XawselectNull)
		    newType = *(ctx->text.sarray);
	    }
	}
	else		/* single-click event */
	    newType = *(ctx->text.sarray);

	ctx->text.lasttime = time;
    }
    switch (newType) {
	case XawselectPosition:
	    newLeft = newRight = pos;
	    break;
	case XawselectChar:
	    newLeft = pos;
	    newRight = SrcScan(src, pos, XawstPositions, XawsdRight, 1, False);
	    break;
	case XawselectWord:
	case XawselectParagraph:
	case XawselectAlphaNumeric: {
	    XawTextScanType stype;

	    if (newType == XawselectWord)
		stype = XawstWhiteSpace;
	    else if (newType == XawselectParagraph)
		stype = XawstParagraph;
	    else
		stype = XawstAlphaNumeric;

	    /*
	     * Somewhat complicated, but basically I treat the space between
	     * two objects as another object.  The object that I am currently
	     * in then becomes the end of the selection.
	     *
	     * Chris Peterson - 4/19/90.
	     */
	    newRight = SrcScan(ctx->text.source, pos, stype,
			       XawsdRight, 1, False);
	    newRight = SrcScan(ctx->text.source, newRight, stype,
			       XawsdLeft, 1, False);

	    if (pos != newRight)
		newLeft = SrcScan(ctx->text.source, pos, stype,
				  XawsdLeft, 1, False);
	    else
		newLeft = pos;

	    newLeft =SrcScan(ctx->text.source, newLeft, stype,
			     XawsdRight, 1, False);

	    if (newLeft > newRight) {
		XawTextPosition temp = newLeft;
		newLeft = newRight;
		newRight = temp;
	    }
	}   break;
	case XawselectLine:
	    newLeft = SrcScan(src, pos, XawstEOL, XawsdLeft, 1, False);
	    newRight = SrcScan(src, pos, XawstEOL, XawsdRight, 1, False);
	    break;
	case XawselectAll:
	    newLeft = SrcScan(src, pos, XawstAll, XawsdLeft, 1, False);
	    newRight = SrcScan(src, pos, XawstAll, XawsdRight, 1, False);
	    break;
	default:
	    XtAppWarning(XtWidgetToApplicationContext((Widget) ctx),
			 "Text Widget: empty selection array.");
	    return;
    }

    if (newLeft != ctx->text.s.left || newRight != ctx->text.s.right
	|| newType != ctx->text.s.type) {
	ModifySelection(ctx, newLeft, newRight);
	if (pos - ctx->text.s.left < ctx->text.s.right - pos)
	    ctx->text.insertPos = newLeft;
	else
	    ctx->text.insertPos = newRight;
	ctx->text.s.type = newType;
    }
    if (!motion) {	/* setup so we can freely mix select extend calls*/
	ctx->text.origSel.type = ctx->text.s.type;
	ctx->text.origSel.left = ctx->text.s.left;
	ctx->text.origSel.right = ctx->text.s.right;

	if (pos >= ctx->text.s.left + (ctx->text.s.right - ctx->text.s.left) / 2)
	    ctx->text.extendDir = XawsdRight;
	else
	    ctx->text.extendDir = XawsdLeft;
    }
}

/*
 * This routine implements extension of the currently selected text in
 * the "current" mode (i.e. char word, line, etc.). It worries about
 * extending from either end of the selection and handles the case when you
 * cross through the "center" of the current selection (e.g. switch which
 * end you are extending!).
 */
static void
ExtendSelection(TextWidget ctx, XawTextPosition pos, Bool motion)
{
    XawTextScanDirection dir;

    if (!motion) {	/* setup for extending selection */
	if (ctx->text.s.left == ctx->text.s.right) /* no current selection. */
	    ctx->text.s.left = ctx->text.s.right = ctx->text.insertPos;
	else {
	    ctx->text.origSel.left = ctx->text.s.left;
	    ctx->text.origSel.right = ctx->text.s.right;
	}

	ctx->text.origSel.type = ctx->text.s.type;

	if (pos >= ctx->text.s.left + (ctx->text.s.right - ctx->text.s.left) / 2)
	    ctx->text.extendDir = XawsdRight;
	else
	    ctx->text.extendDir = XawsdLeft;
    }
    else	/* check for change in extend direction */
	if ((ctx->text.extendDir == XawsdRight &&
	     pos <= ctx->text.origSel.left) ||
	    (ctx->text.extendDir == XawsdLeft &&
	     pos >= ctx->text.origSel.right)) {
	    ctx->text.extendDir = (ctx->text.extendDir == XawsdRight) ?
		XawsdLeft : XawsdRight;
	    ModifySelection(ctx, ctx->text.origSel.left, ctx->text.origSel.right);
	}

    dir = ctx->text.extendDir;
    switch (ctx->text.s.type) {
	case XawselectWord:
	case XawselectParagraph:
	case XawselectAlphaNumeric: {
	    XawTextPosition left_pos, right_pos;
	    XawTextScanType stype;

	    if (ctx->text.s.type == XawselectWord)
	        stype = XawstWhiteSpace;
	    else if (ctx->text.s.type == XawselectParagraph)
	        stype = XawstParagraph;
	    else
	        stype = XawstAlphaNumeric;

	    /*
	     * Somewhat complicated, but basically I treat the space between
	     * two objects as another object.  The object that I am currently
	     * in then becomes the end of the selection.
	     *
	     * Chris Peterson - 4/19/90.
	     */
	    right_pos = SrcScan(ctx->text.source, pos, stype,
				XawsdRight, 1, False);
	    right_pos =SrcScan(ctx->text.source, right_pos, stype,
			       XawsdLeft, 1, False);

	    if (pos != right_pos)
		left_pos = SrcScan(ctx->text.source, pos, stype,
				   XawsdLeft, 1, False);
	    else
		left_pos = pos;

	    left_pos =SrcScan(ctx->text.source, left_pos, stype,
			      XawsdRight, 1, False);

	    if (dir == XawsdLeft)
		pos = Min(left_pos, right_pos);
	    else	/* dir == XawsdRight */
		pos = Max(left_pos, right_pos);
	}   break;
	case XawselectLine:
	    pos = SrcScan(ctx->text.source, pos, XawstEOL,
			  dir, 1, dir == XawsdRight);
	    break;
	case XawselectAll:
	    pos = ctx->text.insertPos;
	    /*FALLTHROUGH*/
	case XawselectPosition:
	default:
	    break;
    }

    if (dir == XawsdRight)
	ModifySelection(ctx, ctx->text.s.left, pos);
    else
	ModifySelection(ctx, pos, ctx->text.s.right);

    ctx->text.insertPos = pos;
}

/*
 * Function:
 *	_XawTextClearAndCenterDisplay
 *
 * Parameters:
 *	ctx - text widget
 *
 * Description:
 *	  Redraws the display with the cursor in insert point
 *		     centered vertically.
 */
void
_XawTextClearAndCenterDisplay(TextWidget ctx)
{
    int left_margin = ctx->text.left_margin;
    Bool visible = IsPositionVisible(ctx, ctx->text.insertPos);

    _XawTextShowPosition(ctx);

    if (XtIsRealized((Widget)ctx) && visible &&
	left_margin == ctx->text.left_margin) {
	int insert_line = LineForPosition(ctx, ctx->text.insertPos);
	int scroll_by = insert_line - (ctx->text.lt.lines >> 1);
	Boolean clear_to_eol = ctx->text.clear_to_eol;

	XawTextScroll(ctx, scroll_by, 0);
	SinkClearToBG(ctx->text.sink, 0, 0, XtWidth(ctx), XtHeight(ctx));
	ClearWindow(ctx);
	clear_to_eol = ctx->text.clear_to_eol;
	ctx->text.clear_to_eol = False;
	FlushUpdate(ctx);
	ctx->text.clear_to_eol = clear_to_eol;
    }
}

/*
 * Internal redisplay entire window
 * Legal to call only if widget is realized
 */
static void
DisplayTextWindow(Widget w)
{
    TextWidget ctx = (TextWidget)w;

    _XawTextBuildLineTable(ctx, ctx->text.lt.top, False);
    ClearWindow(ctx);
}

static void
TextSinkResize(Widget w)
{
    if (w && XtClass(w)->core_class.resize)
	XtClass(w)->core_class.resize(w);
}

/* ARGSUSED */
void
_XawTextCheckResize(TextWidget ctx)
{
    return;
}

/*
 * Converts (params, num_params) to a list of atoms & caches the
 * list in the TextWidget instance.
 */
Atom *
_XawTextSelectionList(TextWidget ctx, String *list, Cardinal nelems)
{
    Atom *sel = ctx->text.s.selections;
    Display *dpy = XtDisplay((Widget)ctx);
    int n;

    if (nelems > (Cardinal)ctx->text.s.array_size) {
	sel = (Atom *)XtRealloc((char *)sel, sizeof(Atom) * nelems);
	ctx->text.s.array_size = nelems;
	ctx->text.s.selections = sel;
    }
    for (n = nelems; --n >= 0; sel++, list++)
	*sel = XInternAtom(dpy, *list, False);
    ctx->text.s.atom_count = nelems;

    return (ctx->text.s.selections);
}

/*
 * Function:
 *	SetSelection
 *
 * Parameters:
 *	ctx	   - text widget
 *	defaultSel - default selection
 *	l	   - left and right ends of the selection
 *	r	   - ""
 *	list	   - the selection list (as strings).
 *	nelems	   - ""
 *
 * Description:
 *	Sets the current selection.
 *
 * Note:
 *	if (ctx->text.s.left >= ctx->text.s.right) then the selection is unset
 */
void
_XawTextSetSelection(TextWidget ctx, XawTextPosition l, XawTextPosition r,
		     String *list, Cardinal nelems)
{
    if (nelems == 1 && !strcmp (list[0], "none"))
	return;
    if (nelems == 0) {
	String defaultSel = "PRIMARY";
	list = &defaultSel;
	nelems = 1;
    }
    _SetSelection(ctx, l, r, _XawTextSelectionList(ctx, list, nelems), nelems);
}

/*
 * Function:
 *	ModifySelection
 *
 * Parameters:
 *	ctx   - text widget
 *	left  - left and right ends of the selection
 *	right - ""
 *
 * Description:
 *	Modifies the current selection.
 *
 * Note:
 *	if (ctx->text.s.left >= ctx->text.s.right) then the selection is unset
 */
static void
ModifySelection(TextWidget ctx, XawTextPosition left, XawTextPosition right)
{
    if (left == right)
	ctx->text.insertPos = left;
    _SetSelection(ctx, left, right, NULL, 0);
}

/*
 * This routine is used to perform various selection functions. The goal is
 * to be able to specify all the more popular forms of draw-through and
 * multi-click selection user interfaces from the outside.
 */
void
_XawTextAlterSelection(TextWidget ctx, XawTextSelectionMode mode,
		       XawTextSelectionAction action, String *params,
		       Cardinal *num_params)
{
    XawTextPosition position;
    Boolean flag;

    /*
     * This flag is used by TextPop.c:DoReplace() to determine if the selection
     * is okay to use, or if it has been modified.
     */
    if (ctx->text.search != NULL)
	ctx->text.search->selection_changed = True;

    position = PositionForXY(ctx, (int) ctx->text.ev_x, (int) ctx->text.ev_y);

    flag = (action != XawactionStart);
    if (mode == XawsmTextSelect)
	DoSelection(ctx, position, ctx->text.time, flag);
    else		/* mode == XawsmTextExtend */
	ExtendSelection (ctx, position, flag);

    if (action == XawactionEnd)
	_XawTextSetSelection(ctx, ctx->text.s.left, ctx->text.s.right,
			     params, *num_params);
}

/*
 * Function:
 *	UpdateTextInRectangle
 *
 * Parameters:
 *	ctx  - the text widget
 *	rect - rectangle
 *
 * Description:
 *	Updates the text in the given rectangle
 */
static void
UpdateTextInRectangle(TextWidget ctx, XRectangle *rect)
{
    XawTextLineTable *lt;
    int line, y1, y2, x2;

    y1 = rect->y;
    y2 = y1 + rect->height;
    x2 = rect->x + rect->width;

    for (line = 0, lt = &ctx->text.lt; line < lt->lines; line++)
	if (lt->info[line + 1].y > y1)
	    break;
    for (; line <= lt->lines; line++) {
	if (lt->info[line].y > y2)
	    break;
	UpdateTextInLine(ctx, line, rect->x, x2);
    }
}

/*
 * This routine processes all "expose region" XEvents. In general, its job
 * is to the best job at minimal re-paint of the text, displayed in the
 * window, that it can.
 */
/* ARGSUSED */
static void
XawTextExpose(Widget w, XEvent *event, Region region)
{
    TextWidget ctx = (TextWidget)w;
    Boolean clear_to_eol;
    XRectangle expose;

    if (event->type == Expose) {
	expose.x = event->xexpose.x;
	expose.y = event->xexpose.y;
	expose.width = event->xexpose.width;
	expose.height = event->xexpose.height;
    }
    else if (event->type == GraphicsExpose) {
	expose.x = event->xgraphicsexpose.x;
	expose.y = event->xgraphicsexpose.y;
	expose.width = event->xgraphicsexpose.width;
	expose.height = event->xgraphicsexpose.height;
    }
    else
	return;

    _XawTextPrepareToUpdate(ctx);

    if (Superclass->core_class.expose)
	(*Superclass->core_class.expose)(w, event, region);

    clear_to_eol = ctx->text.clear_to_eol;
    ctx->text.clear_to_eol = False;

    UpdateTextInRectangle(ctx, &expose);
    XawTextSinkGetCursorBounds(ctx->text.sink, &expose);
    UpdateTextInRectangle(ctx, &expose);
    SinkClearToBG(ctx->text.sink, expose.x, expose.y,
		  expose.width, expose.height);
    _XawTextExecuteUpdate(ctx);
    ctx->text.clear_to_eol = clear_to_eol;
}

/*
 * This routine does all setup required to syncronize batched screen updates
 */
void
_XawTextPrepareToUpdate(TextWidget ctx)
{
    if (ctx->text.old_insert < 0) {
	InsertCursor((Widget)ctx, XawisOff);
	ctx->text.showposition = False;
	ctx->text.old_insert = ctx->text.insertPos;
	ctx->text.clear_to_eol = False;
#ifndef OLDXAW
	ctx->text.source_changed = SRC_CHANGE_NONE;
#endif
    }
}

/*
 * This is a private utility routine used by _XawTextExecuteUpdate. It
 * processes all the outstanding update requests and merges update
 * ranges where possible.
 */
static void
FlushUpdate(TextWidget ctx)
{
    XmuSegment *seg;
    void (*display_text)(Widget, XawTextPosition, XawTextPosition);

    if (XtIsRealized((Widget)ctx)) {
	ctx->text.s.right = XawMin(ctx->text.s.right, ctx->text.lastPos);
	ctx->text.s.left = XawMin(ctx->text.s.left, ctx->text.s.right);

#ifndef OLDXAW
	if (XawTextSinkBeginPaint(ctx->text.sink) == False)
#endif
	    display_text = OldDisplayText;
#ifndef OLDXAW
	else
	    display_text = DisplayText;
#endif
	for (seg = ctx->text.update->segment; seg; seg = seg->next)
	    (*display_text)((Widget)ctx,
			    (XawTextPosition)seg->x1,
			    (XawTextPosition)seg->x2);
#ifndef OLDXAW
	if (display_text != OldDisplayText) {
	    XawTextSinkDoPaint(ctx->text.sink);
	    XawTextSinkEndPaint(ctx->text.sink);
	}
#endif
    }
    (void)XmuScanlineXor(ctx->text.update, ctx->text.update);
}

static int
CountLines(TextWidget ctx, XawTextPosition left, XawTextPosition right)
{
    if (ctx->text.wrap == XawtextWrapNever || left >= right)
	return (1);
    else {
	XawTextPosition tmp;
	int dim, lines = 0, wwidth = GetMaxTextWidth(ctx);

	while (left < right) {
	    tmp = left;
	    XawTextSinkFindPosition(ctx->text.sink, left,
				    ctx->text.left_margin,
				    wwidth, ctx->text.wrap == XawtextWrapWord,
				    &left, &dim, &dim);
	    ++lines;
	    if (tmp == left)
	      ++left;
	}

	return (lines);
    }
    /*NOTREACHED*/
}

static int
GetMaxTextWidth(TextWidget ctx)
{
    XRectangle cursor;
    int width;

    XawTextSinkGetCursorBounds(ctx->text.sink, &cursor);
    width = (int)XtWidth(ctx) - RHMargins(ctx) - cursor.width;

    return (XawMax(0, width));
}

/*
 * Function:
 *	_XawTextShowPosition
 *
 * Parameters:
 *	ctx - the text widget to show the position
 *
 * Description:
 *	  Makes sure the text cursor visible, scrolling the text window
 *	if required.
 */
void
_XawTextShowPosition(TextWidget ctx)
{
    /*
     * Variable scroll is used to avoid scanning large files to calculate
     * line offsets
     */
    int hpixels, vlines;
    XawTextPosition first, last, top, tmp;
    Bool visible, scroll;

    if (!XtIsRealized((Widget)ctx))
	return;

    /*
     * Checks if a horizontal scroll is required
     */
    if (ctx->text.wrap == XawtextWrapNever) {
	int x, vwidth, distance, dim;
	XRectangle rect;

	vwidth = (int)XtWidth(ctx) - RHMargins(ctx);
	last = SrcScan(ctx->text.source, ctx->text.insertPos,
		       XawstEOL, XawsdLeft, 1, False);
	XawTextSinkFindDistance(ctx->text.sink, last,
				ctx->text.left_margin,
				ctx->text.insertPos,
				&distance, &first, &dim);
	XawTextSinkGetCursorBounds(ctx->text.sink, &rect);
	x = ctx->text.left_margin - ctx->text.r_margin.left;

	if (x + distance + rect.width > vwidth)
	    hpixels = x + distance + rect.width - vwidth + (vwidth >> 2);
	else if (x + distance < 0)
	    hpixels = x + distance - (vwidth >> 2);
	else
	    hpixels = 0;
    }
    else
	hpixels = 0;

    visible = IsPositionVisible(ctx, ctx->text.insertPos);

    /*
     * If the cursor is already visible
     */
    if (!hpixels && visible)
	return;

    scroll = ctx->core.background_pixmap == XtUnspecifiedPixmap && !hpixels;
    vlines = 0;
    first = ctx->text.lt.top;

    /*
     * Needs to scroll the text window
     */
    if (visible)
      top = ctx->text.lt.top;
    else {
        top = SrcScan(ctx->text.source, ctx->text.insertPos,
		      XawstEOL, XawsdLeft, 1, False);

	/*
	 * Finds the nearest left position from ctx->text.insertPos
	 */
	if (ctx->text.wrap != XawtextWrapNever) {
	    int dim, vwidth = GetMaxTextWidth(ctx);

	    last = top;
	    /*CONSTCOND*/
	    while (1) {
		tmp = last;
		XawTextSinkFindPosition(ctx->text.sink, last,
					ctx->text.left_margin, vwidth,
					ctx->text.wrap == XawtextWrapWord,
					&last, &dim, &dim);
		if (last == tmp)
		    ++last;
		if (last <= ctx->text.insertPos)
		    top = last;
		else
		    break;
	    }
	}
    }

    if (scroll) {
	if (ctx->text.insertPos < first) {	/* Scroll Down */
	    while (first > top) {
		last = first;
		first = SrcScan(ctx->text.source, first,
				XawstEOL, XawsdLeft, 2, False);
		vlines -= CountLines(ctx, first, last);
		if (-vlines >= ctx->text.lt.lines) {
		    scroll = False;
		    break;
		}
	    }
	}
	else if (!visible) {			/* Scroll Up */
	    while (first < top) {
		last = first;
		first = SrcScan(ctx->text.source, first,
				XawstEOL, XawsdRight, 1, True);
		vlines += CountLines(ctx, last, first);
		if (vlines > ctx->text.lt.lines) {
		    scroll = False;
		    break;
		}
	    }
	}
	else
	    scroll = False;
    }

    /*
     * If a portion of the text that will be scrolled is visible
     */
    if (scroll)
	XawTextScroll(ctx, vlines ? vlines - (ctx->text.lt.lines >> 1) : 0, 0);
    /*
     * Else redraw the entire text window
     */
    else {
	ctx->text.left_margin -= hpixels;
	if (ctx->text.left_margin > ctx->text.r_margin.left)
	    ctx->text.left_margin = ctx->text.margin.left =
		ctx->text.r_margin.left;

	if (!visible) {
	    vlines = ctx->text.lt.lines >> 1;
	    if (vlines)
		top = SrcScan(ctx->text.source, ctx->text.insertPos,
			      XawstEOL, XawsdLeft, vlines + 1, False);

	    if (ctx->text.wrap != XawtextWrapNever) {
		int dim;
		int n_lines = CountLines(ctx, top, ctx->text.insertPos);
		int vwidth = GetMaxTextWidth(ctx);

		while (n_lines-- > vlines) {
		    tmp = top;
		    XawTextSinkFindPosition(ctx->text.sink, top,
					    ctx->text.left_margin,
					    vwidth,
					    ctx->text.wrap == XawtextWrapWord,
					    &top, &dim, &dim);
		    if (tmp == top)
			++top;
		}
	    }
	    _XawTextBuildLineTable(ctx, top, True);
	}
	else
	    ClearWindow(ctx);
    }
    ctx->text.clear_to_eol = True;
}

#ifndef OLDXAW
static int
ResolveLineNumber(TextWidget ctx)
{
    int line_number = ctx->text.lt.base_line;
    XawTextPosition position = ctx->text.lt.top;

    if (ctx->text.lt.base_line < 1)
	return (ctx->text.line_number);

    if (ctx->text.wrap == XawtextWrapNever
	&& IsPositionVisible(ctx, ctx->text.insertPos))
	line_number += LineForPosition(ctx, ctx->text.insertPos);
    else if (position < ctx->text.insertPos) {
	while (position < ctx->text.insertPos) {
	    position = SrcScan(ctx->text.source, position,
			       XawstEOL, XawsdRight, 1, True);
	    if (position <= ctx->text.insertPos) {
		++line_number;
		if (position == ctx->text.lastPos) {
		    line_number -= !_XawTextSourceNewLineAtEOF(ctx->text.source);
		    break;
		}
	    }
	}
    }
    else if (position > ctx->text.insertPos) {
	while (position > ctx->text.insertPos) {
	    position = SrcScan(ctx->text.source, position,
			       XawstEOL, XawsdLeft, 1, False);
	    if (--position >= ctx->text.insertPos)
		--line_number;
	}
    }

    return (line_number);
}
#endif

/*
 * This routine causes all batched screen updates to be performed
 */
void
_XawTextExecuteUpdate(TextWidget ctx)
{
    if (ctx->text.update_disabled || ctx->text.old_insert < 0)
	return;

    if(ctx->text.old_insert != ctx->text.insertPos || ctx->text.showposition)
	_XawTextShowPosition(ctx);

    FlushUpdate(ctx);
    InsertCursor((Widget)ctx, XawisOn);
    ctx->text.old_insert = -1;
#ifndef OLDXAW
    _XawTextSetLineAndColumnNumber(ctx, False);
#endif
}

static void
XawTextDestroy(Widget w)
{
    TextWidget ctx = (TextWidget)w;

    DestroyHScrollBar(ctx);
    DestroyVScrollBar(ctx);

    XtFree((char *)ctx->text.s.selections);
    XtFree((char *)ctx->text.lt.info);
    XtFree((char *)ctx->text.search);
    XmuDestroyScanline(ctx->text.update);
    XtReleaseGC((Widget)ctx, ctx->text.gc);
}

/*
 * by the time we are managed (and get this far) we had better
 * have both a source and a sink 
 */
static void
XawTextResize(Widget w)
{
    TextWidget ctx = (TextWidget)w;

    PositionVScrollBar(ctx);
    PositionHScrollBar(ctx);
    TextSinkResize(ctx->text.sink);

    ctx->text.showposition = True;
    _XawTextBuildLineTable(ctx, ctx->text.lt.top, True);
}

/*
 * This routine allow the application program to Set attributes.
 */
/*ARGSUSED*/
static Boolean
XawTextSetValues(Widget current, Widget request, Widget cnew,
		 ArgList args, Cardinal *num_args)
{
    TextWidget oldtw = (TextWidget)current;
    TextWidget newtw = (TextWidget)cnew;
    Boolean redisplay = False;
    Boolean display_caret = newtw->text.display_caret;
#ifndef OLDXAW
    Boolean show_lc = False;
#endif

    newtw->text.display_caret = oldtw->text.display_caret;
    _XawTextPrepareToUpdate(newtw);
    newtw->text.display_caret = display_caret;

    if (oldtw->text.r_margin.left != newtw->text.r_margin.left) {
	newtw->text.left_margin = newtw->text.margin.left =
	    newtw->text.r_margin.left;
	if (newtw->text.vbar != NULL) {
	    newtw->text.left_margin += XtWidth(newtw->text.vbar) +
		XtBorderWidth(newtw->text.vbar);
	}
	redisplay = True;
    }

    if (oldtw->text.scroll_vert != newtw->text.scroll_vert) {
	if (newtw->text.scroll_vert == XawtextScrollAlways)
	    CreateVScrollBar(newtw);
	else
	    DestroyVScrollBar(newtw);

	redisplay = True;
    }

    if (oldtw->text.r_margin.bottom != newtw->text.r_margin.bottom) {
	newtw->text.margin.bottom = newtw->text.r_margin.bottom;
	if (newtw->text.hbar != NULL)
	    newtw->text.margin.bottom += newtw->text.hbar->core.height +
		newtw->text.hbar->core.border_width;
	redisplay = True;
    }

    if (oldtw->text.scroll_horiz != newtw->text.scroll_horiz) {
	if (newtw->text.scroll_horiz == XawtextScrollAlways)
	    CreateHScrollBar(newtw);
	else
	    DestroyHScrollBar(newtw);

	redisplay = True;
    }

    if (oldtw->text.source != newtw->text.source) {
#ifndef OLDXAW
	show_lc = True;
	_XawSourceRemoveText(oldtw->text.source, cnew,
			     oldtw->text.source &&
			     XtParent(oldtw->text.source) == cnew);
	_XawSourceAddText(newtw->text.source, cnew);
#endif
	_XawTextSetSource((Widget)newtw, newtw->text.source, newtw->text.lt.top,
			  newtw->text.insertPos);
    }

    newtw->text.redisplay_needed = False;
    XtSetValues((Widget)newtw->text.source, args, *num_args);
    XtSetValues((Widget)newtw->text.sink, args, *num_args);

    if (oldtw->text.wrap != newtw->text.wrap
	|| oldtw->text.lt.top != newtw->text.lt.top
	|| oldtw->text.insertPos != newtw->text.insertPos
	|| oldtw->text.r_margin.right != newtw->text.r_margin.right
	|| oldtw->text.r_margin.top != newtw->text.r_margin.top
	|| oldtw->text.sink != newtw->text.sink
	|| newtw->text.redisplay_needed) {
	if (oldtw->text.wrap != newtw->text.wrap) {
	    newtw->text.left_margin = newtw->text.margin.left =
		newtw->text.r_margin.left;
	    if (oldtw->text.lt.top == newtw->text.lt.top)
		newtw->text.lt.top = SrcScan(newtw->text.source, 0, XawstEOL,
					     XawsdLeft, 1, False);
	}
	newtw->text.showposition = True;
#ifndef OLDXAW
	show_lc = True;
	newtw->text.source_changed = SRC_CHANGE_OVERLAP;
#endif
	_XawTextBuildLineTable(newtw, newtw->text.lt.top, True);
	redisplay = True;
    }

#ifndef OLDXAW
    if (newtw->text.left_column < 0)
	newtw->text.left_column = 0;
    if (newtw->text.right_column < 0)
	newtw->text.right_column = 0;
#endif

    _XawTextExecuteUpdate(newtw);

#ifndef OLDXAW
    if (show_lc)
	_XawTextSetLineAndColumnNumber(newtw, True);
#endif

    if (redisplay)
	_XawTextSetScrollBars(newtw);

    return (redisplay);
}

/* invoked by the Simple widget's SetValues */
static Bool
XawTextChangeSensitive(Widget w)
{
    Arg args[1];
    TextWidget tw = (TextWidget)w;

    (*(&simpleClassRec)->simple_class.change_sensitive)(w);

    XtSetArg(args[0], XtNancestorSensitive,
	     (tw->core.ancestor_sensitive && tw->core.sensitive));
    if (tw->text.vbar)
	XtSetValues(tw->text.vbar, args, ONE);
    if (tw->text.hbar)
	XtSetValues(tw->text.hbar, args, ONE);
    return (False);
}

/*
 * Function:
 *	XawTextGetValuesHook
 *
 * Parameters:
 *	w	 - Text Widget
 *	args	 - argument list
 *	num_args - number of args
 *
 * Description:
 *	  This is a get values hook routine that gets the
 *		     values in the text source and sink.
 */
static void
XawTextGetValuesHook(Widget w, ArgList args, Cardinal *num_args)
{
    XtGetValues(((TextWidget)w)->text.source, args, *num_args);
    XtGetValues(((TextWidget)w)->text.sink, args, *num_args);
}

/*
 * Function:
 *	FindGoodPosition
 *
 * Parameters:
 *	pos - any position
 *
 * Description:
 *	Returns a valid position given any postition.
 *
 * Returns:
 *	A position between (0 and lastPos)
 */
static XawTextPosition
FindGoodPosition(TextWidget ctx, XawTextPosition pos)
{
    if (pos < 0)
	return (0);
    return (((pos > ctx->text.lastPos) ? ctx->text.lastPos : pos));
}

/* Li wrote this so the IM can find a given text position's screen position */
void
_XawTextPosToXY(Widget w, XawTextPosition pos, Position *x, Position *y)
{
    int line, ix, iy;

    LineAndXYForPosition((TextWidget)w, pos, &line, &ix, &iy);
    *x = ix;
    *y = iy;
}

/*******************************************************************
The following routines provide procedural interfaces to Text window state
setting and getting. They need to be redone so than the args code can use
them. I suggest we create a complete set that takes the context as an
argument and then have the public version lookup the context and call the
internal one. The major value of this set is that they have actual application
clients and therefore the functionality provided is required for any future
version of Text.
********************************************************************/
void
XawTextDisplay(Widget w)
{
    TextWidget ctx = (TextWidget)w;

    if (!XtIsRealized(w))
	return;

    _XawTextPrepareToUpdate(ctx);
    ctx->text.clear_to_eol = True;
    DisplayTextWindow(w);
    _XawTextExecuteUpdate(ctx);
}

void
XawTextSetSelectionArray(Widget w, XawTextSelectType *sarray)
{
    ((TextWidget)w)->text.sarray = sarray;
}

void
XawTextGetSelectionPos(Widget w, XawTextPosition *left, XawTextPosition *right)
{
    *left = ((TextWidget)w)->text.s.left;
    *right = ((TextWidget)w)->text.s.right;
}

void
_XawTextSetSource(Widget w, Widget source,
		  XawTextPosition top, XawTextPosition startPos)
{
    TextWidget ctx = (TextWidget)w;
#ifndef OLDXAW
    Bool resolve = False;
#endif

#ifndef OLDXAW
    if (source != ctx->text.source)
	_XawSourceRemoveText(ctx->text.source, w, ctx->text.source &&
			     XtParent(ctx->text.source) == w);
    _XawSourceAddText(source, w);

    if (source != ctx->text.source || ctx->text.insertPos != startPos)
	resolve = True;

    ctx->text.source_changed = SRC_CHANGE_OVERLAP;
#endif
    ctx->text.source = source;
    ctx->text.s.left = ctx->text.s.right = 0;
    ctx->text.lastPos = GETLASTPOS;
    top = FindGoodPosition(ctx, top);
    startPos = FindGoodPosition(ctx, startPos);
    ctx->text.insertPos = ctx->text.old_insert = startPos;
    _XawTextPrepareToUpdate(ctx);

    _XawTextBuildLineTable(ctx, top, True);

    _XawTextExecuteUpdate(ctx);
#ifndef OLDXAW
    if (resolve)
	_XawTextSetLineAndColumnNumber(ctx, True);
#endif
}

void
XawTextSetSource(Widget w, Widget source, XawTextPosition top)
{
    _XawTextSetSource(w, source, top, top);
}

/*
 * This public routine deletes the text from startPos to endPos in a source and
 * then inserts, at startPos, the text that was passed. As a side effect it
 * "invalidates" that portion of the displayed text (if any), so that things
 * will be repainted properly.
 */
int
XawTextReplace(Widget w, XawTextPosition startPos, XawTextPosition endPos,
	       XawTextBlock *text)
{
    TextWidget ctx = (TextWidget)w;
    int result;
#ifndef OLDXAW
    Cardinal i;
    TextSrcObject src = (TextSrcObject)ctx->text.source;

    for (i = 0; i < src->textSrc.num_text; i++)
	_XawTextPrepareToUpdate((TextWidget)src->textSrc.text[i]);
#else
    _XawTextPrepareToUpdate(ctx);
#endif

    endPos = FindGoodPosition(ctx, endPos);
    startPos = FindGoodPosition(ctx, startPos);
    result = _XawTextReplace(ctx, startPos, endPos, text);

#ifndef OLDXAW
    for (i = 0; i < src->textSrc.num_text; i++)
	_XawTextExecuteUpdate((TextWidget)src->textSrc.text[i]);
#else
    _XawTextExecuteUpdate(ctx);
#endif

    return (result);
}

XawTextPosition 
XawTextTopPosition(Widget w)
{
    return (((TextWidget)w)->text.lt.top);
}

XawTextPosition 
XawTextLastPosition(Widget w)
{
    return (((TextWidget)w)->text.lastPos);
}

void
XawTextSetInsertionPoint(Widget w, XawTextPosition position)
{
    TextWidget ctx = (TextWidget)w;

    _XawTextPrepareToUpdate(ctx);
    ctx->text.insertPos = FindGoodPosition(ctx, position);
    ctx->text.showposition = True;
    ctx->text.from_left = -1;

    _XawTextExecuteUpdate(ctx);
#ifndef OLDXAW
    _XawTextSetLineAndColumnNumber(ctx, False);
#endif
}

XawTextPosition
XawTextGetInsertionPoint(Widget w)
{
    return (((TextWidget)w)->text.insertPos);
}

/*
 * Note: Must walk the selection list in opposite order from TextLoseSelection
 */
void
XawTextUnsetSelection(Widget w)
{
    TextWidget ctx = (TextWidget)w;

    while (ctx->text.s.atom_count != 0) {
	Atom sel = ctx->text.s.selections[ctx->text.s.atom_count - 1];

	if (sel != (Atom) 0) {
	    /*
	     * As selections are lost the atom_count will decrement
	     */
	    if (GetCutBufferNumber(sel) == NOT_A_CUT_BUFFER)
		XtDisownSelection(w, sel, ctx->text.time);
	    TextLoseSelection(w, &sel); /* In case this is a cut buffer, or
					   XtDisownSelection failed to call us */
	}
    }
}

void
XawTextSetSelection(Widget w, XawTextPosition left, XawTextPosition right)
{
    TextWidget ctx = (TextWidget)w;

    _XawTextPrepareToUpdate(ctx);
    _XawTextSetSelection(ctx, FindGoodPosition(ctx, left),
			 FindGoodPosition(ctx, right), NULL, 0);
    _XawTextExecuteUpdate(ctx);
}

void
XawTextInvalidate(Widget w, XawTextPosition from, XawTextPosition to)
{
    TextWidget ctx = (TextWidget)w;

    from = FindGoodPosition(ctx, from);
    to = FindGoodPosition(ctx, to);
    ctx->text.lastPos = GETLASTPOS;
    _XawTextPrepareToUpdate(ctx);
    _XawTextNeedsUpdating(ctx, from, to);
    _XawTextExecuteUpdate(ctx);
}

/*ARGSUSED*/
void
XawTextDisableRedisplay(Widget w)
{
    ((TextWidget)w)->text.update_disabled = True;
    _XawTextPrepareToUpdate((TextWidget)w);
}

void 
XawTextEnableRedisplay(Widget w)
{
    TextWidget ctx = (TextWidget)w;
    XawTextPosition lastPos;

    if (!ctx->text.update_disabled)
	return;

    ctx->text.update_disabled = False;
    lastPos = ctx->text.lastPos = GETLASTPOS;
    ctx->text.lt.top = FindGoodPosition(ctx, ctx->text.lt.top);
    ctx->text.insertPos = FindGoodPosition(ctx, ctx->text.insertPos);

    if (ctx->text.s.left > lastPos || ctx->text.s.right > lastPos)
	ctx->text.s.left = ctx->text.s.right = 0;

    _XawTextExecuteUpdate(ctx);
}

Widget
XawTextGetSource(Widget w)
{
    return (((TextWidget)w)->text.source);
}

Widget
XawTextGetSink(Widget w)
{
    return (((TextWidget)w)->text.sink);
}

void
XawTextDisplayCaret(Widget w,
#if NeedWidePrototypes
	int display_caret
#else
	Boolean display_caret
#endif
)
{
    TextWidget ctx = (TextWidget)w;

    if (XtIsRealized(w)) {
	_XawTextPrepareToUpdate(ctx);
	ctx->text.display_caret = display_caret;
	_XawTextExecuteUpdate(ctx);
    }
    else
	ctx->text.display_caret = display_caret;
}

/*
 * Function:
 *	XawTextSearch
 *
 * Parameters:
 *	w    - text widget
 *	dir  - direction to search
 *	text - text block containing info about the string to search for
 *
 * Description:
 *	Searches for the given text block.
 *
 * Returns:
 *	The position of the text found, or XawTextSearchError on an error
 */
XawTextPosition
XawTextSearch(Widget w,
#if NeedWidePrototypes
	int dir,
#else
	XawTextScanDirection dir,
#endif
	XawTextBlock *text)
{
    TextWidget ctx = (TextWidget)w;

    return (SrcSearch(ctx->text.source, ctx->text.insertPos, dir, text));
}

TextClassRec textClassRec = {
  /* core */
  {
    (WidgetClass)&simpleClassRec,	/* superclass */ 
    "Text",				/* class_name */
    sizeof(TextRec),			/* widget_size */
    XawTextClassInitialize,		/* class_initialize */
    NULL,				/* class_part_init */
    False,				/* class_inited */
    XawTextInitialize,			/* initialize */
    NULL,				/* initialize_hook */
    XawTextRealize,			/* realize */
    _XawTextActionsTable,		/* actions */
    0,					/* num_actions */
    resources,				/* resources */
    XtNumber(resources),		/* num_resource */
    NULLQUARK,				/* xrm_class */
    True,				/* compress_motion */
    XtExposeGraphicsExpose |		/* compress_exposure */
	XtExposeNoExpose,
    True,				/* compress_enterleave */
    False,				/* visible_interest */
    XawTextDestroy,			/* destroy */
    XawTextResize,			/* resize */
    XawTextExpose,			/* expose */
    XawTextSetValues,			/* set_values */
    NULL,				/* set_values_hook */
    XtInheritSetValuesAlmost,		/* set_values_almost */
    XawTextGetValuesHook,		/* get_values_hook */
    NULL,				/* accept_focus */
    XtVersion,				/* version */
    NULL,				/* callback_private */
    _XawDefaultTextTranslations,	/* tm_table */
    XtInheritQueryGeometry,		/* query_geometry */
    XtInheritDisplayAccelerator,	/* display_accelerator */
    NULL,				/* extension */
  },
  /* simple */
  {
    XawTextChangeSensitive,		/* change_sensitive */
  },
  /* text */
  {
    NULL,				/* extension */
  }
};

WidgetClass textWidgetClass = (WidgetClass)&textClassRec;