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 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104
|
//=================================================================
//
// Copyright (C) 1995,1996,1997.1998 Bruce E. Wampler
//
// This file is part of the V C++ GUI Framework, and is covered
// under the terms of the GNU Library General Public License,
// Version 2. This library has NO WARRANTY. See the source file
// vapp.cxx for more complete information about license terms.
//
// This code is based on Bruce Wampler's editor, See. I think a
// bit of history about See will explain some things about the
// reasons some of the code structure and variable names are
// what they are. See end end of this file for the See history.
//
//
//===============================================================
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <v/vwindow.h>
#include <v/vkeys.h>
#include <v/vtexted.h>
#include <v/vfinddlg.h>
#include <v/vrepldlg.h>
#include <v/vmodald.h>
#include <v/vdialog.h>
/// Working notes:
// allow line list to grow dynamically by adding lines and
// copying old list to new one, then deleting old one.
const long lineListSize = 5000; // allocation increment for lines
// one static find pattern
globalState vTextEditor::gState = {
0, // findAtBeginning
0, // fixed_scroll
1, // delOnInsert
8, // tabspc
0 // wraplm
};
int vTextEditor::findCaseSensitive = 0;
int vTextEditor::findDown = 1;
int vTextEditor::findWrap = 0;
int vTextEditor::replaceConfirm = 0;
int vTextEditor::replaceAll = 0;
char vTextEditor::theFindPat[MAX_LINE + 2] = "";
char vTextEditor::theReplPat[MAX_LINE + 2] = "";
// =========================>>> vTextEditor::vTextEditor <<<================
vTextEditor::vTextEditor(vCmdWindow* parent)
{
_parent = parent; // remember who my parent was
// Set the command interp
_teCInt = new vTextEdCmdInterp(this, (vCmdWindow*)parent);
state.readOnly =
use_wild = 0; // use wild cards in match
oldcol = 1;
state.ins_mode = 1; // insert (vs. overtype)
b_lastln =
leftmg = xoutcm =
tvdlin = state.echof = lastLine = 1;
linptr = 0;
mouseMoveRow = mouseMoveCol =
mouseCol = mouseRow = -1; // no mouse stuff yet
scroll_lin = -1;
setCursor = 0;
for (int ix = 0 ; ix < 26 ; ++ix)
noteloc[ix] = 0;
_lines = 0; // make these null for smarter destructor
_highlighter = ChHighlight;
// create space, initialize buffers
undoBuff = new char[MAX_UNDO+2];
undoBuff[0] = 0;
initBuff(); // initialize buffer routines
resetBuff();
myConfirm = new vTEConfirm(_parent,"Replace?");
}
//=========================>>> vTextEditor::~vTextEditor <<<================
vTextEditor::~vTextEditor()
{
if (_teCInt)
delete _teCInt; // kill our command interp
delete [] undoBuff; // zap the undo buffer
if (_lines)
{
for (long ix = 0 ; ix < _nextLine ; ++ix) // delete contents
delete [] _lines[ix];
delete [] _lines; // delete if created
}
}
//=========================>>> vTextEditor::ChangeCmdInterp <<<================
void vTextEditor::ChangeCmdInterp(vTextEdCmdInterp* newCI)
{
if (_teCInt)
delete _teCInt; // delete current command interp
if (newCI == 0) // use default CI
_teCInt = new vTextEdCmdInterp(this, (vCmdWindow*)_parent);
else
_teCInt = newCI;
}
// #########################################################################
//
// buffer methods
//
// These are designed to be easily overriden to allow more complex
// buffer management -- it should be possible to easily design a
// package that allows unlimited file size, for example.
//
// #########################################################################
//===========================>>> vTextEditor::initBuff <<<==================
void vTextEditor::initBuff(void)
{
// initialize buffer routines - handled as a method so
// can override
_lines = new char*[lineListSize];
_maxLines = lineListSize - 1; // safety factor
_lines[0] = new char[2]; strcpy(_lines[0],"\n");
_lines[1] = new char[2]; strcpy(_lines[1],"\n");
for (long lx = 2 ; lx < lineListSize ; ++lx)
_lines[lx] = 0;
_nextLine = 2;
lastLine = lastLineBF();
}
// =======================>>> vTextEditor::resetBuff <<<====================
void vTextEditor::resetBuff() // open the buffer
{
// values used by buffer management
if (_lines)
{
delete [] _lines[0];
delete [] _lines[1];
for (long ix = 2 ; ix < _nextLine ; ++ix) // delete contents
{
if (_lines[ix])
delete [] _lines[ix];
}
_lines[0] = new char[2]; strcpy(_lines[0],"\n");
_lines[1] = new char[2]; strcpy(_lines[1],"\n");
}
else
{
_lines = new char*[lineListSize];
_maxLines = lineListSize - 1; // safety factor
_lines[0] = new char[2]; strcpy(_lines[0],"\n");
_lines[1] = new char[2]; strcpy(_lines[1],"\n");
}
_curLineBF =
_nextLine = 1;
wasColCmd = 0;
// values used by editor code
mark.beg_lin = mark.end_lin = /* manually clear mark range */
mark.beg_col = mark.end_col = 0; /* DO NOT CALL ClearMarkRange() */
mark.beg_chr = mark.end_chr = 0;
curlin = lastLine = 1;
curchr = _lines[1];
last_col_out =
state.counter =
state.changes = 0;
dsplin = ddline = 1;
state.ins_mode = 1; // insert (vs. overtype)
}
// =========================>>> vTextEditor::addLine <<<====================
int vTextEditor::addLine(char* line)
{
// This method adds the line to the end of the working buffer.
// Note: line numbers start at 1!
if (!line)
return 0;
if (_nextLine >= _maxLines) // check for max lines
if (!reallocLines())
return 0;
BUFFPTR bp = appendToBuff(line);
if (!bp)
return 0;
if (curchr == _lines[_nextLine])
{
// When the very first line is added, curchr must be updated to
// point to the new line instead of to the initial "\n"!
curchr = bp;
}
_lines[_nextLine++] = bp; // point line array to beginning of line
lastLine = lastLineBF(); // update
return 1;
}
// ======================>>> vTextEditor::appendToBuff <<<==================
BUFFPTR vTextEditor::appendToBuff(char* line)
{
// add text to the buffer -- allocate the spaced right here!
BUFFPTR buff;
BUFFPTR bp;
int len = 0;
char* cp;
for (cp = line ; *cp && *cp != '\n' && *cp != '\r' ; ++cp) // break on eos or eol
{
++len;
}
buff = (BUFFPTR) new char[len+2]; // the space for the line
if (!buff) // did we get the space?
return 0;
for (bp = buff, cp = line ; *cp && *cp != '\n' && *cp != '\r' ; ) // break on eos or eol
{
*bp++ = *cp++;
}
*bp++ = '\n';
*bp++ = 0;
return buff;
}
// =========================>>> vTextEditor::insertLine <<<====================
int vTextEditor::insertLine(char* line, long before, bool doUndo)
{
// This method adds the line before line before.
// line numbers start at 1
if (!line)
return 0;
if (doUndo)
undoOff("Undo");
if (_nextLine >= _maxLines) // check for max lines
if (!reallocLines())
return 0;
BUFFPTR bp = appendToBuff(line); // allocate space for line
if (!bp)
return 0;
if (curchr == _lines[_nextLine])
{
// When the very first line is added, curchr must be updated to
// point to the new line instead of to the initial "\n"!
curchr = bp;
}
for (long to = _nextLine++ ; to > before ; --to)
{
_lines[to] = _lines[to-1];
}
_lines[before] = bp; // point line array to beginning of line
lastLine = lastLineBF();
adjustLines(before,1);
return 1;
}
// =====================>>> vTextEditor::getFirstLine <<<===================
int vTextEditor::getFirstLine(char* line, int maxChars)
{
// return the first line in the buffer
return getLine(line, maxChars,1);
}
// =========================>>> vTextEditor::getNextLine <<<================
int vTextEditor::getNextLine(char* line, int maxChars)
{
// return the next line in the text buffer, not including end of line
// returns the number of characters, -1 if at end of file
return getLine(line, maxChars, _curLineBF +1);
}
// =========================>>> vTextEditor::getLine <<<===================
int vTextEditor::getLine(char* line, int maxChars, long lineNum)
{
// return the next line in the text buffer, not including end of line
// returns the number of characters, -1 if at end of file
if (lineNum >= _nextLine || lineNum < 1) // at end!
return -1;
_curLineBF = lineNum; // Set _curlineBF
int len = 0;
for (char* ix = _lines[_curLineBF] ; *ix != 0 && *ix != '\n' && len < maxChars ; )
{
line[len++] = *ix++; // copy & track length
}
line[len] = 0; // terminate
return len;
}
// ===================>>> vTextEditor::displayBuff <<<======================
void vTextEditor::displayBuff(long lineNum, int paintIt)
{
// All lines added, so now display the buffer
if (lineNum >= _nextLine)
lineNum = _nextLine - 1;
if (lineNum < 1)
lineNum = 1;
curlin = lineNum; // Internal stuff begins with line 1
curchr = GLine(curlin); /* first char of buffer */
lastLine = lastLineBF();
int oldef = state.echof;
state.echof = paintIt;
ShowVScroll(1);
Verify();
state.echof = oldef;
}
// =========================>>> vTextEditor::GLine <<<======================
BUFFPTR vTextEditor::GLine(long lineNum)
{
if (lineNum < 1 || lineNum >= _nextLine)
{
return _lines[1]; // something safe
}
return _lines[lineNum];
}
// =====================>>> vTextEditor::deleteCharBF <<<===================
BUFFPTR vTextEditor::deleteCharBF(BUFFPTR charNum, long lineNum)
{
// First, shift everything up to next end of line left
// This routine must not assume that lines are stored contiguously
BUFFPTR ix;
BUFFPTR newCurChar = charNum;
char combine[MAX_LINE*2+2];
char chrDeleted = GCh(charNum);
if (lastLineBF() < 1)
return 0;
if (IsEndLine(chrDeleted)) // combine two lines?
{
if (lineNum < (_nextLine - 1)) // There IS a next line
{
BUFFPTR bp;
int to;
int offset;
// combine this line with the next line
to = 0;
for (bp = _lines[lineNum] ; *bp && !IsEndLine(*bp) ; )
combine[to++] = *bp++;
offset = to; // track where we were
for (bp = _lines[lineNum+1] ; *bp && !IsEndLine(*bp) ; )
combine[to++] = *bp++;
*bp = 0;
combine[to] = 0;
// create buffer for it
bp = appendToBuff(combine);
if (!bp)
return 0;
newCurChar = bp + offset; // this is new cur char
// free the old space
delete [] _lines[lineNum];
delete [] _lines[lineNum+1];
_lines[lineNum] = bp;
// shift up the rest of the lines
for (long lp = lineNum + 1 ; lp < _nextLine ; ++lp)
_lines[lp] = _lines[lp + 1];
--_nextLine;
lastLine = lastLineBF();
}
adjustLines(lineNum,-1); // one line deleted
}
else
{
newCurChar = charNum;
for (ix = charNum + 1 ; *ix && !IsEndLine(*ix) ; ++ix)
*(ix-1) = *ix;
*(ix-1) = *ix;
*ix = 0;
}
return newCurChar;
}
// ===================>>> vTextEditor::deleteLinesBF <<<====================
long vTextEditor::deleteLinesBF(long start, long end)
{
// delete the lines in the specified range,
// return new last line of file
long to, from;
if (lastLineBF() < 1)
return 0;
for (to = start ; to <= end ; ++to)
delete[] _lines[to];
for (to = start, from = end+1 ; from < _nextLine ; ++to, ++from)
{
_lines[to] = _lines[from]; // shift
}
_lines[from] = 0;
_nextLine -= (end - start + 1);
adjustLines(start, -(end - start + 1));
lastLine = lastLineBF();
if (_nextLine <= 1)
{
_lines[1] = new char[2]; strcpy(_lines[1], "\n");
return 1;
}
return _nextLine - 1;
}
// =====================>>> vTextEditor::insertCharBF <<<===================
int vTextEditor::insertCharBF(int chr, BUFFPTR& curchar, long& curline)
{
// insert a char into the buffer at curline, curchar.
// return new curchar, curline
char ln1[MAX_LINE+2];
char ln2[MAX_LINE+2];
char* newLine;
BUFFPTR origLine = _lines[curline]; // original full line
int offSet;
int ix;
BUFFPTR fp;
if (chr == 0) // don't allow this case
return 0;
offSet = 0;
for (BUFFPTR bp = origLine ; bp != curchar ; ++bp, ++offSet)
;
if (chr != '\n')
{
for (ix = 0, fp = origLine ; *fp != 0 && ix < MAX_LINE ; )
{
if (ix == offSet)
ln1[ix++] = chr; // add the new char
ln1[ix++] = *fp++; // copy original line
}
if (ix == offSet)
ln1[ix++] = chr; // add the new char
ln1[ix] = 0;
newLine = appendToBuff(ln1);
if (!newLine) // see if got the space
return 0;
delete [] _lines[curline]; // free the old space
_lines[curline] = newLine; // point line to new space
curchar = newLine + offSet + 1; // update curchar
}
else // adding newline
{
// we are inserting a newline!
for (ix = 0, fp = origLine ; *fp != 0 && ix < MAX_LINE ; )
{
if (ix == offSet)
ln1[ix++] = chr; // add the new char
ln1[ix++] = *fp++; // copy original line
}
if (ix == offSet)
ln1[ix++] = chr; // add the new char
ln1[ix] = 0;
// ln1 now has two lines in it, split em up.
char* cp;
for (cp = ln1 ; *cp != '\n' ; ++cp) // find first part
;
*cp++ = 0;
char *tp;
for (tp = ln2 ; *cp ; ) // copy second part
*tp++ = *cp++;
*tp = 0;
newLine = appendToBuff(ln1);
if (!newLine) // see if got the space
return 0;
delete [] _lines[curline]; // free the old space
_lines[curline] = newLine; // point line to new space
if (_nextLine >= _maxLines) // check for max lines
if (!reallocLines()) // add some more lines
return 0;
long lx;
for (lx = _nextLine ; lx > curline ; --lx)
_lines[lx] = _lines[lx - 1];
newLine = appendToBuff(ln2);
if (!newLine) // see if got the space
return 0;
++curline; ++_nextLine;
lastLine = lastLineBF();
adjustLines(curline,1); // one more line
_lines[curline] = newLine; // point line to new space
curchar = _lines[curline]; // point to line beginning
}
return 1;
}
// ==========================>>> vTextEditor::lineLenBF <<<=================
int vTextEditor::lineLenBF(long lineNum)
{
if (lineNum < 1 || lineNum >= (_nextLine - 1))
return 0;
int len = 0;
for (BUFFPTR ix = _lines[lineNum] ; *ix != '\n' && *ix != 0 ; ++ix)
++len;
return len;
}
// ===================>>> vTextEditor::adjustLines <<<======================
void vTextEditor::adjustLines(long line_1, int by)
{
/* adjust starting line number int note locaetions */
int ix;
for (ix = 0 ; ix < 26 ; ++ix) /* adjust notes */
{
if (noteloc[ix] > line_1) /* page needs fixing */
noteloc[ix] += (long) by;
}
}
// #########################################################################
//
// Command Interface methods
//
// #########################################################################
// ===================>>> vTextEditor::EditCommand <<<======================
int vTextEditor::EditCommand(int id, long val)
{
// returns 1 if command succeeded, 0 if failed
// or -1 if not processed
int retval = 1; // assume OK
switch (id)
{
case edCut: // ^X : Cut
{
EditCut();
break;
}
case edCopy: // ^C : Copy
{
EditCopy();
break;
}
case edPaste: // ^V: Paste
{
EditPaste();
break;
}
case edBalMatch: // find matching paren (up to val lines away)
retval = BalMatch(val);
break;
case edFind: // find
{
vFindDialog fdlg((vBaseWindow*)_parent);
if (!fdlg.AskFindPat(theFindPat, MAX_LINE,
findCaseSensitive, findDown, findWrap))
return 0;
retval = Find(theFindPat,
findCaseSensitive,findDown,findWrap);
break;
}
case edFindNext: // find again
{
if (*theFindPat) // something to find
retval = FindNext(findCaseSensitive,findDown,findWrap);
else
retval = 0;
break;
}
case edReplace: // Replace
{
vReplaceDialog rdlg((vBaseWindow*)_parent);
if (!rdlg.AskReplacePat(theFindPat, MAX_LINE,
theReplPat, MAX_LINE,
findCaseSensitive, findDown, findWrap,
replaceConfirm, replaceAll))
return 0;
retval = Replace(theFindPat, theReplPat,
findCaseSensitive, findDown, findWrap,
replaceConfirm, replaceAll);
break;
}
case edBufferBottom: // move to bottom of file (no val)
bufferBottom();
break;
case edCenterScreen: // center cursor in screen
CenterScreen();
break;
case edCharDelete: // delete val chars
retval = charDelete(val);
break;
case edCharFoldCase: // swap case of val letters
retval = charFoldCase(val);
break;
case edCharInsert: // insert char val
retval = charInsert(val);
break;
case edCharRight: // move val chars right
retval = charRight(val);
break;
case edFill:
retval = lineFill(val);
break;
case edHelp: // Show help screen
_teCInt->CmdInterpHelp();
break;
case edLineBeginning: // move to line beginning (no val)
lineBeginning();
break;
case edLineDown: // move down val lines in column
retval = lineDown(val);
break;
case edLineDownBeg: // move down val lines
retval = lineDownBeg(val);
break;
case edLineDelete: // delete val lines
lineDelete(val);
break;
case edLineDeleteFront: // delete to beginning of line (no val)
retval = lineDeleteFront();
break;
case edLineDeleteToEnd: // delete to end of line (no val)
retval = lineDeleteToEnd();
break;
case edLineEnd: // move to end of line (no val)
lineEnd();
break;
case edLineGoto: // move cursor to line val
retval = lineGoto(val);
break;
case edLineOpen: // open val new blank lines
retval = lineOpen(val);
break;
case edFormatC: // format C code lines
retval = formatC(val);
break;
case edNoteLocation: // note which line we are on
if (val >= 'a' && val <= 'z' )
val = val-'a';
if (val < 0 || val > 25) // 0 - 25 valid
val = 0;
noteloc[val] = GetCurLine();
break;
case edNoteGoto: // goto noted locateion
if (val >= 'a' && val <= 'z' )
val = val-'a';
if (val < 0 || val > 25) // 0 - 25 valid
val = 0;
retval = lineGoto(noteloc[val]);
break;
case edUndo: // undo the last edit
retval = undoEdit();
break;
case edWordRight: // move cursor val words right
wordRight(val);
break;
case edScrollDown: // scroll val lines without changing cursor
scrollDown(val);
break;
case edVerify: // repaint screen
Verify();
break;
default: // editor can't handle
retval = -1;
break;
}
return retval;
}
// ======================>>> vTextEditor::EditKeyIn <<<=====================
int vTextEditor::EditKeyIn(vKey key, unsigned int shift)
{
// process keystrokes -- return 1 if processed, 0 if
// command fails, and -1 if not recognized
return _teCInt->ProcessKey(key, shift);
}
// ===================>>> vTextEditor::defaultKeyIn <<<=====================
int vTextEditor::defaultKeyIn(vKey key, unsigned int shift)
{
// this is a virtual function so that it would be possible
// to override this method to implement a modal editor like
// vi or see.
// Returns -1 if not processed, 1 if successful, 0 if
// command didn't succeed.
int chr = (int)key;
if ((shift & VKM_Alt || shift & VKM_Ctrl)
&& (chr < ' ' || chr > 0x7E))
return -1;
if (chr < ' ' && chr != '\r' && chr != '\t' && chr != '\n')
return -1;
if (chr > 0xFE) // ignore function and alt keys
return -1;
return charInsert(key);
}
// =======================>>> vTextEditor::Find <<<=========================
int vTextEditor::Find(char* pat, int caseSensitive, int Down, int Wrap)
{
// Copy a new pattern to the pattern buffer
char *fp = pat;
int ix;
for (ix = 0 ; *fp && ix < MAX_LINE ; )
theFindPat[ix++] = *fp++;
theFindPat[ix] = 0;
return FindNext(caseSensitive, Down, Wrap); // now use findNext
}
// =====================>>> vTextEditor::FindNext <<<=======================
int vTextEditor::FindNext(int caseSensitive, int Down, int Wrap)
{
// find a pattern = return 1 if found, 0 if not
BUFFPTR findBP;
char* foundAt;
int ix;
int lineOffset = 0;
char workPat[MAX_LINE + 2]; // working pattern
char workLine[MAX_LINE + 2]; // working line
long findLine = curlin;
long origLine = curlin;
if (lastLineBF() < 1)
return 0;
int patLen = strlen(theFindPat);
for (ix = 0 ; theFindPat[ix] != 0 ; ++ix)
{
if (caseSensitive)
workPat[ix] = theFindPat[ix];
else
workPat[ix] = tolower(theFindPat[ix]);
}
workPat[ix] = 0;
if (Down)
{
if (gState.findAtBeginning)
{
charRight(1,0);
}
findBP = curchr;
for ( ; ; )
{
lineOffset = findBP - GLine(findLine); // if not at beginning
for (ix = 0 ; ix < MAX_LINE && GCh(findBP) != 0 ; ++findBP, ++ix)
{
if (caseSensitive)
workLine[ix] = GCh(findBP);
else
workLine[ix] = tolower(GCh(findBP));
}
workLine[ix] = 0;
foundAt = strstr(workLine,workPat); // see if in there
if (foundAt != 0) // We found it!
{
undoOff("Undo");
ClearMarkRange(); /* no range now */
curlin = findLine;
int offset = foundAt - &workLine[0];
curchr = GLine(curlin) + offset + lineOffset;
// set up mark range
mark.beg_lin = mark.end_lin = curlin;
mark.beg_col = col_pos(curchr,0);
mark.end_col = col_pos(curchr+patLen,0);
mark.beg_chr = curchr;
mark.end_chr = curchr + patLen;
if (!gState.findAtBeginning)
curchr += patLen; // leave at end
// need intelligent screen updating
// This puts the pattern in the middle if we've
// moved over a screen
if ((curlin - origLine) > GetRows())
CenterScreen();
else
update(curlin - origLine);
return 1;
}
if (findLine >= lastLine)
{
StatusMessage(" Pattern Not Found");
if (gState.findAtBeginning)
{
charRight(-1,0);
}
return 0;
}
else
{
findLine++;
findBP = GLine(findLine);
}
}
if (gState.findAtBeginning)
{
charRight(-1,0);
}
StatusMessage(" Pattern Not Found");
return 0;
}
else // find up
{
if (!gState.findAtBeginning)
{
charRight(-1,0);
}
findBP = GLine(findLine);
BUFFPTR lim = curchr; // for first time
for ( ; ; )
{
for (ix = 0 ; ix < MAX_LINE && GCh(findBP) != 0 ;
++findBP, ++ix)
{
if (lim != 0 && findBP >= lim)
break; // first line only
if (caseSensitive)
workLine[ix] = GCh(findBP);
else
workLine[ix] = tolower(GCh(findBP));
}
lim = 0; // done with first line
workLine[ix] = 0;
foundAt = strstr(workLine,workPat); // search from end
if (foundAt != 0) // We found it!
{
// We found the first instance, now look for the last
char *cp = foundAt;
char *lastPat = 0;
char *nextPat = 0;
for (cp++ ; (nextPat = strstr(cp,workPat)) != 0 ; ++cp)
{
lastPat = nextPat; // remember last one
}
if (lastPat != 0) // there was a later pat
{
foundAt = &workLine[strlen(workLine)-strlen(lastPat)];
}
undoOff("Undo");
ClearMarkRange(); /* no range now */
curlin = findLine;
int offset = foundAt - &workLine[0];
curchr = GLine(curlin) + offset;
// set up mark range
mark.beg_lin = mark.end_lin = curlin;
mark.beg_col = col_pos(curchr,0);
mark.end_col = col_pos(curchr+patLen,0);
mark.beg_chr = curchr;
mark.end_chr = curchr + patLen;
if (!gState.findAtBeginning)
curchr += patLen; // leave at end
// need intelligent screen updating
// This puts the pattern in the middle if we've
// moved over a screen
if ((curlin - origLine) > GetRows())
CenterScreen();
else
update(curlin - origLine);
return 1;
}
if (findLine <= 0)
{
if (!gState.findAtBeginning)
{
charRight(1,0);
}
StatusMessage(" Pattern Not Found");
return 0;
}
else
{
findLine--;
findBP = GLine(findLine);
}
}
if (!gState.findAtBeginning)
{
charRight(1,0);
}
StatusMessage(" Pattern Not Found");
return 0;
}
}
// =====================>>> vTextEditor::Replace <<<====================
int vTextEditor::Replace(const char* pat, const char* repl,
int caseSensitive, int Down, int Wrap, int Confirm, int All)
{
const char* fp = pat;
const char* rp = repl;
int ix;
for (ix = 0 ; fp && *fp && ix < MAX_LINE ; )
theFindPat[ix++] = *fp++;
theFindPat[ix] = 0;
for (ix = 0 ; rp && *rp && ix < MAX_LINE ; )
theReplPat[ix++] = *rp++;
theReplPat[ix] = 0;
if (state.echof && All && !Confirm) // echo off if all
state.echof = 0;
while (FindNext(caseSensitive, Down, Wrap)) // now use findNext
{
ReplaceConfirm doit = YesRepl; // assume replace once
if (Confirm)
{
doit = (ReplaceConfirm) myConfirm->ConfirmRep();
}
if (doit == YesRepl || doit == YesReplNext || doit == RepAll)
{
checkIfScrolled();
(void) SaveKillLine(1,true); // save current line
RemoveMarkRange(false);
ClearMarkRange(); /* no range now */
for (const char* cp = repl ; *cp ; ++cp)
{
charInsert((int)*cp,false);
}
undoOn("Undo Replace");
// just overrite the undo stuff caused by RemoveMarkRange
// undo: dodoReplace(findpattern,repl);
}
if (doit == RepAll) // change to replace all mode
{
Confirm = 0; All = 1;
if (state.echof)
state.echof = 0;
}
if (doit == QuitR)
{
if (All && !Confirm)
{
state.echof = 1;
Verify();
}
return 1;
}
if (doit == YesReplNext || doit == NoReplNext || All)
continue;
else
{
if (All && !Confirm)
{
state.echof = 1;
Verify();
}
return 1;
}
}
if (All && !Confirm)
{
state.echof = 1;
Verify();
}
return 0;
}
// =======================>>> vTextEditor::HPage <<<========================
void vTextEditor::HPage(int shown, int top)
{
vTextCanvasPane::HPage(shown, top);
}
// =======================>>> vTextEditor::VPage <<<========================
void vTextEditor::VPage(int shown, int top)
{
long target;
if (lastLineBF() < 1 || lastLine <= GetRows())
{
setScrollBar();
return;
}
if ((top+shown) >= 100)
target = lastLine;
else if (top < 1)
target = 1;
else
target = ((long)top * lastLine) / 100;
if (target < 1)
target = 1;
if (target > lastLine)
target = lastLine;
if (scroll_lin < 0)
scrollDown(target - curlin);
else
scrollDown(target-scroll_lin);
}
// =====================>>> vTextEditor::HScroll <<<========================
void vTextEditor::HScroll(int step)
{
vTextCanvasPane::HScroll(step);
}
// =====================>>> vTextEditor::VScroll <<<========================
void vTextEditor::VScroll(int step)
{
if (step < 0)
scrollDown(-1);
else
scrollDown(1);
}
// =====================>>> vTextEditor::FontChanged <<<====================
void vTextEditor::FontChanged(VCONST vFont& newFont)
{
vTextCanvasPane::FontChanged(newFont);
}
// ====================>>> vTextEditor::TextMouseDown <<<===================
void vTextEditor::TextMouseDown(int row, int col, int button)
{
if (button != 1) // ignore right button moves
{
vTextCanvasPane::TextMouseDown(row, col, button);
return;
}
mouseMoveRow = mouseRow = row + 1; // convert to 1,1 corner
mouseMoveCol = mouseCol = col + 1;
MouseMoveTo(mouseRow,mouseCol); // doing the move at mouse down
// makes selections easier
setScrollBar();
ClearMarkRange(); // forces deselect
}
// ======================>>> vTextEditor::TextMouseUp <<<===================
void vTextEditor::TextMouseUp(int r, int c, int button)
{
if (button != 1) // ignore right button moves
{
vTextCanvasPane::TextMouseUp(r, c, button);
return;
}
}
// ======================>>> vTextEditor::CenterScreen <<<=================
void vTextEditor::CenterScreen()
{
// Some tricky stuff to put the cursor in the middle.
int old_ddline = ddline;
if (lastLine > GetRows())
ddline = GetRows() / 2;
else
ddline = lastLine / 2;
if (ddline < 1)
ddline = 1;
newscr();
tvxy(findX(),tvy); /* reset cursor to current position */
ddline = old_ddline;
setScrollBar(); // MUST do this!
ChangeLoc(curlin,col_pos(curchr,0));
}
// ======================>>> vTextEditor::MouseMoveTo <<<=================
void vTextEditor::MouseMoveTo(int row, int col)
{
// used by MouseUp and MouseMove to update cursor location
if (scroll_lin < 0) // regular move
{
int cnt = row - tvdlin;
if (cnt != 0) // difference
lineDownBeg((long)cnt,0); // changing
else
lineBeginning(); // same row
tvhdln();
}
else // switching position to scroll window
{
curlin = maxl((long)1,minl(scroll_lin + row - 1,lastLine));
curchr = GLine(curlin);
scroll_lin = -1;
long old_ddline = ddline; // trick - terrible awful coding, but...
ddline = row;
newscr();
int xf = findX();
tvxy(xf,tvy); /* reset cursor to current position */
ddline = old_ddline;
tvhdln();
}
b_lastln = curlin;
while (!IsEndLine(GCh(curchr)) && col > findX() )
{
++curchr;
}
tvhdln();
ChangeLoc(curlin,col_pos(curchr,0));
_teCInt->MouseJump(curlin, col_pos(curchr,0)); // notify cmdinterp
}
// ======================>>> vTextEditor::TextMouseMove <<<=================
void vTextEditor::TextMouseMove(int r, int c, int button)
{
if (button != 1) // ignore right button moves
{
vTextCanvasPane::TextMouseMove(r, c, button);
}
else // highlighting text
{
if (mouseRow <= 0 || mouseCol <= 0)
return;
if (curlin < 1 || GetCols() < 1 || lastLineBF() < 1)
return;
int row = r + 1; // convert to 1,1 corner
int col = c + 1;
if (mouseMoveRow != row) // have moved a row down or up
{
if (row > mouseMoveRow) // moved down
{
int cnt = row - mouseMoveRow;
AddToRangeRow(cnt, row, col); // add rows
mouseMoveRow = row;
// and update to columns
AddToRangeCol(row, col);
mouseMoveCol = col;
}
else // moving mouse up rows
{
int cnt = row - mouseMoveRow;
AddToRangeRow(cnt, row, col); // add rows
mouseMoveRow = row;
// and update to columns
AddToRangeCol(row, col);
mouseMoveCol = col;
}
}
else if (mouseMoveCol != col) // mouse to right of start
{
AddToRangeCol(row, col);
mouseMoveCol = col;
}
}
}
// =========================>>> vTextEditor::AddToRangeCol <<<=================
void vTextEditor::AddToRangeCol(int row, int col)
{
if (col_pos(curchr,0) < col) // add to right
{
if (mark.beg_lin == 0) /* no lines yet */
{
mark.end_lin = mark.beg_lin = curlin;
mark.end_chr = mark.beg_chr = curchr;
mark.end_col = mark.beg_col = col_pos(curchr,0);
while (!IsEndLine(GCh(curchr))
&& col_pos(curchr,0) < col) // find new col
charRight(1,0);
mark.end_chr = curchr;
mark.end_col = col_pos(curchr,0);
}
else if (mark.end_lin < curlin)
{
mark.end_lin = curlin;
mark.end_chr = curchr;
mark.end_col = col_pos(curchr,0);
}
else if (mark.beg_lin == curlin && mark.end_lin == curlin
&& col_pos(curchr,0) > mark.end_col)
{
mark.beg_col = mark.end_col;
mark.beg_chr = mark.end_chr;
mark.end_chr = curchr;
mark.end_col = col_pos(curchr,0);
}
else if (mark.beg_lin == curlin && // some of this line marked
((mark.end_lin == curlin && col_pos(curchr,0) < mark.end_col)
|| mark.end_lin != mark.beg_lin))
{
while (!IsEndLine(GCh(curchr))
&& col_pos(curchr) < col) // find new col
charRight(1,0);
mark.beg_col = col_pos(curchr,0);
mark.beg_chr = curchr;
}
else // normal add to marked
{
while (!IsEndLine(GCh(curchr))
&& col_pos(curchr) < col) // find new col
charRight(1,0);
mark.end_lin = curlin;
mark.end_chr = curchr;
mark.end_col = col_pos(curchr,0);
}
}
else // add to left
{
if (mark.beg_lin == 0) // no lines yet, so note it
{
if (curchr == GLine(curlin))
return; // No op at line beginning
mark.end_lin = mark.beg_lin = curlin;
mark.end_chr = mark.beg_chr = curchr;
mark.end_col = mark.beg_col = col_pos(curchr,0);
while (col_pos(curchr) > col) // find new col
charRight(-1,0);
mark.beg_chr = curchr;
mark.beg_col = col_pos(curchr,0);
}
else if (mark.end_lin < curlin)
{
mark.end_lin = curlin;
mark.end_chr = curchr;
mark.end_col = col_pos(curchr,0);
}
else if (mark.end_lin == curlin && // some of this line marked
((mark.beg_lin == curlin && col_pos(curchr,0) > mark.beg_col)
|| mark.beg_lin != curlin))
{
// handles two cases: highlight on current line only,
// or highlight starts on some line before
while (col_pos(curchr) > col) // find new col
charRight(-1,0);
mark.end_col = col_pos(curchr,0);
mark.end_chr = curchr;
}
else if (mark.end_lin == curlin // some of this line marked
&& col_pos(curchr,0) > mark.beg_col)
{
while (col_pos(curchr) > col) // find new col
charRight(-1,0);
mark.end_col = col_pos(curchr,0);
mark.end_chr = curchr;
}
else // normal add to marked
{
while (col_pos(curchr) > col) // find new col
charRight(-1,0);
mark.beg_lin = curlin;
mark.beg_chr = curchr;
mark.beg_col = col_pos(curchr,0);
}
}
tvxy(1,tvy); // Finally, fix up the highlight
type_lines(curlin, 1); // by repainting current line
tvhdln(); // and rehome cursor
((vWindow *)_parent)->SetValue(M_Cut, 1, Sensitive);
((vWindow *)_parent)->SetValue(M_Copy, 1, Sensitive);
}
// =========================>>> vTextEditor::AddToRangeRow <<<=================
void vTextEditor::AddToRangeRow(long cnt, int row, int col)
{
/* add to mark range varaiable, update current line, then go down */
if (cnt == 0)
{
ClearMarkRange(); /* no range now */
return;
}
if (cnt > 0)
{
if (curlin + cnt > lastLine) /* past end? */
cnt = lastLine - curlin + 1;
if (mark.beg_lin == 0) /* no lines yet */
{
mark.beg_lin = curlin;
mark.beg_chr = curchr;
mark.beg_col = col_pos(curchr,0);
}
if (mark.end_lin == 0)
{
mark.end_lin = curlin + cnt - 1;
}
else if (mark.beg_lin == curlin // some of this line marked
&& mark.end_lin != mark.beg_lin)
{
if (mark.end_lin == curlin + cnt
&& col_pos(curchr,0) > mark.end_col)
{
mark.beg_lin = curlin + cnt;
mark.beg_col = mark.end_col;
mark.beg_chr = mark.end_chr;
}
else
{
mark.beg_col = col_pos(curchr,0);
mark.beg_chr = GLine(curlin+cnt); // Something legal
mark.beg_lin = curlin + cnt;
}
}
else
{
mark.end_lin += cnt;
mark.end_col = 1000; /* BIG */
mark.end_chr = 0; /* end of line */
}
/* paint the lines to highlight */
if (tvdlin+cnt-1 <= GetRows())
{
tvxy(1,tvy); /* fix it up */
type_lines(curlin,(int)cnt);
}
if (curlin == lastLine && cnt > 0) /* down from last line? */
{
return;
}
lineDown(cnt,0);
}
else if (cnt < 0) // up lines
{
if (curlin + cnt < 1) // past start?
cnt = -curlin;
if (mark.end_lin == curlin && mark.beg_lin == curlin
&& mark.end_lin != 0)
{
mark.end_col = mark.beg_col;
mark.end_chr = mark.beg_chr;
lineDown(cnt,0);
mark.beg_lin = curlin;
mark.beg_col = col_pos(curchr,0);
mark.beg_chr = curchr;
}
else
{
if (mark.end_lin == 0) //no lines yet
{
mark.beg_lin = mark.end_lin = curlin;
mark.end_chr = curchr;
mark.end_col = col_pos(curchr,0);
mark.beg_col = 1;
mark.beg_chr = GLine(curlin);
}
if (mark.end_lin == curlin // some of this line marked
&& mark.end_lin != mark.beg_lin)
{
if (mark.beg_lin == curlin + cnt
&& col_pos(curchr,0) < mark.beg_col)
{
mark.end_lin = curlin + cnt;
mark.end_col = mark.beg_col;
mark.end_chr = mark.beg_chr;
lineDown(cnt,0);
mark.beg_col = col_pos(curchr,0);
mark.beg_chr = curchr;
}
else
{
lineDown(cnt,0);
mark.end_col = col_pos(curchr,0);
mark.end_chr = GLine(curlin+cnt); // Something legal
mark.end_lin = curlin;
}
}
else
{
// now move up specified number of lines, no clrRange
lineDown(cnt, 0);
mark.beg_lin = curlin;
mark.beg_chr = curchr;
mark.beg_col = col_pos(curchr,0);
}
}
/* paint the lines to highlight */
tvxy(1,tvy); /* fix it up */
type_lines(curlin,(int)-cnt+1);
tvhdln();
}
((vWindow *)_parent)->SetValue(M_Cut, 1, Sensitive);
((vWindow *)_parent)->SetValue(M_Copy, 1, Sensitive);
}
// =========================>>> vTextEditor::Redraw <<<=====================
void vTextEditor::Redraw(int x, int y, int w, int h)
{
vTextCanvasPane::Redraw(x,y,w,h);
}
// ======================>>> vTextEditor::ResizeText <<<====================
void vTextEditor::ResizeText(const int rows, const int cols)
{
// now, if the curlin is on the new scrolled screen, we need to
// repaint it so the cursor and highlight area show
Verify();
if (ddline >= GetRows())
{
HideTextCursor();
setCursor = 1;
}
else
setCursor = 0;
setScrollBar();
}
// #########################################################################
//
// screen update stuff - maps old see/tv to vTextCanvasPane methods
//
// #########################################################################
// =========================>>> vTextEditor::tvxy <<<=======================
void vTextEditor::tvxy(int ix, int iy)
{
// tvxy - position cursor at position x,y
// x=1 is left most column
// y=1 is top most line
tvx = ix;
tvy = iy;
if (!state.echof)
return;
GotoRC(iy-1,ix-1); // Convert to 0,0 based coords
}
// =========================>>> vTextEditor::tvplin <<<=====================
void vTextEditor::tvplin(long lineNum, BUFFPTR chrptr, int whole_line,
int hibeg, int hiend)
{ /* tvplin - put line beginning at chrptr
will only type what will fit on screen (using xout) */
char tmp;
int linlen, origx, need_hi_end;
int hiStart = -1;
int hiLast = -1;
BUFFPTR i;
char linout[MAX_LINE+1];
need_hi_end = 0;
last_col_out = linptr = 0;
origx = xoutcm; /* save x so can get true linelen */
for (i = chrptr; !IsEndLine(GCh(i)) && xoutcm < MAX_LINE; ++i)
{
/* xoutcm has current column. If a hilite goes here,
add to the linout array as 0x01 or 0x02.
*/
if (xoutcm == hibeg)
{
hiStart = linptr; // where highlight starts
need_hi_end = 1; /* will need a follow */
}
if (xoutcm == hiend)
{
hiLast = linptr; // where highlight ends
need_hi_end = 0; /* don't need a follow now */
}
if (GCh(i) < ' ' && GCh(i) >= 0) /* control character? */
{
if (GCh(i) == '\t')
{
if (gState.tabspc > 0)
{
do
{
linout[linptr++] = ' '; /* replace with blanks */
++xoutcm;
}
while ( ((xoutcm-1) % gState.tabspc) != 0);
}
else
{
linout[linptr++] = '^';
linout[linptr++] = 'I';
xoutcm += 2;
linout[linptr++] = '*';
++xoutcm;
}
continue;
}
else /* other control character */
{
linout[linptr++] = '^';
++xoutcm;
if (xoutcm == GetCols() && !(IsEndLine(GCh(i))) )
continue;
tmp = GCh(i) + '@';
linout[linptr++] = tmp;
}
} /*# end if control character */
else
{
linout[linptr++] = GCh(i);
}
++xoutcm;
}
if (need_hi_end)
{
hiLast = linptr;
need_hi_end = 0; /* don't need a follow now */
}
linout[linptr] = 0; // terminate the line
int lineStart = 0;
// This whole line stuff is left over. I don't know exactly what it
// did, but it breaks repainting when the margin is shifted, so just
// ignore the whole_line value... BEW: 10/1/98
//@ if (whole_line) /* write whole line */
{
last_col_out = linlen = mint(GetCols(),linptr-leftmg+1);
lineStart = leftmg - 1;
}
if (linlen > 0)
paintLine(linout, lineStart, hiStart, hiLast, lineNum);
}
// ======================>>> vTextEditor::paintLine <<<=====================
void vTextEditor::paintLine(char* linout, int lineStart,
int hiStart, int hiLast, long lineNum)
{
// paint a line. This can be overridden (e.g., for syntax highlighting).
// linout: the line to output with tabs converted to spaces, etc.
// lineStart: where to begin printing the line (for hoiz. scrolling)
// hiStart, hiLast: reverse text attribute
// lineNum: the real line number in the buffer this is. This is unused
// normally, but can be used for syntax highlighting to get
// surrounding context.
int linlen = strlen(linout);
if (linlen > 0) // only draw if there!
{
char tmpChr; // to hold char
if (hiStart < lineStart && hiLast < lineStart) // no highlight
{
DrawText(&linout[lineStart]); // simple case
}
else // has highlighting
{
if (hiStart > lineStart) // highlight from beginning
{
tmpChr = linout[hiStart]; // remember char
linout[hiStart] = 0;
DrawText(&linout[lineStart]); // normal part
linout[hiStart] = tmpChr; // replace
}
tmpChr = linout[hiLast]; // remember char
linout[hiLast] = 0; // make end of string
DrawAttrText(&linout[hiStart],getHighlight()); // highlight part
linout[hiLast] = tmpChr; // replace
if (linlen > hiLast) // rest of line
DrawText(&linout[hiLast]);
}
}
}
// ====================>>> vTextEditor::highLightCurLine <<<======================
void vTextEditor::highLightCurLine()
{
// Highlight current line using reverse instead of highlight
ChrAttr old = getHighlight();
setHighlight(ChReverse);
type_lines(curlin, 1, 1); // by repainting current line
tvhdln(); // and rehome cursor
setHighlight(old);
}
// ====================>>> vTextEditor::type_lines <<<======================
void vTextEditor::type_lines(long ibeg, int icnt, int wholeLine)
{ /* type_lines - type icnt lines starting at lines[ibeg]
no cr/lf on the last line */
long i, lim;
int hibeg, hiend;
BUFFPTR start;
if (!state.echof)
return;
if (ibeg < 1 || ibeg > lastLine)
return;
xoutcm = tvx;
lim = ibeg+icnt-1;
for (i = ibeg ; i <= lim && i <= lastLine ; ++i)
{
/* simple check for whole line highlighting for now */
if (wholeLine)
{
hibeg = 1;
hiend = 1000;
}
else if (i == mark.beg_lin)
{
hibeg = mark.beg_col;
if (i == mark.end_lin)
hiend = mark.end_col;
else
hiend = 1000;
}
else if (i >= mark.beg_lin && i <= mark.end_lin)
{
hibeg = 1;
if (i == mark.end_lin)
hiend = mark.end_col;
else
hiend = 1000;
}
else
{
hibeg = hiend = -1;
}
start = GLine(i);
tvplin(i,start,1,hibeg, hiend); /* type out a wole line */
xoutcm = 1;
if (last_col_out < GetCols())
tvelin(); /* erase rest of line */
if ( i != lim )
tvxy(1,++tvy);
}
}
// =========================>>> vTextEditor::Verify <<<=====================
void vTextEditor::Verify(void)
{ // Verify - rewrite the screen or type current line with cursor
int xf, old_ddline;
if (!state.echof)
return;
if (lastLineBF() < 1)
{
tvclr();
return;
}
old_ddline = ddline;
ddline = dsplin;
newscr();
xf = findX();
tvxy(xf,tvy); /* reset cursor to current position */
ddline = old_ddline;
ChangeLoc(curlin,col_pos(curchr,0));
setScrollBar();
}
// =====================>>> vTextEditor::tvbotb <<<=========================
void vTextEditor::tvbotb(int n)
{ // tvbotb - make n blank lines at the bottom of the screen
if (!state.echof)
return;
if (n >= GetRows())
{
tvclr();
}
else
{
tvxy(1,GetRows()); /* go to real last line */
ScrollText(n);
int j = GetRows()-n+1; /* home to virtual last line */
tvxy(1,j); /* position at first new blank line */
}
}
// ==========================>>> vTextEditor::tvclr <<<==========================
void vTextEditor::tvclr(void)
{ // tvclr - clear the entire screen and home
if (!state.echof)
return;
Clear();
tvxy(1,1);
tvx = tvy = 1;
}
// ========================>>> vTextEditor::tvelin <<<======================
void vTextEditor::tvelin(void)
{ // tvelin - erase the rest of the current line
if (!state.echof)
return;
int r,c;
GetRC(r,c);
ClearRow(r, c);
}
// ========================>>> vTextEditor::tvescr <<<======================
void vTextEditor::tvescr(void)
{ // tvelin - erase from cursor to end of screen
if (!state.echof)
return;
ClearToEnd(tvy-1, tvx - 1);
}
// ==========================>>> vTextEditor::tvtopb <<<====================
void vTextEditor::tvtopb(int n)
{ // tvtopb - create n blank lines at the top of the screen
if (!state.echof)
return;
tvxy(1,1); /* home first */
if ( n >= GetRows())
{
tvescr(); /* simply erase the screen */
}
else
{
ScrollText(-n);
}
tvxy(1,1); /* home first */
}
// =======================>>> vTextEditor::bufferBottom <<<=================
void vTextEditor::bufferBottom(void)
{ // bufferBottom - move cursor to bottom of current buffer
if (lastLineBF() < 1)
return;
ClearMarkRange(); /* no range now */
wasColCmd = 0;
curlin = lastLine; /* the last real line of text */
curchr = GLine(curlin); /* the first char of that line */
lineEnd(); /* goto end of the line */
newscr(); /* update the screen */
}
// #########################################################################
//
// char methods
//
// #########################################################################
// =======================>>> vTextEditor::charDelete <<<===================
int vTextEditor::charDelete(long cnt, bool doUndo)
{ // charDelete - delete next cnt characters
static char chdel;
int abscnt,newx;
int i;
if (state.readOnly || lastLineBF() < 1)
return 0;
checkIfScrolled();
if (RemoveMarkRange(doUndo)) /* there was a range to kill */
return 1;
ClearMarkRange(); /* no range now */
wasColCmd = 0;
abscnt = (cnt > 0) ? cnt : (-cnt); /* remember absolute value of cnt */
state.changes += abscnt; /* track changes */
if (cnt < 0)
charRight(cnt,0);
if (doUndo)
{
long cntCurlin = curlin; // copy of current line
BUFFPTR cntCurchr = curchr; // copy of current char
// find out how many lines are involved, then save that many...
bool partialLine = false;
bool lastDeleted = false; // end of line in last line deleted?
int cntLines = 1;
for (i = 0 ; i < abscnt ; ++i)
{
if (IsEndLine(GCh(cntCurchr))) /* at end of line */
{
if (cntCurlin >= lastLine)
{
break; /* don't go beyond end! */
}
lastDeleted = true; // have reached end of line
++cntLines; // how many lines we've seen
++cntCurlin;
cntCurchr = GLine(cntCurlin);
}
else
{
lastDeleted = false; // haven't deleted whole line
++cntCurchr;
}
}
// We need to handle partial lines on undo. If we delete just
// part of the first line, the first line will be combined
// with the next line. Same is true if we have a partial last
// line - it will be combined with the line above it. In
// either case, we need to delete the line that remains at
// curlin after the delete before restoring the unkill buffer.
partialLine = curchr != GLine(curlin); // partial first line
if (curchr == GLine(curlin) && lastDeleted && cntLines == 2)
partialLine = true; // exactly one line (but count is 2!)
if (!lastDeleted) // partial last line
partialLine = true;
if (!SaveKillLine(cntLines, partialLine))
{
//@@ abort if can't save all. no-op for now.
}
}
for (i = 0 ; i < abscnt ; ++i)
{
chdel = GCh(curchr); /* remember the char we are deleting */
curchr = deleteCharBF(curchr,curlin);
if (curchr == 0)
return 0;
if (!IsEndLine(chdel))
{
if (i + 1 == abscnt || IsEndLine(GCh(curchr)))
{
if (abscnt > 1)
Verify(); // need to verify for big deletes
else
retypeCurlin(1); // just retype current line
}
}
else
{
lastLine = lastLineBF();
if (tvdlin < dsplin) // not at end
{
tvescr(); /* erase rest of screen */
tvxy(1,tvy); /* fix it up */
type_lines(curlin,mint((int)(GetRows() - tvdlin+1),
(int)(lastLine - curlin)) );
newx = findX(); /* where cursor will go */
tvxy(newx,tvy); /* reposition cursor */
}
else /* at end of buffer */
Verify();
}
}
ChangeLoc(curlin,col_pos(curchr,0));
return 1;
}
//=====================>>> vTextEditor::charFoldCase <<<====================
int vTextEditor::charFoldCase(long cnt)
{
/* fold from upper to lower or lower to upper case if letter */
int ni;
char fchr;
if (state.readOnly)
return 0;
ClearMarkRange(); /* no range now */
wasColCmd = 0;
for (ni = 0 ; ni < cnt ; ++ni)
{
fchr = GCh(curchr); /* get current character */
if (fchr >= 'a' && fchr <= 'z')
fchr = cupper(fchr);
else if (fchr >= 'A' && fchr <= 'Z')
fchr = clower(fchr);
if (IsEndLine(fchr))
charRight((long)1,1);
else
{
if (!charDelete((long) 1, false)) // delete cur char
return 0;
if (!charInsert((int)fchr,false)) /* and put back */
return 0;
}
}
return 1;
}
// ====================>>> vTextEditor::charInsert <<<======================
int vTextEditor::charInsert(int ival, bool doUndo)
{
int xf;
// int force_tidy;
int chr;
if (state.readOnly)
return 0;
checkIfScrolled();
if (doUndo)
{
// for now, we don't undo inserts
undoOff("Undo");
}
if (gState.delOnInsert)
RemoveMarkRange(doUndo); /* no range now */
wasColCmd = 0;
chr = ival; /* use the passed value */
++state.changes; /* count changes */
if (lastLineBF() == 0) // First actual insert!
{
char ln1[4];
resetBuff();
if (chr == '\n') // this effecitvely is two lines!
{
ln1[0] = 0;
addLine(ln1);
addLine(ln1);
tvdlin = 2;
displayBuff(1); // get a proper display
}
else
{
ln1[0] = chr;
ln1[1] = 0;
addLine(ln1);
if (!_lines[1])
return 0;
curlin = 1;
curchr = _lines[1] + 1;
tvdlin = 1;
displayBuff(1); // get a proper display
lineEnd(); // move to end of the line
}
setScrollBar();
return 1;
}
if (!state.ins_mode) // overtype mode?
{
if (!charDelete(1,false)) // delete nextchar
return 0;
}
if (chr == '\r')
chr = '\n'; // this is our convention
if (chr == 0)
chr = ' ';
if (!insertCharBF(chr, curchr, curlin))
return 0;
lastLine = lastLineBF(); // update this one
if (chr != '\n')
{
retypeCurlin(0); // just retype whole line
}
else
{
long dummy1; int dummy2;
FindDispLine(dummy1,dummy2);
if (tvdlin < dsplin) // not at end
{
tvescr(); /* erase rest of screen */
tvxy(1,tvy); /* fix it up */
type_lines(curlin,mint((int)(GetRows() - tvdlin + 1),
(int)(lastLine - curlin)) );
}
else /* at end of buffer */
Verify();
setScrollBar();
tvhdln(); /* home to display line */
xf = findX();
tvxy(xf,tvy); /* reset cursor to current position */
}
ChangeLoc(curlin,col_pos(curchr,0));
return 1;
}
// ====================>>> vTextEditor::retypeCurlin <<<======================
void vTextEditor::retypeCurlin(int eraseLine)
{
if (!state.echof)
return;
if (eraseLine)
tvelin();
tvxy(1,tvy);
tvtyln(curlin,GLine(curlin), curchr == GLine(curlin));
tvhdln(); // home to display line
int xf = findX();
tvxy(xf,tvy); // reset cursor to current position
}
// ====================>>> vTextEditor::charRight <<<======================
int vTextEditor::charRight(long cnt, int clear_mark)
{ // charRight: move cursor right cnt characters
// newlines count as one character
long change;
int i, rv;
if (lastLineBF() < 1)
return 0;
if (cnt == 0)
return 1;
checkIfScrolled();
if (clear_mark)
ClearMarkRange(); /* no range now */
wasColCmd = 0;
rv = 1; /* assume success */
change = 0; /* no change yet */
if (cnt > 0) /* moving right */
{
for (i = 1 ; i <= cnt ; ++i)
{
if (IsEndLine(GCh(curchr))) /* at end of line */
{
if (curlin >= lastLine)
{
rv = 0;
break; /* don't go beyond end! */
}
++curlin;
++change; /* we've gone down one line */
curchr = GLine(curlin);
}
else
++curchr;
}
}
else if (cnt < 0) /* moving left */
{
cnt = (-cnt); /* fix this */
for (i = 1 ; i <= cnt ; ++i)
{
if (curchr == GLine(curlin)) /* at front */
{
if (curlin > 1) /* can only go up on >= line 2 */
{
--curlin;
--change;
for (curchr = GLine(curlin) ;
!IsEndLine(GCh(curchr)) ;
++curchr)
; /* bump curchr to end of the line */
}
else
{
rv = 0;
break; /* don't go beyond front! */
}
}
else
--curchr;
}
}
if (change != 0) /* don't do unnecessary change */
update(change);
if (clear_mark)
ChangeLoc(curlin,col_pos(curchr,0));
tvhdln();
return rv;
}
// #########################################################################
//
// line methods
//
// #########################################################################
// ===================>>> vTextEditor::lineBeginning <<<====================
void vTextEditor::lineBeginning()
{ /* lineBeginning - move cursor to beginning of current line */
int xf;
if (lastLineBF() < 1)
return;
ClearMarkRange(); /* no range now */
wasColCmd = 0;
curchr = GLine(curlin); /* point to current character */
xf = findX(); /* this line needed to make the next */
/* call eval order independent, if you wondered */
tvxy(xf,tvy); /* and move cursor */
ChangeLoc(curlin,col_pos(curchr,0));
}
// =====================>>> vTextEditor::lineDelete <<<=====================
void vTextEditor::lineDelete(long cnt, bool doUndo)
{ // lineDelete - delete cnt lines
int ityp;
long line_1, last_line; /* range of lines to kill */
long istrt;
if (state.readOnly || lastLineBF() < 1)
return;
ClearMarkRange(); /* no range now */
wasColCmd = 0;
if (cnt == 0)
return;
if (cnt < 0) /* negative kill */
{
cnt = minl(-cnt,curlin-1); /* all upwards? */
lineDownBeg(-cnt,0); /* go up that far */
}
if (doUndo)
{
if (!SaveKillLine(cnt))
{
//@@ abort if can't save all. no-op for now.
}
}
if (cnt != 0)
{
range(cnt,&line_1,&last_line); /* calculate the line numbers to kill */
curlin = line_1; /* remember new current line */
++state.changes; /* count changes */
lastLine = deleteLinesBF(line_1,last_line); /* delete the lines from the buffer(s) */
if (lastLine < curlin)
curlin = lastLine; /* don't go past end */
curchr = GLine(curlin); /* remember new current character */
if (cnt >= 0 && curlin+(GetRows() - tvdlin) <= lastLine &&
tvdlin < GetRows()) /* killing down */
{
tvxy(1,tvy); /* get to start of line */
ityp = (int) minl((long)(GetRows()-tvdlin+1),lastLine - curlin + 1);
tvescr(); /* erase the screen */
istrt = curlin;
type_lines(istrt, ityp);
tvhdln(); /* home to display line */
}
else
Verify(); /* kill up, retype screen */
}
ChangeLoc(curlin,col_pos(curchr,0));
}
// =====================>>> vTextEditor::lineDeleteFront <<<===============
int vTextEditor::lineDeleteFront(bool doUndo)
{ // lineDeleteFront - delete from cursor to beginning of line
int chrs;
if (state.readOnly || lastLineBF() < 1)
return 0;
ClearMarkRange(); /* no range now */
wasColCmd = 0;
chrs = curchr - GLine(curlin); /* how much to delete */
if (chrs > 0)
return charDelete((long)(-chrs),doUndo); // won't cause a combine, so don't worry
return 1;
}
// =====================>>> vTextEditor::lineDeleteToEnd <<<===============
int vTextEditor::lineDeleteToEnd(bool doUndo)
{ // lineDeleteToEnd:
// kill the rest of the line, not including cursor and endLine
int chrs;
BUFFPTR i;
if (state.readOnly || lastLineBF() < 1)
return 0;
ClearMarkRange(); // no range now
wasColCmd = 0;
chrs = 0;
for (i = curchr; !IsEndLine(GCh(i)) ; ++i)
++chrs; // count how much to delete
if (chrs > 0)
return charDelete((long) chrs, doUndo); // won't cause combine
return 1;
}
// ========================>>> vTextEditor::lineDown <<<====================
int vTextEditor::lineDown(long cnt, int clrRange)
{ /* lineDown - move down in column */
int curcol,l,oldef,needns,ic,ix,lim,rv;
if (lastLineBF() < 1)
return 0;
if (clrRange)
ClearMarkRange(); /* no range now */
oldef = state.echof;
needns = 0;
if (leftmg > 1) /* handle right virtual screen different */
{
needns = 1;
}
if (wasColCmd) // going up/down columnwise
curcol = oldcol;
else
oldcol = curcol = col_pos(curchr,1); /* calculate the current column */
rv = lineDownBeg(cnt,0,clrRange); /* go down given lines, update screen */
state.echof = 0;
if (curlin >= 1 && curlin <= lastLine && curcol > 1) /* not at ends? */
{
l = lineLenBF(curlin);
lim = mint(curcol-1,l);
for (ix = 0, ic = col_pos(curchr,1) ; ix < lim && ic < curcol ;
++ix, ic = col_pos(curchr,1))
;
charRight((long)ix,clrRange);
}
state.echof = oldef;
if (needns) /* needed new screen */
{
Verify();
}
else
tvhdln();
ChangeLoc(curlin,col_pos(curchr,0));
wasColCmd = 1;
return rv;
}
// ====================>>> vTextEditor::lineDownBeg <<<=====================
int vTextEditor::lineDownBeg(long cnt, int notify, int clrRange)
{ /* lineDownBeg - move dot down cnt lines */
long oldlin,change;
if (lastLineBF() < 1)
return 0;
if (clrRange)
ClearMarkRange(); /* no range now */
wasColCmd = 0;
if (curlin == lastLine && cnt > 0) /* down from last line? */
{
lineEnd();
return 0; /* make loops fail */
}
oldlin = curlin; /* remember where we started from */
if (curlin + cnt < 1)
curlin = 1;
else if (curlin + cnt > lastLine)
curlin = lastLine;
else
curlin = curlin + cnt;
curchr = GLine(curlin); /* point to the current character */
change = curlin-oldlin; /* calculate how many lines changed */
update(change); /* update the screen */
if (notify)
ChangeLoc(curlin,col_pos(curchr,0));
return change != 0;
}
// =====================>>> vTextEditor::lineEnd <<<========================
void vTextEditor::lineEnd(int clrRange)
{ // lineEnd - move cursor to end of the line
int knt;
BUFFPTR i;
if (lastLineBF() < 1)
return;
if (clrRange)
ClearMarkRange(); /* no range now */
wasColCmd = 0;
knt = 0;
for (i = curchr ; !IsEndLine(GCh(i)) ; ++i) /* find end of line */
++knt;
charRight((long)knt,clrRange); /* move to end of line */
ChangeLoc(curlin,col_pos(curchr,0));
}
// ======================>>> vTextEditor::lineOpen <<<======================
int vTextEditor::lineOpen(long cnt)
{ // lineOpen - open a new line
int i;
long pcnt;
if (state.readOnly || lastLineBF() < 1)
return 0;
undoOff("Undo");
pcnt = cnt >= 0 ? cnt : (-cnt); /* only allow positive opens */
for (i=1; i<=pcnt; ++i)
{
if (!charInsert('\n')) /* insert right number of newlines */
return 0;
}
lineDownBeg(-pcnt,0); /* and goto beginning of the opened line */
lineEnd();
return 1;
}
// ======================>>> vTextEditor::lineGoto <<<======================
int vTextEditor::lineGoto(long cnt)
{ // lineGoto: move cursor to line cnt
if (cnt < 0 || lastLineBF() < 1)
return 0;
undoOff("Undo");
ClearMarkRange(); /* no range now */
wasColCmd = 0;
curlin = maxl(minl(cnt,lastLine),(long) 1); /* move to lines */
curchr = GLine(curlin); /* point to the current character */
Verify();
return 1;
}
// =====================>>> vTextEditor::lineAutoFill <<<===================
void vTextEditor::lineAutoFill(void)
{
BUFFPTR bx, startx;
int lines, old_ef, old_y, ityp, go_right, cp, chr;
long start_line, istrt;
if (state.readOnly || lastLineBF() < 1)
return;
if (gState.wraplm < 1)
return; /* no auto-wrap going on */
/* 1. Check if current line needs to be wrapped */
startx = GLine(curlin); /* first char of line */
bx = lastBX(curlin); /* index of last char of line */
if (startx == bx)
return; /* blank line, can't need tidy */
if (col_pos(bx,0) <= gState.wraplm) /* is last char beyond limit? */
return; /* line doesn't need tidy */
/* 2. If it does, figure out where the cursor is. */
go_right = curchr - startx; /* where CURRENT char is */
start_line = curlin;
old_y = tvdlin; /* original line */
/* 3. Echo off. */
old_ef = state.echof;
state.echof = 0;
/* 4. Tidy down to PP or blank line or line beginning with white */
for (lines = 1 ; lines < 50 && curlin <= lastLine ;
++lines )
{
lineFill((long) 1); /* fix current line */
chr = GCh(curchr);
if (chr == '.' || chr == ' ' || chr == '\t' || IsEndLine(chr))
break;
}
/* 5. Restore echo */
state.echof = old_ef;
/* 6. Return to current line, making sure it stays where it was
on the screen (or shift by one if it gets moved),
and repaint the rest of the screen.
*/
curlin = start_line;
curchr = GLine(curlin);
tvdlin = old_y;
if (state.echof)
{
tvxy(1,old_y);
istrt = curlin;
ityp =(int) minl((long)(GetRows()-tvdlin+1),
lastLine - curlin + 1);
type_lines(istrt, ityp);
}
tvhdln(); /* home to display line */
for ( ; ; )
{
bx = lastBX(curlin); /* see if still on this line */
cp = col_pos(bx,0);
if (go_right <= cp) /* on this line */
{
curchr = GLine(curlin) + go_right;
break;
}
go_right -= cp; /* next line */
if (!lineDownBeg((long) 1,0))
break;
}
tvhdln(); /* home to display line */
ChangeLoc(curlin,col_pos(curchr,0));
}
// =====================>>> vTextEditor::lineFill <<<=======================
int vTextEditor::lineFill(long count)
{ // lineFill - fill lines to current margin
char curline[20]; /* first part of current line */
int oldef, i ;
BUFFPTR linbeg, old_cc;
int retval;
if (state.readOnly || lastLineBF() < 1 || gState.wraplm < 1)
return 0; /* no auto-wrap going on */
undoOff("Undo");
ClearMarkRange(); /* no range now */
wasColCmd = 0;
retval = 1;
oldef = state.echof;
if (count > 1)
state.echof = 0;
if (gState.wraplm <= 1 || curlin > lastLine)
goto l900; /* work only if wrap limit turned on */
for (i = 1 ; i <= count ; ++i)
{
lineBeginning(); /* start at beginning of line */
if (curlin >= lastLine)
goto l900;
// don't fill leading space, cr, period, tab,
// latex keyword, or some HTML
getCurLineFront(curline,curlin); /* current line */
if (!(*curline) || isSpecial(curline))
{
lineDownBeg((long) 1,0);
continue; /* skip special lines */
}
while (curlin < lastLine)
{
if (IsEndLine(GCh(curchr)))
{
if (tvx+leftmg-1 < gState.wraplm) /* combine lines! */
{
/* pt to first char of next line */
linbeg = GLine(curlin+1);
getCurLineFront(curline,curlin+1); // get the front
if (IsEndLine(GCh(linbeg)) || isSpecial(curline))
{
lineDownBeg((long) 1);
break; /* no more combining */
}
if (! Fill1(1,32))
goto l990;
/* fall thru to test for wrap */
}
else
{
lineDownBeg((long) 1); /* no more combining on line */
break;
}
}
old_cc = curchr;
wordRight((long) 1);
if (tvx+leftmg-1 > gState.wraplm)
{
if (tvx+leftmg-2 == gState.wraplm && IsEndLine(GCh(curchr)) )
{
lineDownBeg((long) 1);
break;
}
else if ((tvx+leftmg-2 == gState.wraplm ||
tvx+leftmg-3 == gState.wraplm) &&
(GCh(curchr-1) == ' ' || GCh(curchr-1) == '\t'))
{
if (!Fill1(-1,'\n'))
goto l990;
}
else if (GCh(old_cc-1) == ' ' ||
GCh(old_cc-1) == '\t')
{
curchr = old_cc;
if (!Fill1(-1,'\n'))
goto l990;
}
else if (GCh(old_cc-2) == ' ' ||
GCh(old_cc-2) == '\t')
{
curchr = old_cc-1;
if (!Fill1(-1,'\n'))
goto l990;
}
else if (IsEndLine(GCh(curchr)) )
{
lineDownBeg((long) 1);
break;
}
else if (GCh(curchr-1) == ' ' ||
GCh(curchr-1) == '\t')
{
if (!Fill1(-1,'\n'))
goto l990;
}
else
{
for (wordRight((long) -1) ; /* go back a word, then break */
(GCh(curchr-1) != ' ' && GCh(curchr-1) != '\t'
&& !IsEndLine(GCh(curchr-1)) ) ;
wordRight((long) -1))
{
/* this line can't be filled - stop now */
if (curchr == GLine(curlin))
goto l990;
}
charInsert('\n',false);
}
break;
}
} /* end of for (;;) */
} /*# end of the for (count) */
l900:
state.echof = oldef;
if (oldef && count > 1)
Verify();
ChangeLoc(curlin,col_pos(curchr,0));
return retval;
l990: /* failure return */
retval = 0;
goto l900;
}
//===========================>>> vTextEditor::isSpecial <<<=====================
bool vTextEditor::isSpecial(char* curline)
{
static char* special[] = /* special keywords */
{ /* that shouldn't cause a wrap */
"<a ",
"<applet",
"<area",
"<base",
"<block",
"<body",
"<br",
"<center",
"<cite",
"<code",
"<col",
"<dd",
"<dir",
"<div",
"<dl",
"<dt",
"<form",
"<frame",
"<h1",
"<h2",
"<h3",
"<h4",
"<h5",
"<h6",
"<head",
"<hr",
"<html",
"<iframe",
"<img",
"<input",
"<li",
"<map",
"<menu",
"<meta",
"<no",
"<obj",
"<ol",
"<opt",
"<p>",
"<p ",
"<pre",
"<samp",
"<table",
"<tbody",
"<td",
"<th",
"<title",
"<tr",
"<ul",
""
};
if (*curline == ' ' || *curline == '\t' || *curline == '.')
return true;
if (*curline == '<') // html?
for (int iy = 0 ; *special[iy] ; ++iy) /* search keyword list */
{
if (strstr(curline,special[iy]) == curline)
return true;
}
return false;
}
//===========================>>> vTextEditor::formatC <<<=====================
int vTextEditor::formatC(long count)
{
/* format C code according to Bruce's style */
int oldef, i, ij, ik, spaces, tabs, prev_key;
long prev_lnum;
char prev_line[40];
char prev_prev_line[40];
oldef = state.echof;
if ( curlin >= _nextLine-1) /* can't do last line */
return 0;
if (count > 1)
state.echof = 0;
for (i = 1 ; i <= count ; ++i, lineDownBeg((long)1))
{
if ((prev_lnum = get_prev(prev_line,curlin)) <= 0) /* get the previous line */
continue; /* handle 1st line of file! */
(void) get_prev(prev_prev_line,prev_lnum); /* and the line before it */
if (*prev_line != ' ' && *prev_line != '\t') /* funny line */
{
lineDownBeg((long)1);
break; /* give up */
}
/* count previous tabs/spaces */
tabs = spaces = 0;
for (ij = 0 ; ij < 38 ; ++ij)
{
if (prev_line[ij] == '\t')
{
spaces = 0; /* skip embedded spaces */
++tabs;
}
else if (prev_line[ij] == ' ')
++spaces;
else
break; /* break on first non tab non blank */
}
if (tabs == 0 && spaces <= 2)
{
lineDownBeg((long) 1);
break; /* give up on function heads */
}
/* now process current line */
lineBeginning(); /* start at beginning of line */
if (curlin >= _nextLine-1)
break; /* done */
/* don't fix blank lines */
if (GCh(curchr) != ' ' && GCh(curchr) != '\t' )
{
lineDownBeg((long) 1);
break; /* break on line I don't know about */
}
while (GCh(curchr) == ' ' || GCh(curchr) == '\t')
charDelete((long)1,false); /* delete them */
if (GCh(curchr) == '\n')
continue; /* skip blank lines */
/* determine spacing of current line based on prev line */
prev_key = 0;
if (is_key_word(&prev_line[ij],1)) /* a kew word! */
{
prev_key = 1; /* last line was key */
if (GCh(curchr) == '{')
spaces += 2;
else
spaces += 4;
}
else if (prev_line[ij] == '{')
{
spaces += 2;
}
else if (prev_line[ij] == '}')
{
spaces -= 2;
}
if (has_key((char*)curchr, "else"))
{
if (prev_line[ij] != '}')
spaces -= 4;
}
else if (has_key((char *)curchr, "case") ||
has_key((char*)curchr, "default:"))
{
if (prev_line[ij] != '{')
spaces -= 4;
}
else if (!prev_key && prev_line[ij] != '{' &&
prev_line[ij] != '}' && is_key_word(prev_prev_line,0))
spaces -= 4;
/* don't else next one, works with last else if */
if (GCh(curchr) == '}') /* } */
spaces -= 2;
/* fix tab/space counts */
if (spaces < 0)
{
if (--tabs < 0)
tabs = 0;
spaces = 8 + spaces;
}
while (spaces >= 8)
{
++tabs;
spaces -= 8;
}
for (ik = 0 ; ik < tabs ; ++ik)
charInsert('\t',false);
for (ik = 0 ; ik < spaces ; ++ik)
charInsert(' ',false);
} /* end of main for loop */
state.echof = oldef;
if (oldef && count > 1)
Verify();
return 1;
}
// =============================>>> vTextEditor::has_key <<<=======================
int vTextEditor::has_key(char *buff_ptr, char *key)
{
while (*key)
{
if (*buff_ptr++ != *key++)
return 0;
}
return 1; /* buff_ptr matches key up to key's EOS */
}
// ==========================>>> vTextEditor::is_key_word <<<======================
int vTextEditor::is_key_word(char *bf, int case_def)
{
//char *strstr();
while (*bf == ' ' || *bf == '\t') /* skip leading white */
++bf;
if (strstr(bf,"if(") == bf)
return 1;
if (strstr(bf,"if (") == bf)
return 1;
if (strstr(bf,"for(") == bf)
return 1;
if (strstr(bf,"for (") == bf)
return 1;
if (strstr(bf,"while(") == bf)
return 1;
if (strstr(bf,"while (") == bf)
return 1;
if (strstr(bf,"else") == bf)
return 1;
if (strstr(bf,"typedef ") == bf)
return 1;
if (strstr(bf,"struct ") == bf)
return 1;
if (strstr(bf,"switch(") == bf)
return 1;
if (strstr(bf,"switch (") == bf)
return 1;
if (case_def && strstr(bf,"case ") == bf)
return 1;
if (case_def && strstr(bf,"default:") == bf)
return 1;
return 0;
}
//=============================>>> vTextEditor::get_prev <<<======================
long vTextEditor::get_prev(char *prev_buff, long start)
{ // Get previous non-blank line in text buffer (BEW: 10/1/98)
long pline;
int ix, blank_line;
BUFFPTR bi;
*prev_buff = 0; /* empty line for sure */
pline = start;
GP_TOP:
--pline; /* previous line */
if (pline <= 1)
return 0; /* there is no previous line */
bi = GLine(pline); /* first char of buffer */
blank_line = 1;
for (ix = 0 ; ix < 38 ; ++ix)
{
if (GCh(bi+ix) == '\n')
break;
prev_buff[ix] = GCh(bi+ix); /* copy the char */
if (prev_buff[ix] != ' ' || prev_buff[ix] != '\t')
blank_line = 0; /* not a blank line */
}
prev_buff[ix] = 0; /* terminate the string */
if (blank_line || prev_buff[0] == '#')
goto GP_TOP; /* find previous non-blank line */
return pline;
}
// ########################################################################
//
// Misc. methods
//
// ########################################################################
// ======================>>> vTextEditor::getSelection <<<=================
int vTextEditor::getSelection(char* sel, int max)
{
// Fetch the highlighted text
long sl = SelectionLen(); // length of selection
if (sl < 0 || sl >= max-1)
return 0;
CopySelection(sel, max, 0); // copy the selection
return 1;
}
// ======================>>> vTextEditor::EditCut <<<=================
int vTextEditor::EditCut()
{
// Cut text to clipboard
long sl = SelectionLen(); // length of selection
if (sl < 0)
return 0;
char* cut = new char[sl+1]; // space for a copy
CopySelection(cut, sl+1, 0); // copy the selection
theApp->ClipboardSetText(cut); // set the clipboard
delete [] cut; // free the space
RemoveMarkRange(); // finally, cut it
return 1;
}
// ======================>>> vTextEditor::EditCopy <<<=================
int vTextEditor::EditCopy()
{
// Cut text to clipboard
long sl = SelectionLen(); // length of selection
if (sl < 0)
return 0;
char* cut = new char[sl+1]; // space for a copy
CopySelection(cut, sl+1, 0); // copy the selection
theApp->ClipboardSetText(cut); // set the clipboard
delete [] cut; // free the space
return 1;
}
// ======================>>> vTextEditor::EditPaste <<<=================
int vTextEditor::EditPaste()
{
char* paste = theApp->ClipboardGetText(); // get the text
if (!paste || !*paste)
return 0;
long pasteLen = strlen(paste);
long orig_line = curlin;
int old_echo = state.echof;
if (pasteLen > 80)
state.echof = 0;
while (*paste) // dumb paste - char at a time insert
{
if (*paste != '\r') // ignore \r on both X and Windows
{
if (*paste == '\n')
charInsert((int)'\r',false); // this works for both X and Windows
else
charInsert((int)*paste,false);
}
++paste;
}
if (state.echof == 0)
{
state.echof = old_echo;
// Verify();
}
if (curlin != orig_line) /* refresh screen if moved */
Verify();
else
tvhdln(); /* just fix the cursor */
return 1;
}
// ======================>>> vTextEditor::SelectionLen <<<=================
int vTextEditor::SelectionLen()
{
long lines;
int len;
MARK_RANGE r_mark;
if (mark.beg_lin != 0) // we have some text to count
{
r_mark = mark; // use a copy
// count the range
lines = r_mark.end_lin - r_mark.beg_lin + 1;
if (lines == 1) /* all on current line */
{
if (r_mark.end_chr) /* find type range */
{
len = r_mark.end_chr - r_mark.beg_chr;
}
else
{
for (len = 0 ; GCh(r_mark.beg_chr + len) != 0 ; ++len)
;
}
return len;
}
else // more than one line
{
// Count 1st line
for (len = 0 ; GCh(r_mark.beg_chr + len) != 0 ; ++len)
;
// Count middle lines
for (long mid = r_mark.beg_lin + 1 ;
mid < r_mark.end_lin ; ++mid)
{
int linlen;
for (linlen = 0 ; GCh(GLine(mid) + linlen ) != 0 ;
++linlen)
; // count len of this line
len += linlen; // add to total
}
// Count last line - count up to r_mark.end_chr, or
// to the end of the line (some places we call end_chr
// a big number that really isn't the end of the line
int lastlen;
for (lastlen = 0 ;
GLine(r_mark.end_lin) + lastlen < r_mark.end_chr
&& GCh(GLine(r_mark.end_lin) + lastlen) != 0 ;
++lastlen)
;
len += lastlen;
return len;
}
}
return 0;
}
// ======================>>> vTextEditor::CopySelection <<<=================
int vTextEditor::CopySelection(char* buff, int max, int unmark)
{
long cnt;
int ix;
char *to;
MARK_RANGE r_mark;
to = buff;
if (mark.beg_lin != 0) /* we have some text to kill */
{
if (max < SelectionLen() + 1) // make sure fits
return 0;
r_mark = mark; /* make copy to avoid recursion */
/* now safe to kill off range */
if (unmark)
{
mark.beg_lin = mark.end_lin =
mark.beg_col = mark.end_col = 0;
mark.beg_chr = mark.end_chr = 0;
theApp->SetValueAll(M_Cut, 0, Sensitive); // zap menu
theApp->SetValueAll(M_Copy, 0, Sensitive);
}
/* clean up screen */
if (curlin != r_mark.beg_lin ||
r_mark.end_lin - r_mark.beg_lin > 0)
{
Verify();
}
else
{
tvxy(1,tvdlin); /* fix it up */
type_lines(curlin, 1);
tvhdln();
}
/* copy the range */
cnt = r_mark.end_lin - r_mark.beg_lin + 1;
if (cnt == 1) /* all on current line */
{
if (r_mark.end_chr) /* find type range */
{
cnt = r_mark.end_chr - r_mark.beg_chr;
}
else
{
for (cnt = 0 ; GCh(r_mark.beg_chr + cnt) != 0 && cnt < (max - 1) ;
++cnt)
;
}
for (ix = 0 ; ix < cnt && ix < (max - 1) ; ++ix) /* copy directly */
{
*(to+ix) = GCh(r_mark.beg_chr+ix);
if (*(to+ix) == 0)
break;
}
*(to+ix) = 0;
}
else /* more than one line */
{
// Copy 1st line
for (long len1 = 0 ; GCh(r_mark.beg_chr + len1) != 0 ; ++len1)
*to++ = GCh(r_mark.beg_chr + len1);
// Copy middle lines
for (long mid = r_mark.beg_lin + 1 ;
mid < r_mark.end_lin ; ++mid)
{
for (int linlen = 0 ; GCh(GLine(mid) + linlen) != 0 ;
++linlen)
*to++ = GCh(GLine(mid) + linlen);
}
// Count last line - count up to r_mark.end_chr, or
// to the end of the line (some places we call end_chr
// a big number that really isn't the end of the line
for (int lastlen = 0 ;
GLine(r_mark.end_lin) + lastlen < r_mark.end_chr
&& GCh(GLine(r_mark.end_lin) + lastlen) != 0 ;
++lastlen)
*to++ = GCh(GLine(r_mark.end_lin) + lastlen);
*to++ = 0;
}
return 1;
}
return 0;
}
// ======================>>> vTextEditor::ClearMarkRange <<<================
void vTextEditor::ClearMarkRange(void)
{
checkIfScrolled(); // can check for scroll here
/* clear the range variables */
if (mark.beg_lin != 0)
{
mark.beg_lin = mark.end_lin =
mark.beg_col = mark.end_col = 0;
mark.beg_chr = mark.end_chr = 0;
Verify();
}
else /* just be sure we stay in phase */
{
mark.beg_lin = mark.end_lin =
mark.beg_col = mark.end_col = 0;
mark.beg_chr = mark.end_chr = 0;
}
theApp->SetValueAll(M_Cut, 0, Sensitive);
theApp->SetValueAll(M_Copy, 0, Sensitive);
}
// =====================>>> vTextEditor::RemoveMarkRange <<<================
int vTextEditor::RemoveMarkRange(bool doUndo)
{
/* delete the text in the marked range
return
0 : no text deleted
1 : text deleted
*/
static int inHere = 0; // don't let it be recursive
int retval = 0;
long cnt, orig_line;
MARK_RANGE r_mark;
BUFFPTR orig_chr;
if (inHere)
return 0;
if (mark.beg_lin == 0) /* we have some text to kill */
return 0;
inHere = 1;
r_mark = mark; /* make copy to avoid recursion */
/* now safe to kill off range */
mark.beg_lin = mark.end_lin =
mark.beg_col = mark.end_col = 0;
mark.beg_chr = mark.end_chr = 0;
/* kill off range */
orig_line = curlin;
orig_chr = curchr;
curlin = r_mark.beg_lin;
curchr = r_mark.beg_chr;
cnt = r_mark.end_lin - r_mark.beg_lin + 1;
if (!state.readOnly && cnt == 1) /* all on current line */
{
if (r_mark.end_chr) /* find type range */
{
long dcnt = r_mark.end_chr - r_mark.beg_chr;
charDelete(dcnt,doUndo);
}
else /* ^M type range */
{
if (curchr == GLine(curlin))
{
lineDelete(cnt,doUndo); /* kill whole current line */
}
else
{
lineDeleteToEnd(doUndo); // past line beg, just kill rest
}
}
retval = 1;
}
else if (!state.readOnly) // more than one line
{
int oldef = state.echof;
state.echof = 0; // turn off echo
if (doUndo)
{
bool partialLine;
partialLine = curchr != GLine(curlin); // partial first line
if (!IsEndLine(GCh(r_mark.end_chr)))
partialLine = true;
if (!SaveKillLine(cnt,partialLine))
{
}
}
lineDeleteToEnd(false); // past line beg, just kill rest
lineDownBeg((long)1,0); /* and the end of line */
--cnt; /* one less */
lineDelete(cnt - 1, false); // kill off all but last line
if (!r_mark.end_chr) /* kill whole last line */
{
lineDelete((long)1,false);
}
else
{
curchr = r_mark.end_chr; /* get last char */
if (IsEndLine(GCh(curchr)))
lineDelete((long)1,false);
else
lineDeleteFront(false); // kill last line
}
charDelete((long)-1,false); // and the lead cr
retval = 1;
state.echof = oldef;
if (doUndo)
undoOn("Undo Delete");
}
if (curlin != orig_line) /* refresh screen if moved */
Verify();
else
tvhdln(); /* just fix the cursor */
ChangeLoc(curlin,col_pos(curchr,0));
inHere = 0;
return retval;
}
// ===========================>>> BalMatch <<<==============================
int vTextEditor::BalMatch(long val)
{
/* Find balance of )]} or [{( */
int orig_line, dir, nest, old_ef, echo_off;
long limit, ix;
BUFFPTR orig_chr;
char start_c, match;
if (lastLineBF() < 1)
return 0;
ClearMarkRange(); /* no range now */
wasColCmd = 0;
old_ef = state.echof;
echo_off = 0; /* haven't turned off echo */
limit = ((long) val) * 4000L; /* limit for search */
orig_chr = curchr;
orig_line = curlin;
start_c = GCh(curchr); /* original char */
switch (start_c)
{
case '(':
match = ')';
dir = 1;
break;
case '{':
match = '}';
dir = 1;
break;
case '[':
match = ']';
dir = 1;
break;
case ')':
match = '(';
dir = -1;
break;
case '}':
match = '{';
dir = -1;
break;
case ']':
match = '[';
dir = -1;
break;
default:
return 0; /* no op if not a paren thing */
}
for (ix = 1, nest = 0 ; ix < limit ; ++ix)
{
charRight((long)dir,1); /* go right */
if (!echo_off && (curlin != orig_line))
{
state.echof = 0;
echo_off = 1; /* disable echoing */
StatusMessage("Scanning for matching paren");
}
if (GCh(curchr) == start_c)
++nest; /* bump nest */
else if (GCh(curchr) == match)
{
if (nest)
--nest; /* undo nest */
else
{
if (echo_off) /* we've turned echo off */
{
if ((state.echof == old_ef)) /* update if it was on */
Verify();
}
return 1; /* found the matching thing */
}
}
}
/* fall through ==> died */
curchr = orig_chr;
curlin = orig_line;
state.echof = old_ef;
newscr();
ChangeLoc(curlin,col_pos(curchr,0));
return 0;
}
// #########################################################################
//
// Private helper methods
//
// #########################################################################
//========================>>> vTextEditor::reallocLines <<<======================
int vTextEditor::reallocLines()
{
BUFFPTR* oldLines;
oldLines = _lines; // remember old lines
_lines = new char*[_maxLines + lineListSize];
if (!_lines)
{
_lines = oldLines; // failed to get more lines
return 0;
}
long lx;
for (lx = 0 ; lx < _maxLines ; ++lx) // copy old lines
_lines[lx] = oldLines[lx];
_maxLines = _maxLines + lineListSize - 1; // safety factor
for (; lx < _maxLines ; ++lx) // null new lines
_lines[lx] = 0;
delete [] oldLines; // free the old lines
return 1;
}
//========================>>> vTextEditor::col_pos <<<======================
int vTextEditor::col_pos(BUFFPTR chr_pos, int do_shift)
{ /* col_pos - find the x position of the character chr_pos on current line
handles spacing for tabs, control characters etc */
BUFFPTR i;
int pos;
pos = 1;
for (i = GLine(curlin) + 1 ; i <= chr_pos ; ++i)
{
if (GCh(i-1) < ' ' && GCh(i-1) > 0) /* cur pos depends on last chr */
{
if (GCh(i-1) == '\t' && gState.tabspc > 0) /* handle tabs */
{
for (++pos ; ((pos-1) % gState.tabspc) != 0; ++pos)
;
}
else /* control characters (echoed as ^X) */
pos += 2; /* 2 spaces for other control character */
}
else /* normal character */
++pos;
}
while (do_shift)
{
if (pos < leftmg) /* won't fit on screen */
{
leftmg -= 16; /* shift left */
}
else if (pos >= GetCols() + leftmg)
{
leftmg += 16;
}
else
break;
}
if (do_shift)
return pos - leftmg + 1;
else
return pos;
}
// ===========================>>> vTextEditor::mint <<<=====================
int vTextEditor::mint(int v1, int v2)
{
return (v1 > v2 ? v2 : v1);
}
// ============================>>> vTextEditor::maxt <<<====================
int vTextEditor::maxt(int v1, int v2)
{
return (v1 > v2 ? v1 : v2);
}
// ===========================>>> vTextEditor::minl <<<=====================
long vTextEditor::minl(long v1,long v2)
{
return (v1 > v2 ? v2 : v1);
}
// ===========================>>> vTextEditor::maxl <<<=====================
long vTextEditor::maxl(long v1, long v2)
{
return (v1 > v2 ? v1 : v2);
}
// ===========================>>> vTextEditor::clower <<<===================
int vTextEditor::clower(int ch)
{
return ((ch >='A' && ch<='Z') ? ch + ' ' : ch);
}
// ==========================>>> vTextEditor::cupper <<<===================
int vTextEditor::cupper(int ch)
{
return ((ch >= 'a' && ch <= 'z') ? ch - ' ' : ch);
}
// ===================>>> vTextEditor::line_can_fit <<<=====================
int vTextEditor::line_can_fit(long l)
{
/* if line can't fit onto screen width, we need to update deleted
characters a bit differently.
*/
BUFFPTR to;
int len;
for (to = GLine(l) + 1, len = 0 ; !IsEndLine(GCh(to)) ; ++to)
{
if (GCh(to) < ' ')
return 0; /* ctrl chars mess it up, so false */
++len;
}
return (len < GetCols());
}
// =======================>>> vTextEditor::FindDispLine <<<=================
void vTextEditor::FindDispLine(long& ibeg, int& cnt)
{ /* FindDispLine - find the display line
known: current line, calculate where it would go on the screen */
if (curlin <= dsplin)
{ /* it is in first part of the display */
ibeg = 1;
cnt = (int) minl((long)GetRows(),(long)(lastLine));
tvdlin = (int)curlin; /* update the display line */
}
else if (lastLine-curlin < GetRows()-dsplin)
{ /* at bottom of display */
ibeg = maxl((long)1,(long)(lastLine - GetRows() + 1));
cnt = mint(GetRows(),(int) lastLine);
tvdlin = minl(curlin,(long)(GetRows()-(lastLine-curlin+1)+1));
}
else /* normal case: in middle */
{
ibeg = maxl((long) 1,(long)(curlin-dsplin+1));
cnt = minl((long)GetRows(),(long)(lastLine-(ibeg)+1));
tvdlin = dsplin;
}
}
// ======================>>> vTextEditor::findX <<<========================
int vTextEditor::findX(void)
{ /* findX - find the x position of the current character
handles spacing for tabs, control characters etc */
BUFFPTR i;
int pos, need_newscr;
if (curlin < 1 || GetCols() <= 1 || lastLineBF() < 1)
return 1;
need_newscr = 0;
pos = 1;
for (i = GLine(curlin)+1 ; i <= curchr ; ++i)
{
if (GCh(i-1) < ' ' && GCh(i-1) > 0) /* cur pos depends on last chr */
{
if (GCh(i-1) == '\t' && gState.tabspc > 0) /* handle tabs */
{
for (++pos ; ((pos-1) % gState.tabspc) != 0; ++pos)
;
}
else /* control characters (echoed as ^X) */
pos += 2; /* 2 spaces for other control character */
}
else /* normal character */
++pos;
}
for (;;)
{
if (pos < leftmg) /* won't fit on screen */
{
leftmg -= 16; /* shift left */
need_newscr = 1;
if (GetCols() <= 16)
break;
}
else if (pos >= GetCols()+leftmg)
{
leftmg += 16;
need_newscr = 1;
if (GetCols() <= 16)
break;
}
else
break;
}
if (need_newscr)
{
Verify();
}
return (pos-leftmg+1);
}
// ===================>>> vTextEditor::getCurLineFront <<<=======================
void vTextEditor::getCurLineFront(char* buff, long start)
{
int ix;
BUFFPTR bi;
buff[0] = 0; /* empty line for sure */
if (lastLineBF() < 1)
return;
bi = GLine(start); /* first char of buffer */
for (ix = 0 ; ix < 18 ; ++ix) /* 18 chars max */
{
if (IsEndLine(GCh(bi+ix)))
break;
buff[ix] = tolower(GCh(bi+ix)); /* copy the char */
}
buff[ix] = 0; /* terminate the string */
}
// ====================>>> vTextEditor::lastBX <<<==========================
BUFFPTR vTextEditor::lastBX(long line)
{
/* return the buff index of the last char of the line */
BUFFPTR bx;
for (bx = GLine(line) ; !IsEndLine(GCh(bx)) ; ++bx)
; /* find last char in line */
return bx;
}
// ====================>>> vTextEditor::Fill1 <<<==========================
int vTextEditor::Fill1(int dir, int val)
{ /* change character dir to val */
int oldwrp;
if (state.readOnly)
return 0;
oldwrp = gState.wraplm;
gState.wraplm = 0;
if (! charDelete((long)dir,false))
goto l900;
if (! charInsert(val,false))
goto l900;
gState.wraplm = oldwrp;
return 1;
l900:
gState.wraplm = oldwrp;
return 0;
}
// ======================>>> vTextEditor::newscr <<<========================
void vTextEditor::newscr(void)
{ /* newscr - retype entire screen, updating cursor position if necessary */
long ibeg;
int cnt;
if (!state.echof)
return;
if (curlin < 1)
{
tvclr();
tvxy(1,1);
return;
}
if (lastLine < GetRows()) /* two kinds of screen rewrite */
tvclr(); /* clear the screen and home */
tvxy(1,1);
dsplin = tvdlin = ddline; /* home to middle */
FindDispLine(ibeg, cnt); /* calculate where it will go */
type_lines(ibeg,cnt); /* type it out */
tvhdln(); /* home to display line */
}
// =====================>>> vTextEditor::range <<<=========================
void vTextEditor::range(long cnt, long *lbeg, long *lend)
{ /* determine a legal line number range given cnt */
if (cnt <= 0)
{
if ((*lbeg = curlin + cnt) < 0)
*lbeg = 1;
*lend = curlin;
if (cnt < 0)
*lend = (*lend)-1;
}
else
{
*lbeg = curlin;
if ((*lend = curlin+cnt-1) > lastLine)
*lend = lastLine;
}
}
// ====================>>> vTextEditor::setScrollBar <<<====================
void vTextEditor::setScrollBar()
{
long shown;
long top;
long last = lastLine;
if (!state.echof)
return;
if (last < 1)
last = 1;
if (lastLine <= (long)GetRows())
shown = 100L;
else
shown = ((long) GetRows() * 100L) / last;
if (shown < 1)
shown = 1;
long cur = (scroll_lin > 0) ? scroll_lin : curlin; // handle scrolling
if (cur >= last)
top = 100L;
else if (cur == 1)
top = 0;
else
top = (cur * 100L) / last;
if (top < 0)
top = 0;
SetVScroll((int) shown, (int) top);
}
// ====================>>> vTextEditor::checkIfScrolled <<<=================
void vTextEditor::checkIfScrolled()
{
// If we are scrolled, we need to restore screen
if (!state.echof)
return;
if (scroll_lin > 0 || setCursor)
{
setCursor = 0;
scroll_lin = -1;
ShowTextCursor();
Verify();
}
}
// ====================>>> vTextEditor::scrollDown <<<======================
void vTextEditor::scrollDown(long delta)
{ /* scroll screen without moving cursor either 1 line or screenful*/
long change;
if (!state.echof)
return;
if (lastLineBF() < 1 || delta == 0)
{
setScrollBar();
return;
}
if (!gState.fixed_scroll && scroll_lin < 0) /* might be on same screen */
{
// this just adjusts the screen without really scrolling
if (delta == 1 && dsplin > 1) /* scroll down */
{
if ((curlin+GetRows()-tvdlin) >= lastLine)
return; // no where to go
tvbotb(1); /* make room */
tvxy(1,GetRows()); /* home to last line */
dsplin = tvdlin = tvdlin - 1; /* change line */
type_lines((long)(curlin+GetRows()-tvdlin),1); /* fix up screen */
tvhdln(); /* home to display line */
setScrollBar();
return;
}
else if (delta == -1 && dsplin < GetRows())
{
if ((curlin-tvdlin) <= 0 )
return;
tvtopb(1); /* make blank lines at top */
dsplin = tvdlin = tvdlin + 1; /* change line */
type_lines((long)(curlin-tvdlin+1),1); /* fill in */
tvhdln(); /* home to display line */
setScrollBar();
return;
}
}
if (scroll_lin < 0) /* initial setup */
{
if (curlin <= dsplin) /* on first screen */
{
scroll_lin = 1; /* assume 1st line */
}
else if (lastLine - curlin < GetRows() - dsplin)
{ /* at bottom of display */
scroll_lin = maxl((long)1,(long)(lastLine - GetRows() + 1));
}
else /* normal case: in middle */
{
scroll_lin = maxl((long)1,(long)(curlin-dsplin+1));
}
}
if (delta < 0 && scroll_lin == 1) /* at top already */
return;
change = delta;
if (change < 0) /* have to scroll screen down */
{
if (change == -1)
{
if (scroll_lin <= 1)
return;
scroll_lin -= 1;
tvtopb(1); /* make blank lines at top */
type_lines(scroll_lin,1); /* fill in */
}
else /* a screenful or so */
{
scroll_lin = maxl((long)1,(long)(scroll_lin + delta));
newScrollScreen(scroll_lin);
}
}
else if (change > 0) /* have to scroll screen up */
{
if ((scroll_lin+GetRows()) > lastLine)
return; // no where to go
if (change == 1)
{
scroll_lin += 1;
tvbotb(1); /* make blank lines at top */
type_lines((long)(scroll_lin+GetRows()-1),1); /* fill in */
}
else /* a screenful or so */
{
scroll_lin = minl(lastLine, (long)(scroll_lin + delta));
newScrollScreen(scroll_lin);
}
}
// now, if the curlin is on the new scrolled screen, we need to
// repaint it so the cursor and highlight area show
if (curlin >= scroll_lin && curlin < (scroll_lin + GetRows()))
{
ShowTextCursor();
int xf = findX();
tvxy(xf, curlin - scroll_lin + 1); // reset cursor to current position
// scroll_lin = -1; // BEW: 3/31/99 removed reset to -1
}
else
HideTextCursor();
setScrollBar();
}
// =================>>> vTextEditor::newScrollScreen <<<====================
void vTextEditor::newScrollScreen(long ibeg)
{ /* newScrollScreen - retype entire screen,
updating cursor position if necessary */
if (!state.echof)
return;
tvclr(); /* clear the screen and home */
type_lines(ibeg,(int)minl(lastLine,(long)GetRows())); /* type it out */
}
// ===================>>> vTextEditor::undoOn <<<=====================
void vTextEditor::undoOn(const char* msg)
{
if (!state.echof)
return;
((vWindow *)_parent)->SetValue(M_UnDo, 1, Sensitive);
if (msg && *msg)
((vWindow *)_parent)->SetString(M_UnDo, (char*) msg);
}
// ===================>>> vTextEditor::undoOff <<<=====================
void vTextEditor::undoOff(const char* msg)
{
undoCurlin = -1; // so can't try to undo...
if (!state.echof)
return;
((vWindow *)_parent)->SetValue(M_UnDo, 0, Sensitive);
if (msg && *msg)
((vWindow *)_parent)->SetString(M_UnDo, (char*) msg);
}
// ===================>>> vTextEditor::SaveKillLine <<<=====================
bool vTextEditor::SaveKillLine(long cnt, bool partialLine)
{
// SaveKillLine - save lines that will be killed
// killFirst and killLast are used with charDelete to note
// if we are killing part of a line that needs to be replaced
// in case of an undo.
char ln[MAX_LINE+2];
// BUFFPTR from;
undoOn("Undo Delete");
// remember state
undoCurcol = col_pos(curchr,1);
undoCurlin = curlin;
undoPartialLine = partialLine;
// copy lines to buffer
int to = 0; // start at beginning of undo buffer
for (long lx = 0 ; lx < cnt && to < MAX_UNDO ; ++lx)
{
int len = getLine(ln,MAX_LINE,curlin+lx);
if (len < 0) // out of lines?
break;
for (int ix = 0 ; ix < len && to < MAX_UNDO ; ++ix)
{
undoBuff[to++] = ln[ix]; // copy to buffer
}
undoBuff[to++] = '\n'; // add end of line
}
undoBuff[to] = 0; // terminate the buffer
if (to >= MAX_UNDO) // oops!
{
// issue warning?
return false;
}
return true; // save succeeded
}
// =======================>>> vTextEditor::undoEdit <<<=======================
int vTextEditor::undoEdit(void)
{ /* unkill the single last line killed */
char ln[MAX_LINE+2]; // temp line
long lines = 0;
if (undoCurlin < 0)
return 0;
long useCurlin = undoCurlin; // keep local copy
lineGoto(useCurlin); // and go to beginning of undo stuff
if (undoPartialLine) // kills partial line before restore
lineDelete(1,false);
for (int from = 0 ; from < MAX_UNDO && undoBuff[from] != 0 ; ++from)
{ // scan whole buffer
for (int to = 0 ; to < MAX_LINE && from < MAX_UNDO ; ++to)
{ // copy line at a time
if (undoBuff[from] == '\n' || undoBuff[from] == 0)
{ // \n marks end of each line
ln[to] = 0;
insertLine(ln, useCurlin + lines++, false); // insert it
break;
}
else
ln[to] = undoBuff[from++];
}
}
lineGoto(useCurlin); // and go to beginning of undo stuff
//@@ goto curchr...
long lim = lineLenBF(curlin);
for (int ic = 0; ic < lim && col_pos(curchr,1) < undoCurcol ; ++ic)
charRight(1, 0);
if (state.readOnly)
return 0;
return 1;
}
// =====================>>> vTextEditor::tvhdln <<<=========================
void vTextEditor::tvhdln(void)
{ /* tvhdln - home to display line */
int xf;
if (curlin < 1)
tvxy(1,1);
else
{
if (mark.beg_lin > 0)
{
tvxy(1,tvdlin);
type_lines(curlin, 1);
}
xf = findX();
tvxy(xf,tvdlin);
}
}
// ========================>>> vTextEditor::tvtyln <<<======================
void vTextEditor::tvtyln(long lineNum, BUFFPTR chrptr, int whole_line)
{ /* tvtyln - type a line on the screen without cr/lf */
xoutcm = tvx;
tvplin(lineNum,chrptr,whole_line,-1,-1);
}
// =======================>>> vTextEditor::update <<<=======================
void vTextEditor::update(long change)
{ /* update - update the screen when line position has changed
will not be used if any alterations have been made */
if (change == 0)
{
tvhdln();
return;
}
setScrollBar();
if (gState.fixed_scroll)
updateScroll(change); /* cursor stays on fixed line */
else
updateNoScroll(change); /* cursor roams around screen */
}
// ===================>>> vTextEditor::updateScroll <<<=====================
void vTextEditor::updateScroll(long change)
{ /* update - update the screen when line position has changed
will not be used if any alterations have been made */
long abschg;
if (!state.echof)
return;
abschg = change;
if (change < 0) /* have to scroll screen down */
{
abschg = (-change);
if (tvdlin-abschg < 1)
Verify();
else if (curlin < tvdlin) /* won't fit exactly */
{
if (tvdlin >= dsplin && abschg != 1)
{
tvclr(); /* clear the screen */
type_lines((long)1,GetRows()); /* type out a screen */
}
tvdlin = curlin;
}
else if (tvdlin - abschg >= dsplin)
tvdlin -= abschg;
else
{
if (tvdlin > dsplin)
{ /* moving up from below display line */
abschg = dsplin-(tvdlin-abschg);
tvdlin=dsplin; /* update */
}
tvtopb((int)abschg); /* make blank lines at top */
type_lines((long)(curlin-tvdlin+1),(int)abschg); /* fill in */
}
}
else if (change > 0) /* have to scroll screen up */
{
if (tvdlin+change > GetRows() && tvdlin < dsplin ||
change >= GetRows())
Verify();
else if (tvdlin < dsplin || lastLine <= GetRows())
if (tvdlin+change > dsplin && lastLine > GetRows())
Verify();
else
tvdlin += change;
else if (lastLine - curlin < GetRows() - tvdlin) /* on bottom part */
{
if (tvdlin<=dsplin && abschg != 1)
{
tvclr(); /* rewrite whole screen */
type_lines((long)(lastLine - GetRows() + 1), GetRows());
}
tvdlin = (int) minl((long)GetRows(), (long)(lastLine))
- (lastLine - curlin + 1) + 1;
}
else
{
tvbotb((int)abschg); /* make room */
tvxy(1,(int)(GetRows()-abschg+1)); /* home to right line */
type_lines((long)(curlin+GetRows()-tvdlin-abschg+1),(int)abschg); /* fix up screen */
if (tvdlin < dsplin)
tvdlin = dsplin;
}
}
tvhdln();
}
// ==================>>> vTextEditor::updateNoScroll <<<====================
void vTextEditor::updateNoScroll(long change)
{ /* update - update the screen when line position has changed
will not be used if any alterations have been made */
long abschg;
if (!state.echof)
return;
abschg = change;
if (change < 0) /* have to scroll screen down */
{
abschg = (-change);
if (curlin + abschg < tvdlin && curlin < tvdlin) // won't fit exactly
{
dsplin = tvdlin = curlin;
}
else if (tvdlin - abschg >= 1)
{
dsplin = tvdlin -= (int)abschg;
}
else if (abschg == 1) /* simple case */
{
tvtopb((int)abschg); /* make blank lines at top */
type_lines((long)(curlin-tvdlin+1),(int)abschg); /* fill in */
dsplin = tvdlin = 1;
}
else /* scroll to above top line */
{
dsplin = tvdlin;
Verify();
}
}
else if (change > 0) /* have to scroll screen up */
{
if (tvdlin + change <= GetRows())
{
dsplin =
tvdlin = tvdlin + change;
}
else if (change == 1)
{
tvbotb((int)abschg); /* make room */
tvxy(1,(int)(GetRows()-abschg+1)); /* home to right line */
type_lines((long)(curlin+GetRows()-tvdlin-abschg+1),(int)abschg);
/* fix up screen */
dsplin =
tvdlin = GetRows();
}
else /* scroll to above top line */
{
dsplin = tvdlin;
Verify();
}
}
tvhdln();
}
// #########################################################################
//
// word methods
//
// #########################################################################
// =====================>>> vTextEditor::wordRight <<<=====================
int vTextEditor::wordRight(long cnt)
{ /* wordRight - move cursor over words */
long lim, words, incr, lenmov;
int rv;
if (lastLineBF() < 1)
return 0;
ClearMarkRange(); /* no range now */
wasColCmd = 0;
rv = 1;
lenmov = 0;
if (cnt < 0)
{
incr = (-1); /* change */
lim = (-cnt);
}
else if (cnt == 0)
{
incr = -1;
lim = 0;
}
else
{
incr = 1;
lim = cnt;
}
for (words = 1; words <= lim; ++words)
{
if ((IsEndLine(GCh(curchr)) && incr > 0) ||
(curchr == GLine(curlin) && incr < 0) )
{
if (curlin + incr > lastLine || curlin + incr < 1)
{
rv = 0;
break; /* at a buffer limit, so quit */
}
lineDownBeg((long)incr,0); /* move up or down */
lenmov += incr;
if (cnt<0)
lineEnd();
continue; /* move to next word */
}
/* ok, first, skip over word characters */
while (IsWordChar(GCh(curchr)))
{
if (curchr == GLine(curlin) && incr < 0)
goto l100;
else
{
curchr += incr;
lenmov += incr;
}
}
/* now skip over remaining non word chars */
while (! IsWordChar(GCh(curchr)))
{
if ((IsEndLine(GCh(curchr)) && incr > 0) ||
(curchr == GLine(curlin) && incr < 0) )
break;
else
{
curchr += incr;
lenmov += incr;
}
}
l100: ;
}
if (incr < 0) /* move cursor to beginning of word */
{
while (IsWordChar(GCh(curchr-1)))
{
curchr += incr;
lenmov += incr;
}
}
tvhdln();
oldlen = lenmov;
ChangeLoc(curlin,col_pos(curchr,0));
return rv;
}
// ====================>>> vTextEditor::IsWordChar <<<======================
int vTextEditor::IsWordChar(int chr)
{ /* IsWordChar - determine if a character is a "word" type character */
if ((chr>='a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') ||
(chr >= '0' && chr <= '9') || chr == '_' || chr == '\\')
return 1;
else
return 0;
}
// #########################################################################
//
// DEFAULT vTextEdCmdInterp
//
// #########################################################################
// initially, all blank. We will allocate space on an as needed
// basis. We will let the allocated space hang around until
// we exit the editor.
char* vTextEdCmdInterp::QReg[26] = {0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0};
// ==================>>> vTextEdCmdInterp::vTextEdCmdInterp <<<=================
vTextEdCmdInterp::vTextEdCmdInterp(vTextEditor* textEd, vCmdWindow* cw)
{
_textEd = textEd;
_myCmdWindow = cw;
metaWait = 0;
countWait = 0; // not waiting on meta cmd
metaChar = 'A'-'@'; // ^A is default meta char
cmdCount = 1;
curReg = 'a';
}
// ==================>>> vTextEdCmdInterp::~vTextEdCmdInterp <<<================
vTextEdCmdInterp::~vTextEdCmdInterp()
{
}
// =====================>>> vTextEdCmdInterp::InitCmdInterp <<<====================
void vTextEdCmdInterp::InitCmdInterp()
{
}
// --------------- Global Helper routines for most CI's --------------------
// =====================>>> vTextEdCmdInterp::setQReg <<<====================
int vTextEdCmdInterp::setQReg(int reg, char* str)
{
// validate reg range, then copy str to appropriate entry
if (reg < 'a' || reg > 'z')
return 0; // use default [0]
int regI = reg - 'a'; // make correct Index range [1-26]
if (QReg[regI] == 0) // not allocated yet
{
QReg[regI] = new char[strlen(str)+1];
}
else if (strlen(QReg[regI]) < strlen(str)) // reallocate space
{
delete [] QReg[regI];
QReg[regI] = new char[strlen(str)+1];
}
if (!QReg[regI]) // did we get the space?
return 0;
strcpy(QReg[regI], str); // save it
return 1;
}
// =====================>>> vTextEdCmdInterp::getQReg <<<====================
char* vTextEdCmdInterp::getQReg(int reg)
{
if (reg < 'a' || reg > 'z')
return 0; // use default [0]
int regI = reg - 'a'; // make correct Index range [1-26]
return QReg[regI];
}
// =====================>>> vTextEdCmdInterp::execQReg <<<====================
int vTextEdCmdInterp::execQReg(int reg, long count)
{
// send all commands in Q register reg to the editor count times
char execbeg[] = "Q[x] - Executing";
char execdone[] = "Q[x] - Done";
char execfail[] = "Q[x] - Did not complete";
char execempty[] = "Q[x] - Empty - no commands";
execbeg[2] = curReg;
execdone[2] = curReg;
execfail[2] = curReg;
execempty[2] = curReg;
int retval = 1;
char* rp = getQReg(reg);
if (!rp || ! *rp)
{
te()->StatusMessage(execempty);
return 0;
}
te()->StatusMessage(execbeg);
int oldech = te()->GetEdState().echof;
if (cmdCount > 1)
te()->SetEchoF(0);
for (long ix = 0 ; ix < count && retval ; ++ix)
{
for (char* cp = rp ; *cp ; ++cp)
{
retval = ProcessKey((vKey)*cp, 0); // recursively pass to the CI
if (retval <= 0)
{
retval = 0;
break;
}
}
}
te()->SetEchoF(oldech); // restore echo state
te()->Verify();
if (!retval)
te()->StatusMessage(execfail);
else
te()->StatusMessage(execdone);
return retval; // done...
}
// ==================>>> vTextEdCmdInterp::echoInput <<<==================
void vTextEdCmdInterp::echoInput(vKey chr, char* buff, int maxb)
{
// echoes input to status line, with editing and ctrl char mapping
char eb[256]; // to really echo
char tmp[2] = "x";
if (chr == vk_BackSpace)
{
int pl = strlen(buff);
if (pl > 0)
buff[pl-1] = 0; // wipe last char
}
else
{
tmp[0] = (char)chr; // add to pattern
if (strlen(buff) < (unsigned int)maxb)
{
strcat(buff, tmp);
}
}
int ix = 0;
for (char* cp = buff ; *cp && ix < 254 ; cp++)
{
if (*cp == vk_Escape)
{
eb[ix++] = '$';
}
else if (*cp < ' ') // a ctrl char
{
eb[ix++] = '^';
eb[ix++] = *cp + '@';
}
else
eb[ix++] = *cp;
}
eb[ix] = 0;
te()->StatusMessage(eb);
return;
}
// ----------------------------- Default CI --------------------------------
// =====================>>> vTextEdCmdInterp::CmdInterpHelp <<<====================
void vTextEdCmdInterp::CmdInterpHelp()
{
static char* helplist [] =
{
"Standard V Generic Editor",
"Key -- Command Description",
"Esc -- Prefix to enter count n",
" ",
" *** Movement Commands ***",
"nLeft -- Move left [^L]",
"nCtl-Left -- Move left a word",
"nUp -- Move up [^U]",
"nShft-Up -- Move up to beg of line",
"nRight -- Move right [^R]",
"nCtl-Right -- Move right a word",
"nDown -- Move down [^D]",
"nShft-Down -- Move down to beg of line",
"Home -- Goto beg of line [^A,]",
"Ctrl-Home -- Goto beg of file",
"End -- Goto end of line [^A.]",
"Ctrl-End -- Goto end of file",
"nPgUp -- Move a screenful up",
"nCtrl-PgUp -- Scroll a screenful up",
"nPgDn -- Move a screenful down",
"nCtrl-PgDn -- Scroll a screenful down",
"n^G -- Goto line n",
"n^N -- Note (mark) location n (1-25)",
"n^AN -- Goto noted location n",
"^AM -- Center Cursor in screen",
"^Av -- Repaint screen",
" ",
" *** Searching commands ***",
"^A[ -- Balance match",
"^F -- Find pattern",
"Shift-^F -- Find next",
"^Af -- Find/Replace",
" ",
" *** Insertion Commands ***",
"n^AIns -- Insert char with value of n",
"Ins -- Toggle insert/overtype",
"n^O -- Open a new blank line",
" ",
" *** Kill/change commands ***",
"nBkspace -- Delete previous char",
"^ABkspace -- Delete to line begin [^A']",
"nDel -- Delete next char",
"^ADel -- Delete to line end [^A\"]",
"nShft-Del -- Delete line [^K]",
"nShft-^C -- Fold case",
"n^P -- Prettyprint code/text",
"^V -- Paste from clipboard",
"^C -- Copy highlight to clipboard",
"^X -- Cut highlight to clipboard",
"^Z -- Undo",
0
};
static CommandObject HelpD[] =
{
{C_Button,M_Cancel,0,"Close",NoList,CA_None,isSens,NoFrame,0,0},
{C_Label,8000,0,"List of Editor commands",NoList,CA_None,isSens,NoFrame,M_Cancel,0},
{C_List,8001,0,"Help",(void*)helplist,CA_Size,
isSens, NoFrame,0,M_Cancel,32},
{C_EndOfList,0,0,0,0,CA_None,0,0,0}
};
// I'd have liked a modeless dialog, but then I'd have to track
// and delete the object. This way it gets dynamically created
// each time.
ItemVal temp;
vModalDialog Help(te()->GetPaneParent(),"Editor Help");
Help.AddDialogCmds(HelpD);
(void)Help.ShowModalDialog("Generic Editor Help",temp);
}
// =====================>>> vTextEdCmdInterp::ProcessKey <<<====================
int vTextEdCmdInterp::ProcessKey(vKey key, unsigned int shift)
{
// Commands supported by default keyboard mapping:
//
// Most commands can be preceeded by a count, which is entered
// with Esc followed by a value followed by the command. Negative
// counts can be used for most commands as well. An n in front
// of a command indicates it can use a count.
//
// Some commands have synonyms compatible with See and
// touch typing shown in []'s (e.g., ^A, for Home).
//
// Commands that change text interact with highlighted text
// by deleting it, but without copying it to the clipboard.
// ^X and ^C interact with the clipboard.
//
//
// ^A[ Balance match
// ^ABackspace Delete to line beginngin [^A']
// ^ADel Delete to line end [^A"]
// n^AIns Insert character with value of count
// ^AM Center Cursor to Middle of screen
// ^Av Repaint screen
// ^C Copy highlighted text to clipboard
// nShift-^C Fold case
// ^F Find pattern
// Shift-^F Find next
// n^G Goto line specified by count
// n^K Kill line
// n^O Open a new blank line
// ^V Paste from clipboard
// ^X Cut highlighted text to clipboard
// nLeft Move left [^L]
// nCtrl-Left Move left a word
// nUp Move up [^U]
// nShift-Up Move up to beginning of line
// nRight Move right [^R]
// nCtrl-Right Move right a word
// nDown Move down [^D]
// nShift-Down Move down to beginning of line
// nBackspace Delete previous character
// Esc Prefix for entering counts
// nDel Delete next character
// nShift-Del Delete line
// Home Goto beginning of line [^A,]
// Ctrl-Home Goto beginning of file
// End Goto end of line [^A.]
// Ctrl-End Goto end of file
// nPgUp Move a screenful up
// nCtrl-PgUp Scroll a screenful up
// nPgDn Move a screenful down
// nCtrl-PgDn Scroll a screenful down
// Ins Toggle insert/overtype
if (vk_IsModifier(key)) // ignore modifiers
return -1;
int retval = 1;
if (countWait != 0)
{
if (key == '-')
{
countWait = -1;
return 1;
}
else if (key >= '0' && key <= '9') // it is a count
{
cmdCount = (cmdCount * 10) + (key - '0');
return 1;
}
else
{
cmdCount *= countWait; // negative?
countWait = 0; // done building count
}
}
if (metaWait) // Meta command
{
metaWait = 0;
switch (key)
{
case vk_KP_Insert:
case vk_Insert:
{
retval = te()->charInsert(cmdCount);
break;
}
case '\'':
case vk_BackSpace:
{
retval = te()->lineDeleteFront();
break;
}
case '"':
case vk_Delete:
case vk_KP_Delete:
{
retval = te()->lineDeleteToEnd();
break;
}
case ',': // line beginning
{
te()->lineBeginning();
break;
}
case '.': // line end
{
te()->lineEnd();
break;
}
case '[': // ^A[ - bal match
{
retval = te()->BalMatch(cmdCount);
break;
}
case 'F':
case 'f':
{
te()->EditCommand(edReplace,cmdCount);
break;
}
case 'M':
case 'm':
{
te()->CenterScreen();
break;
}
case 'N': // go to noted location
case 'n':
{
retval = te()->EditCommand(edNoteGoto,cmdCount);
break;
}
case 'v':
case 'V':
case 'V'-'@':
{
te()->Verify();
break;
}
default:
{
retval = -1;
break;
}
}
cmdCount = 1;
return retval;
}
else if (key == (vKey) metaChar)
{
metaWait = 1;
return 1;
}
switch (key)
{
case 'C'-'@': // ^C : Copy ; Shift-^C: fold case
{
if (shift & VKM_Shift) // Shift-^F
{
retval = te()->charFoldCase(cmdCount);
}
else
te()->EditCopy();
break;
}
case 'F'-'@': // ^F: find, Shift-^F: find next
{
if (shift & VKM_Shift) // Shift-^F
{
retval = te()->EditCommand(edFindNext, cmdCount);
}
else
{
retval = te()->EditCommand(edFind, cmdCount);
}
break;
}
case 'G'-'@': // ^G: Goto line
{
retval = te()->lineGoto(cmdCount);
break;
}
case 'K'-'@': // ^K: Kill line
{
te()->lineDelete(cmdCount);
break;
}
case 'N'-'@': // ^N: note location
{
retval = te()->EditCommand(edNoteLocation, cmdCount);
break;
}
case 'O'-'@': // ^O: Open line
{
retval = te()->lineOpen(cmdCount);
break;
}
case 'P'-'@': // ^P: Pretty print code
{
switch (te()->GetFileType())
{
case Text:
case HTML:
case TeX:
retval = te()->lineFill(cmdCount);
break;
case CPP:
case Java:
retval = te()->formatC(cmdCount);
break;
case gccError:
case javaError:
retval = 0;
break;
default:
retval = 0;
break;
}
break;
}
case 'V'-'@': // ^V: Paste
{
te()->EditPaste();
break;
}
case 'X'-'@': // ^X : Cut
{
te()->EditCut();
break;
}
case 'Z'-'@': // ^Z : Undo
{
te()->undoEdit();
break;
}
case 'L'-'@':
case vk_Left:
case vk_KP_Left:
{
if (shift & VKM_Ctrl || shift & VKM_Shift)
retval = te()->wordRight(-cmdCount);
else
retval = te()->charRight(-cmdCount,1);
break;
}
case 'U'-'@':
case vk_Up:
case vk_KP_Up:
{
if (shift & VKM_Shift)
retval = te()->lineDownBeg(-cmdCount);
else
retval = te()->lineDown(-cmdCount);
break;
}
case 'R'-'@':
case vk_Right:
case vk_KP_Right:
{
if (shift & VKM_Ctrl ||shift & VKM_Shift)
retval = te()->wordRight(cmdCount);
else
retval = te()->charRight(cmdCount,1);
break;
}
case 'D'-'@':
case vk_KP_Down:
case vk_Down:
{
if (shift & VKM_Shift)
retval = te()->lineDownBeg(cmdCount);
else
retval = te()->lineDown(cmdCount);
break;
}
case vk_BackSpace:
{
retval = te()->charDelete(-cmdCount);
break;
}
case vk_Tab:
{
retval = te()->defaultKeyIn('\t',shift);
break;
}
case vk_Linefeed:
{
break;
}
case vk_Return:
case vk_KP_Enter:
{
retval = te()->defaultKeyIn('\n',shift);
break;
}
case vk_Escape:
{
countWait = 1;
cmdCount = 0;
return 1;
}
case vk_Delete:
case vk_KP_Delete:
{
if (shift & VKM_Shift || shift & VKM_Ctrl)
te()->lineDelete(cmdCount);
else
retval = te()->charDelete(cmdCount);
break;
}
case vk_Home:
case vk_KP_Home:
{
if (shift & VKM_Shift || shift & VKM_Ctrl)
retval = te()->lineGoto(1);
else
te()->lineBeginning();
break;
}
case vk_Page_Up:
case vk_KP_Page_Up:
{
if (shift & VKM_Ctrl)
te()->scrollDown(-cmdCount * te()->GetRows());
else
retval = te()->lineDown((long)(-cmdCount * te()->GetRows()));
break;
}
case vk_Page_Down:
case vk_KP_Page_Down:
{
if (shift & VKM_Ctrl)
te()->scrollDown(cmdCount * te()->GetRows());
else
retval = te()->lineDown(
(long) te()->minl((cmdCount * te()->GetRows()),
(long)(te()->GetLines() - te()->GetCurLine() + 1)));
break;
}
case vk_End:
case vk_KP_End:
{
if (shift & VKM_Shift || shift & VKM_Ctrl)
te()->bufferBottom();
else
te()->lineEnd();
break;
}
case vk_KP_Insert:
case vk_Insert:
{
te()->SetInsMode(!(te()->GetEdState().ins_mode));
te()->ChangeInsMode(te()->GetEdState().ins_mode);
break;
}
default:
{
retval = te()->defaultKeyIn(key,shift);
break;
}
}
cmdCount = 1;
return retval;
}
//***********************************************************************
DialogCmd vTEConfirm::ConfirmCmds[] =
{
{C_Frame,30,0,"",NoList,CA_NoSpace|CA_NoBorder,isSens,NoFrame,0,0},
{C_Label, 31, 0,"Replace?",NoList,CA_None,isSens,30, 0, 0},
{C_Button, YesRepl, 0, "Yes", NoList, CA_DefaultButton, isSens, 30, 31, 0, 0, "Replace this instance"},
{C_Button, YesReplNext, 0, "Yes/Next", NoList, CA_None, isSens, 30, YesRepl, 0, 0, "Replace this instance, go on to next"},
{C_Button, NoRepl,0," No",NoList,CA_None,isSens,30,31,YesRepl,0,"Don't replace"},
{C_Button, NoReplNext,0," No/Next",NoList,CA_None,isSens,30,NoRepl,YesRepl,0,"Don't replace, go on to next"},
{C_Button, QuitR, 0,"Stop",NoList,CA_None,isSens,30,31,NoRepl,0,"Stop Replace"},
{C_Button, RepAll,0,"Yes/All",NoList,CA_None,isSens,30,QuitR,NoRepl,0,"Replace All"},
{C_EndOfList,0,0,0,0,CA_None,0,0,0}
};
// =====================>>> vTEConfirm::Confirm <<<====================
int vTEConfirm::ConfirmRep()
{
int id, val;
if (!added)
{
AddDialogCmds(ConfirmCmds);
added = 1;
}
id = ShowModalDialog("Replace?",val);
return id;
}
//====================>>> vTEConfirm::DialogCommand <<<=======================
void vTEConfirm::DialogCommand(ItemVal id, ItemVal val, CmdType ctype)
{
vModalDialog::DialogCommand(id,val,ctype);
CloseDialog();
}
//==================================================================
// I wrote the original version of the See editor, called TVX, in
// 1980 in a language called Ratfor, a preprocessor that gave
// Fortran the syntax of C. At that time, much of the TVX command
// structure was based on a Tenex editor called TV, which in turn
// had a command syntax similar to an editor called TECO. The Ratfor
// version of TVX actually worked quite will on a TRS-80 with 48K
// (that's right, 48K) of RAM. I've been using some decendent of
// TVX ever since. My fingers know the commands too well to
// change much.
//
// TVX was character based, and ported to many platforms,
// including CP/M, RT-11, RSX-11, VMS, and of course Unix. Much
// of the organization of the edit updates are designed to work
// over serial lines on a text terminal. This was accomplished
// using either a custom terminal driver, or a package such as
// TERMCAP on Unix. In fact, one could use the full screen editor
// quite well over even 1200 Baud serial lines (visual editors
// such as Vi really failed at much less than 9600 Baud).
//
// TVX was translated (semi-automatically) from Ratfor to C. Most
// recently (about 1995) I converted it to C++, and finally to this
// C++ class for inclusion in the VGUI package. Somewhere along
// the way, I changed the name to See, which stood for Simple
// Enough Editor.
//
// Certainly, if I were designing an editor from scratch in C++,
// it would be quite different in structure than this editor.
// However, I always found it easier to port rather than rewrite.
// Many of the specialized editor commands I developed for See
// are still found in this code. A good emulation of many editor
// command sets can be implemented with different command
// parsers.
//
// What this goes to show is that old code never dies, it just
// mutates. This code originated almost 20 years ago now. Many of
// the 6 character variable names owe their origin to Fortran
// variable name restrictions. Much of the seemingly complicated
// screen update stuff owes its origin to the restrictions of
// working with different terminals over slow baud rates. As
// time goes by, even this code will probably mutate.
//==================================================================
|