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
|
"""Slices is an interactive text control in which a user types in
commands to be sent to the interpreter. This particular shell is
based on wxPython's wxStyledTextCtrl.
Sponsored by Orbtech - Your source for Python programming expertise.
Slices is a version of shell modified by David Mashburn."""
__author__ = "David N. Mashburn <david.n.mashburn@gmail.com> / "
__author__ += "Patrick K. O'Brien <pobrien@orbtech.com>"
__cvsid__ = "$Id: sliceshell.py 60100 2009-04-12 02:56:29Z RD $"
__revision__ = "$Revision: 60100 $"[11:-2]
import wx
from wx import stc
import keyword
import os
import sys
import time
from buffer import Buffer
import dispatcher
import editor
import editwindow
import document
import frame
from pseudo import PseudoFileIn
from pseudo import PseudoFileOut
from pseudo import PseudoFileErr
from version import VERSION
from magic import magic
from parse import testForContinuations
from path import ls,cd,pwd,sx
sys.ps3 = '<-- ' # Input prompt.
USE_MAGIC=True
# Force updates from long-running commands after this many seconds
PRINT_UPDATE_MAX_TIME=2
NAVKEYS = (wx.WXK_HOME, wx.WXK_END, wx.WXK_LEFT, wx.WXK_RIGHT,
wx.WXK_UP, wx.WXK_DOWN, wx.WXK_PRIOR, wx.WXK_NEXT)
GROUPING_SELECTING=0
IO_SELECTING = 1
GROUPING_START = 2
GROUPING_START_FOLDED = 3
GROUPING_MIDDLE = 4
GROUPING_END = 5
INPUT_START = 6
INPUT_START_FOLDED = 7
INPUT_MIDDLE = 8
INPUT_END = 9
OUTPUT_START = 10
OUTPUT_START_FOLDED = 11
OUTPUT_MIDDLE = 12
OUTPUT_END = 13
OUTPUT_BG = 14
READLINE_BG = 15
INPUT_READLINE = 16
# Could add C integration right into the markers...
# Non-editable file marker for auto-loaded files...
# Weave VariableInput = 15
# Weave C code = 16
# C code = 17 (only for use with Pyrex)
# Pyrex / Cython code = 18
GROUPING_MASK = ( 1<<GROUPING_START | 1<<GROUPING_START_FOLDED |
1<<GROUPING_MIDDLE | 1<<GROUPING_END )
INPUT_MASK = ( 1<<INPUT_START | 1<<INPUT_START_FOLDED |
1<<INPUT_MIDDLE | 1<<INPUT_END )
OUTPUT_MASK = ( 1<<OUTPUT_START | 1<<OUTPUT_START_FOLDED |
1<<OUTPUT_MIDDLE | 1<<OUTPUT_END )
IO_MASK = ( INPUT_MASK | OUTPUT_MASK )
IO_START_MASK = ( 1<<INPUT_START | 1<<OUTPUT_START )
IO_START_FOLDED_MASK = ( 1<<INPUT_START_FOLDED | 1<<OUTPUT_START_FOLDED )
IO_ANY_START_MASK = ( 1<<INPUT_START | 1<<OUTPUT_START |
1<<INPUT_START_FOLDED | 1<<OUTPUT_START_FOLDED )
IO_MIDDLE_MASK = ( 1<<INPUT_MIDDLE | 1<<OUTPUT_MIDDLE )
IO_END_MASK = ( 1<<INPUT_END | 1<<OUTPUT_END )
usrBinEnvPythonText = '#!/usr/bin/env python\n'
pyslicesFormatHeaderText = ['#PySlices Save Format Version 1.1 (PySlices v0.9.7.8 and later)\n',
'#PySlices Save Format Version 1.2 (PySlices v0.9.8 and later)\n']
groupingStartText = '#PySlices Marker Information -- Begin Grouping Slice\n'
inputStartText = '#PySlices Marker Information -- Begin Input Slice\n'
outputStartText = '#PySlices Marker Information -- Begin Output Slice\n'
tutorialText = """
Tutorial!!!
------------------------------------------------------------------------
PySlices is the newest member of the Py suite!
It is a modified version of PyCrust that supports multi-line commands.
Input and output are contained in "Slices" shown as markers in the left margin.
Input Slices have RED margins (active, editable).
Output Slices have BLUE margins (frozen, not editable).
Commands in slices can be on more than one line, as with Sage or Mathematica.
For example, the command:
a=1
b=2
print a+b
will all run in sequence, much like a script.
Try running the above Input Slice by clicking somewhere in its text and
using Ctrl-Return, Shift-Return, or Numpad Enter to execute.
Previous commands (Old Slices) can be re-edited and run again in place.
Slices can also be:
* selceted (click on the margin, Shift-click for multiple selection)
* folded (click the margin twice)
* selected and deleted (hit delete while selected)
* divided (Ctrl-D)
* merged (Ctrl-M while selecting adjacent, like-colored slices)
Try deleting the slice above this one by clicking on the red margin.
If you want a more traditional shell feel, try enabling "Shell Mode" in
"Options->Settings->Shell Mode" (or try PyCrust).
In Shell Mode, two returns in a row executes the command, and
Ctrl-Return and Shift-Return always print newlines.
Saving and opening "sessions" is now supported! This is a little
different to other shells where the history is saved. With PySlices,
the whole document is saved in a simple text format!
To disable this Tutorial on startup, uncheck it in the menu at:
"Options->Startup->Show PySlices tutorial"
PySlices may not be the best thing since sliced bread, but
I hope it makes using Python a little bit sweeter!
"""
class SlicesShellFrame(frame.Frame, frame.ShellFrameMixin):
"""Frame containing the sliceshell component."""
name = 'SlicesShell Frame'
revision = __revision__
def __init__(self, parent=None, id=-1, title='PySlicesShell',
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=wx.DEFAULT_FRAME_STYLE, locals=None,
InterpClass=None,
config=None, dataDir=None, filename=None,
*args, **kwds):
"""Create SlicesShellFrame instance."""
frame.Frame.__init__(self, parent, id, title, pos, size, style,shellName='PySlices')
frame.ShellFrameMixin.__init__(self, config, dataDir)
if size == wx.DefaultSize:
self.SetSize((750, 525))
intro = 'PySlices %s - The Flakiest Python Shell... Cut Up!' % VERSION
self.SetStatusText(intro.replace('\n', ', '))
self.sliceshell = SlicesShell(parent=self, id=-1, introText=intro,
locals=locals, InterpClass=InterpClass,
startupScript=self.startupScript,
execStartupScript=self.execStartupScript,
showPySlicesTutorial=self.showPySlicesTutorial,
enableShellMode=self.enableShellMode,
hideFoldingMargin=self.hideFoldingMargin,
*args, **kwds)
self.buffer = self.sliceshell.buffer
# Override the shell so that status messages go to the status bar.
self.sliceshell.setStatusText = self.SetStatusText
self.sliceshell.SetFocus()
self.LoadSettings()
self.currentDirectory = os.path.expanduser('~')
if filename!=None:
self.bufferOpen(filename)
self.Bind(wx.EVT_IDLE, self.OnIdle)
def OnClose(self, event):
"""Event handler for closing."""
self.bufferClose()
# This isn't working the way I want, but I'll leave it for now.
#if self.sliceshell.waiting:
# if event.CanVeto():
# event.Veto(True)
#else:
# # TODO: Add check for saving
# self.SaveSettings()
# self.sliceshell.destroy()
# self.Destroy()
def OnAbout(self, event):
"""Display an About window."""
title = 'About PySliceShell'
text = 'PySliceShell %s\n\n' % VERSION + \
'Yet another Python shell, only flakier.\n\n' + \
'Half-baked by Patrick K. O\'Brien,\n' + \
'the other half is still in the oven.\n\n' + \
'Shell Revision: %s\n' % self.shell.revision + \
'Interpreter Revision: %s\n\n' % self.shell.interp.revision + \
'Platform: %s\n' % sys.platform + \
'Python Version: %s\n' % sys.version.split()[0] + \
'wxPython Version: %s\n' % wx.VERSION_STRING + \
('\t(%s)\n' % ", ".join(wx.PlatformInfo[1:]))
dialog = wx.MessageDialog(self, text, title,
wx.OK | wx.ICON_INFORMATION)
dialog.ShowModal()
dialog.Destroy()
def OnHelp(self, event):
"""Show a help dialog."""
frame.ShellFrameMixin.OnHelp(self, event)
def LoadSettings(self):
if self.config is not None:
frame.ShellFrameMixin.LoadSettings(self)
frame.Frame.LoadSettings(self, self.config)
self.sliceshell.LoadSettings(self.config)
def SaveSettings(self, force=False):
if self.config is not None:
frame.ShellFrameMixin.SaveSettings(self,force)
if self.autoSaveSettings or force:
frame.Frame.SaveSettings(self, self.config)
self.sliceshell.SaveSettings(self.config)
def DoSaveSettings(self):
if self.config is not None:
self.SaveSettings(force=True)
self.config.Flush()
def OnEnableShellMode(self,event):
"""Change between Slices Mode and Shell Mode"""
frame.Frame.OnEnableShellMode(self,event)
self.sliceshell.ToggleShellMode(self.enableShellMode)
def OnHideFoldingMargin(self,event):
"""Change between Slices Mode and Shell Mode"""
frame.Frame.OnHideFoldingMargin(self,event)
self.sliceshell.ToggleFoldingMargin(self.hideFoldingMargin)
# Copied Straight from crustslices.py (update both with any changes...)
# Stolen Straight from editor.EditorFrame
# Modified a little... :)
# ||
# \/
def OnIdle(self, event):
"""Event handler for idle time."""
self._updateTitle()
event.Skip()
def _updateTitle(self):
"""Show current title information."""
title = self.GetTitle()
if self.bufferHasChanged():
if title.startswith('* '):
pass
else:
self.SetTitle('* ' + title)
else:
if title.startswith('* '):
self.SetTitle(title[2:])
def hasBuffer(self):
"""Return True if there is a current buffer."""
if self.buffer:
return True
else:
return False
def bufferClose(self):
"""Close buffer."""
if self.buffer.hasChanged():
cancel = self.bufferSuggestSave()
if cancel:
#event.Veto()
return cancel
self.SaveSettings()
self.sliceshell.destroy()
self.bufferDestroy()
self.Destroy()
return False
def bufferCreate(self, filename=None):
"""Create new buffer."""
self.bufferDestroy()
buffer = Buffer()
self.panel = panel = wx.Panel(parent=self, id=-1)
panel.Bind (wx.EVT_ERASE_BACKGROUND, lambda x: x)
editor = Editor(parent=panel)
panel.editor = editor
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(editor.window, 1, wx.EXPAND)
panel.SetSizer(sizer)
panel.SetAutoLayout(True)
sizer.Layout()
buffer.addEditor(editor)
buffer.open(filename)
self.setEditor(editor)
self.editor.setFocus()
self.SendSizeEvent()
def bufferDestroy(self):
"""Destroy the current buffer."""
if self.buffer:
self.editor = None
self.buffer = None
def bufferHasChanged(self):
"""Return True if buffer has changed since last save."""
if self.buffer:
return self.buffer.hasChanged()
else:
return False
def bufferNew(self):
"""Create new buffer."""
cancel = self.bufferSuggestSave()
if cancel:
return cancel
#self.bufferCreate()
self.clear()
self.SetTitle( 'PySlices')
self.sliceshell.NeedsCheckForSave=False
self.sliceshell.SetSavePoint()
self.buffer.doc = document.Document()
self.buffer.name = 'This shell'
self.buffer.modulename = self.buffer.doc.filebase
cancel = False
return cancel
def bufferOpen(self,file=None):
"""Open file in buffer."""
if self.bufferHasChanged():
cancel = self.bufferSuggestSave()
if cancel:
return cancel
if file==None:
file=wx.FileSelector('Open a PySlices File',
wildcard='*.pyslices',
default_path=self.currentDirectory)
if file!=None and file!=u'':
fid=open(file,'r')
self.sliceshell.LoadPySlicesFile(fid)
fid.close()
self.currentDirectory = os.path.split(file)[0]
self.SetTitle( os.path.split(file)[1] + ' - PySlices')
self.sliceshell.NeedsCheckForSave=False
self.sliceshell.SetSavePoint()
self.buffer.doc = document.Document(file)
self.buffer.name = self.buffer.doc.filename
self.buffer.modulename = self.buffer.doc.filebase
self.sliceshell.ScrollToLine(0)
return
## def bufferPrint(self):
## """Print buffer."""
## pass
## def bufferRevert(self):
## """Revert buffer to version of file on disk."""
## pass
# was self.buffer.save(self): # """Save buffer."""
def simpleSave(self,confirmed=False):
filepath = self.buffer.doc.filepath
self.buffer.confirmed = confirmed
if not filepath:
return # XXX Get filename
if not os.path.exists(filepath):
self.buffer.confirmed = True
if not self.buffer.confirmed:
self.buffer.confirmed = self.buffer.overwriteConfirm(filepath)
if self.buffer.confirmed:
try:
fid = open(filepath, 'wb')
self.sliceshell.SavePySlicesFile(fid)
finally:
if fid:
fid.close()
self.sliceshell.SetSavePoint()
self.SetTitle( os.path.split(filepath)[1] + ' - PySlices')
self.sliceshell.NeedsCheckForSave=False
def bufferSave(self):
"""Save buffer to its file."""
if self.buffer.doc.filepath:
# self.buffer.save()
self.simpleSave(confirmed=True)
cancel = False
else:
cancel = self.bufferSaveAs()
return cancel
def bufferSaveAs(self):
"""Save buffer to a new filename."""
if self.bufferHasChanged() and self.buffer.doc.filepath:
cancel = self.bufferSuggestSave()
if cancel:
return cancel
filedir = ''
if self.buffer and self.buffer.doc.filedir:
filedir = self.buffer.doc.filedir
result = editor.saveSingle(title='Save PySlices File',directory=filedir,
wildcard='PySlices Files (*.pyslices)|*.pyslices')
if result.path not in ['',None]:
if result.path[-9:]!=".pyslices":
result.path+=".pyslices"
self.buffer.doc = document.Document(result.path)
self.buffer.name = self.buffer.doc.filename
self.buffer.modulename = self.buffer.doc.filebase
self.simpleSave(confirmed=True) # allow overwrite
cancel = False
else:
cancel = True
return cancel
def bufferSaveACopy(self):
"""Save buffer to a new filename."""
filedir = ''
if self.buffer and self.buffer.doc.filedir:
filedir = self.buffer.doc.filedir
result = editor.saveSingle(title='Save a Copy of PySlices File',directory=filedir,
wildcard='PySlices Files (*.pyslices)|*.pyslices')
if result.path not in ['',None]:
if result.path[-9:]!=".pyslices":
result.path+=".pyslices"
# if not os.path.exists(result.path):
try: # Allow overwrite...
fid = open(result.path, 'wb')
self.sliceshell.SavePySlicesFile(fid)
finally:
if fid:
fid.close()
cancel = False
else:
cancel = True
return cancel
def bufferSuggestSave(self):
"""Suggest saving changes. Return True if user selected Cancel."""
result = editor.messageDialog(parent=None,
message='%s has changed.\n'
'Would you like to save it first'
'?' % self.buffer.name,
title='Save current file?',
style=wx.YES_NO | wx.CANCEL | wx.NO_DEFAULT |
wx.CENTRE | wx.ICON_QUESTION )
if result.positive:
cancel = self.bufferSave()
else:
cancel = result.text == 'Cancel'
return cancel
def updateNamespace(self):
"""Update the buffer namespace for autocompletion and calltips."""
if self.buffer.updateNamespace():
self.SetStatusText('Namespace updated')
else:
self.SetStatusText('Error executing, unable to update namespace')
# TODO : Update the help text
HELP_TEXT = """\
* Key bindings:
Home Go to the beginning of the line.
End Go to the end of the line.
Shift+Home Select to the beginning of the line.
Shift+End Select to the end of the line.
Ctrl-Home Jump to the beginning of the slice;
If already there, jump to beginning of previous slice
Ctrl-End Jump to the end of the slice;
If already there, jump to end of next slice
Ctrl-PageUp Jump to the beginning of the shell
Ctrl-PageDown Jump to the end of the shell
Ctrl+C Copy selected text, removing prompts.
Ctrl+Shift+C Copy selected text, retaining prompts.
Alt+C Copy to the clipboard, including prefixed prompts.
Ctrl+X Cut selected text.
Ctrl+V Paste from clipboard.
Ctrl+Shift+V Paste and run multiple commands from clipboard.
Ctrl+Up Arrow Retrieve Previous History item.
Alt+P Retrieve Previous History item.
Ctrl+Down Arrow Retrieve Next History item.
Alt+N Retrieve Next History item.
Shift+Up Arrow Insert Previous History item.
Shift+Down Arrow Insert Next History item.
F8 Command-completion of History item.
(Type a few characters of a previous command and press F8.)
Ctrl+] Increase font size.
Ctrl+[ Decrease font size.
Ctrl+= Default font size.
Ctrl-Space Show Auto Completion.
Ctrl-Shift-Space Show Call Tip.
Ctrl-Shift-H Complete Text from History.
Ctrl+F Search
Ctrl+G Search next
F12 on/off "free-edit" mode
For testing only -- This does not preserve markers!
In "Slices Mode":
Return Insert new line
Enter (Numpad) Run command in slice
Ctrl+Return ""
Shift+Return ""
In "Shell Mode":
Return or Enter Insert a new line
Ctrl+Return ""
Shift+Return ""
2 Returns in a row Run command in slice
"""
class SlicesShellFacade:
"""Simplified interface to all shell-related functionality.
This is a semi-transparent facade, in that all attributes of other
are accessible, even though only some are visible to the user."""
name = 'SlicesShell Interface'
revision = __revision__
def __init__(self, other):
"""Create a SlicesShellFacade instance."""
d = self.__dict__
d['other'] = other
d['helpText'] = HELP_TEXT
d['this'] = other.this
def help(self):
"""Display some useful information about how to use the slices shell."""
self.write(self.helpText,type='Output')
def __getattr__(self, name):
if hasattr(self.other, name):
return getattr(self.other, name)
else:
raise AttributeError, name
def __setattr__(self, name, value):
if self.__dict__.has_key(name):
self.__dict__[name] = value
elif hasattr(self.other, name):
setattr(self.other, name, value)
else:
raise AttributeError, name
def _getAttributeNames(self):
"""Return list of magic attributes to extend introspection."""
list = [
'about',
'ask',
'autoCallTip',
'autoComplete',
'autoCompleteAutoHide',
'autoCompleteCaseInsensitive',
'autoCompleteIncludeDouble',
'autoCompleteIncludeMagic',
'autoCompleteIncludeSingle',
'callTipInsert',
'clear',
'pause',
'prompt',
'quit',
'redirectStderr',
'redirectStdin',
'redirectStdout',
'run',
'runfile',
'wrap',
'zoom',
]
list.sort()
return list
DISPLAY_TEXT="""
Author: %r
Py Version: %s
Py Slices Shell Revision: %s
Py Interpreter Revision: %s
Python Version: %s
wxPython Version: %s
wxPython PlatformInfo: %s
Platform: %s"""
class SlicesShell(editwindow.EditWindow):
"""Notebook Shell based on StyledTextCtrl."""
name = 'SlicesShell'
revision = __revision__
def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.CLIP_CHILDREN,
introText='', locals=None, InterpClass=None,
startupScript=None, execStartupScript=True,
showPySlicesTutorial=True,enableShellMode=False,
hideFoldingMargin=False, *args, **kwds):
"""Create Shell instance."""
editwindow.EditWindow.__init__(self, parent, id, pos, size, style)
self.wrap()
if locals is None:
import __main__
locals = __main__.__dict__
# Grab these so they can be restored by self.redirect* methods.
self.stdin = sys.stdin
self.stdout = sys.stdout
self.stderr = sys.stderr
# Import a default interpreter class if one isn't provided.
if InterpClass == None:
from interpreter import Interpreter
else:
Interpreter = InterpClass
# Create a replacement for stdin.
self.reader = PseudoFileIn(self.readline, self.readlines)
self.reader.input = ''
self.reader.isreading = False
# Set up the interpreter.
self.interp = Interpreter(locals=locals,
rawin=self.raw_input,
stdin=self.reader,
stdout=PseudoFileOut(self.writeOut),
stderr=PseudoFileErr(self.writeErr),
*args, **kwds)
# Set up the buffer.
self.buffer = Buffer()
self.id = self.GetId()
self.buffer.addEditor(self)
self.buffer.name='This shell'
self.NeedsCheckForSave=False
# Find out for which keycodes the interpreter will autocomplete.
self.autoCompleteKeys = self.interp.getAutoCompleteKeys()
# Keep track of the last non-continuation prompt positions.
# Removed all references to these... solved a lot of odd bugs...
# self.promptPosStart = 0
# self.promptPosEnd = 0
# Keep track of multi-line commands.
self.more = False
# Use Margins to track input / output / slice number
self.margins = True
# For use with forced updates during long-running scripts
self.lastUpdate=None
if self.margins:
# margin 1 is already defined for the line numbers
# may eventually change it back to 0 like it ought to be...
self.SetMarginType(2, stc.STC_MARGIN_SYMBOL)
self.SetMarginType(3, stc.STC_MARGIN_SYMBOL)
self.SetMarginType(4, stc.STC_MARGIN_SYMBOL)
self.SetMarginWidth(2, 22)
self.SetMarginWidth(3, 22)
self.SetMarginWidth(4, 12)
self.SetMarginSensitive(2,True)
self.SetMarginSensitive(3,True)
self.SetMarginSensitive(4,True)
self.SetProperty("fold", "1")
# tabs are bad, use spaces
self.SetProperty("tab.timmy.whinge.level", "4")
self.SetMargins(0,0)
self.SetMarginMask(2, GROUPING_MASK | 1<<GROUPING_SELECTING )
# Display Markers -24...
self.SetMarginMask(3, IO_MASK | 1<<IO_SELECTING | 1<<READLINE_BG | 1<<INPUT_READLINE )
self.SetMarginMask(4, stc.STC_MASK_FOLDERS)
# Set the mask for the line markers, too...
self.SetMarginMask(1, 0)
if hideFoldingMargin:
self.SetMarginWidth(4, 0)
self.hideFoldingMargin=hideFoldingMargin
sel_color="#E0E0E0"
grouping_color="black"
input_color="red"
output_color="blue"
self.MarkerDefine(GROUPING_SELECTING, stc.STC_MARK_FULLRECT,
sel_color, sel_color)
self.MarkerDefine(IO_SELECTING, stc.STC_MARK_FULLRECT,
sel_color, sel_color)
self.MarkerDefine(GROUPING_START, stc.STC_MARK_BOXMINUS,
"white", grouping_color)
self.MarkerDefine(GROUPING_START_FOLDED, stc.STC_MARK_BOXPLUS,
"white", grouping_color)
self.MarkerDefine(GROUPING_MIDDLE, stc.STC_MARK_VLINE,
"white", grouping_color)
self.MarkerDefine(GROUPING_END, stc.STC_MARK_LCORNER,
"white", grouping_color)
self.MarkerDefine(READLINE_BG, stc.STC_MARK_FULLRECT,
wx.Colour(191,191,191), wx.Colour(191,191,191))
self.MarkerDefine(INPUT_READLINE, stc.STC_MARK_CHARACTER+ord('<'),
input_color, wx.Colour(191,191,191))
if enableShellMode:
self.mode='ShellMode'
else:
self.mode='SlicesMode'
self.execOnNextReturn=False
if self.mode=='SlicesMode':
self.MarkerDefine(INPUT_START, stc.STC_MARK_BOXMINUS,
"white", input_color)
self.MarkerDefine(INPUT_START_FOLDED, stc.STC_MARK_BOXPLUS,
"white", input_color)
self.MarkerDefine(INPUT_MIDDLE, stc.STC_MARK_VLINE,
"white", input_color)
self.MarkerDefine(INPUT_END, stc.STC_MARK_LCORNER,
"white", input_color)
elif self.mode=='ShellMode':
self.MarkerDefine(INPUT_START, stc.STC_MARK_ARROWS,
input_color, "white")
self.MarkerDefine(INPUT_START_FOLDED, stc.STC_MARK_BOXPLUS,
"white", input_color)
self.MarkerDefine(INPUT_MIDDLE, stc.STC_MARK_DOTDOTDOT,
input_color, "white")
self.MarkerDefine(INPUT_END, stc.STC_MARK_DOTDOTDOT,
input_color, "white")
self.MarkerDefine(OUTPUT_START, stc.STC_MARK_BOXMINUS,
"white", output_color)
self.MarkerDefine(OUTPUT_START_FOLDED, stc.STC_MARK_BOXPLUS,
"white", output_color)
self.MarkerDefine(OUTPUT_MIDDLE, stc.STC_MARK_VLINE,
"white", output_color)
self.MarkerDefine(OUTPUT_END, stc.STC_MARK_LCORNER,
"white", output_color)
self.MarkerDefine(OUTPUT_BG, stc.STC_MARK_BACKGROUND,
"white", wx.Colour(242,242,255))
# Markers for folding margin...
self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_BOXMINUS,
"white", "#808080")
self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_BOXPLUS,
"white", "#808080")
self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_VLINE,
"white", "#808080")
self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_LCORNER,
"white", "#808080")
self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_BOXPLUSCONNECTED,
"white", "#808080")
self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_BOXMINUSCONNECTED,
"white", "#808080")
self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNER,
"white", "#808080")
# Create the command history. Commands are added into the
# front of the list (ie. at index 0) as they are entered.
# self.historyIndex is the current position in the history; it
# gets incremented as you retrieve the previous command,
# decremented as you retrieve the next, and reset when you hit
# Enter. self.historyIndex == -1 means you're on the current
# command, not in the history.
self.history = []
self.historyIndex = -1
#DNM -- disable these markers...
#seb add mode for "free edit"
self.noteMode = 0
#self.MarkerDefine(0,stc.STC_MARK_ROUNDRECT) # marker for hidden
self.searchTxt = ""
# Assign handlers for keyboard events.
self.Bind(wx.EVT_CHAR, self.OnChar)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
# TODO : Add a general functions to handle mouse clicks in the
# TODO: STC window whose sole purpose is to make it so
# TODO: that margin selection becomes unselected...
# Assign handler for the context menu
self.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu)
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUI)
# Assign handlers for edit events
self.Bind(wx.EVT_MENU, lambda evt: self.Cut(), id=wx.ID_CUT)
self.Bind(wx.EVT_MENU, lambda evt: self.Copy(), id=wx.ID_COPY)
self.Bind(wx.EVT_MENU, lambda evt: self.CopyWithPrompts(), id=frame.ID_COPY_PLUS)
self.Bind(wx.EVT_MENU, lambda evt: self.Paste(), id=wx.ID_PASTE)
self.Bind(wx.EVT_MENU, lambda evt: self.PasteAndRun(), id=frame.ID_PASTE_PLUS)
self.Bind(wx.EVT_MENU, lambda evt: self.SelectAll(), id=wx.ID_SELECTALL)
self.Bind(wx.EVT_MENU, lambda evt: self.Clear(), id=wx.ID_CLEAR)
self.Bind(wx.EVT_MENU, lambda evt: self.Undo(), id=wx.ID_UNDO)
self.Bind(wx.EVT_MENU, lambda evt: self.Redo(), id=wx.ID_REDO)
# Assign handler for idle time.
self.waiting = False
self.Bind(wx.EVT_IDLE, self.OnIdle)
# Display the introductory banner information.
self.showIntro(introText)
outStart,outEnd,inStart,inMiddle,inEnd = [[],[],[],[],[]]
# Make "executed startup script move to the top..."
if showPySlicesTutorial:
self.write(tutorialText,'Output')
tutStart=5
testStart=16
outStart=[tutStart,testStart+3]
outEnd=[tutStart-1,testStart-1]
inStart=[testStart]
inMiddle=[testStart+1]
inEnd=[testStart+2]
# Assign some pseudo keywords to the interpreter's namespace.
self.setBuiltinKeywords()
# Add 'shell' to the interpreter's local namespace.
self.setLocalShell()
# Do this last so the user has complete control over their
# environment. They can override anything they want.
if execStartupScript:
if startupScript is None:
startupScript = os.environ.get('PYTHONSTARTUP')
self.execStartupScript(startupScript)
else:
self.prompt()
outStart+=[0]
outEnd+=[self.GetLineCount()-2]
inStart+=[self.GetLineCount()-1]
# Set all the line markers to the proper initial states...
for i in range(self.GetLineCount()):
self.clearGroupingMarkers(i)
self.clearIOMarkers(i)
if i in outStart:
self.MarkerAdd(i,GROUPING_START)
self.MarkerAdd(i,OUTPUT_START)
# Background color is confusing for tutorial... skip it!
#self.MarkerAdd(i,OUTPUT_BG)
elif i in outEnd:
self.MarkerAdd(i,GROUPING_END)
self.MarkerAdd(i,OUTPUT_END)
#self.MarkerAdd(i,OUTPUT_BG)
elif i in inStart:
self.MarkerAdd(i,GROUPING_START)
self.MarkerAdd(i,INPUT_START)
elif i in inMiddle:
self.MarkerAdd(i,GROUPING_MIDDLE)
self.MarkerAdd(i,INPUT_MIDDLE)
elif i in inEnd:
self.MarkerAdd(i,GROUPING_END)
self.MarkerAdd(i,INPUT_END)
else:
self.MarkerAdd(i,GROUPING_MIDDLE)
self.MarkerAdd(i,OUTPUT_MIDDLE)
#self.MarkerAdd(i,OUTPUT_BG)
self.SliceSelection=False
self.runningSlice=None
## NOTE: See note at bottom of this file...
## #seb: File drag and drop
## self.SetDropTarget( FileDropTarget(self) )
#ADD UNDO
# Everywhere "ADD UNDO" appears, there is new code to handle markers
self.EmptyUndoBuffer()
wx.CallAfter(self.ScrollToLine, 0)
def ToggleShellMode(self,enableShellMode=None):
if enableShellMode==None:
if self.mode=='ShellMode': self.mode='SlicesMode'
elif self.mode=='SlicesMode': self.mode='ShellMode'
elif enableShellMode:
self.mode='ShellMode'
else:
self.mode='SlicesMode'
input_color="red"
if self.mode=='SlicesMode':
self.MarkerDefine(INPUT_START, stc.STC_MARK_BOXMINUS,
"white", input_color)
self.MarkerDefine(INPUT_START_FOLDED, stc.STC_MARK_BOXPLUS,
"white", input_color)
self.MarkerDefine(INPUT_MIDDLE, stc.STC_MARK_VLINE,
"white", input_color)
self.MarkerDefine(INPUT_END, stc.STC_MARK_LCORNER,
"white", input_color)
elif self.mode=='ShellMode':
self.MarkerDefine(INPUT_START, stc.STC_MARK_ARROWS,
input_color, "white")
self.MarkerDefine(INPUT_START_FOLDED, stc.STC_MARK_BOXPLUS,
"white", input_color)
self.MarkerDefine(INPUT_MIDDLE, stc.STC_MARK_DOTDOTDOT,
input_color, "white")
self.MarkerDefine(INPUT_END, stc.STC_MARK_DOTDOTDOT,
input_color, "white")
def ToggleFoldingMargin(self,hideFoldingMargin=None):
if hideFoldingMargin==None:
self.hideFoldingMargin = not self.hideFoldingMargin
else:
self.hideFoldingMargin = hideFoldingMargin
if self.hideFoldingMargin:
self.SetMarginWidth(4, 0)
else:
self.SetMarginWidth(4, 12)
def clearHistory(self):
self.history = []
self.historyIndex = -1
dispatcher.send(signal="SlicesShell.clearHistory")
def destroy(self):
del self.interp
def setFocus(self):
"""Set focus to the slices shell."""
self.SetFocus()
def OnIdle(self, event):
"""Free the CPU to do other things."""
if self.waiting:
time.sleep(0.05)
event.Skip()
def showIntro(self, text=''):
"""Display introductory text in the slices shell."""
if text:
self.write(text,type='Output')
try:
if self.interp.introText:
if text and not text.endswith(os.linesep):
self.write(os.linesep,type='Output')
self.write(self.interp.introText,type='Output')
except AttributeError:
pass
def setBuiltinKeywords(self):
"""Create pseudo keywords as part of builtins.
This sets "close", "exit" and "quit" to a helpful string.
"""
import __builtin__
__builtin__.close = __builtin__.exit = __builtin__.quit = \
'Click on the close button to leave the application.'
__builtin__.cd = cd
__builtin__.ls = ls
__builtin__.pwd = pwd
__builtin__.sx = sx
def quit(self):
"""Quit the application."""
# XXX Good enough for now but later we want to send a close event.
# In the close event handler we can make sure they want to
# quit. Other applications, like PythonCard, may choose to
# hide rather than quit so we should just post the event and
# let the surrounding app decide what it wants to do.
self.write('Click on the close button to leave the application.',
type='Output')
def setLocalShell(self):
"""Add 'slicesshell' to locals as reference to ShellFacade instance."""
self.interp.locals['slicesshell'] = SlicesShellFacade(other=self)
def execStartupScript(self, startupScript):
"""Execute the user's PYTHONSTARTUP script if they have one."""
if startupScript and os.path.isfile(startupScript):
text = 'Startup script executed: ' + startupScript
self.push('print %r; execfile(%r)' % (text, startupScript))
self.interp.startupScript = startupScript
else:
self.push('')
def about(self):
"""Display information about Py."""
text = DISPLAY_TEXT % \
(__author__, VERSION, self.revision, self.interp.revision,
sys.version.split()[0], wx.VERSION_STRING, str(wx.PlatformInfo),
sys.platform)
self.write(text.strip(),type='Output')
def BreakTextIntoCommands(self,text):
"""Turn a text block into multiple multi-line commands."""
#text = text.lstrip() # This should not be done!
text = self.fixLineEndings(text)
text = self.lstripPrompt(text)
text = text.replace(os.linesep, '\n')
lines = text.split('\n')
continuations = testForContinuations(text)
if len(continuations)==2: # Error case...
return None,continuations[1]
elif len(continuations)==4:
stringContinuationList,indentationBlockList, \
lineContinuationList,parentheticalContinuationList = continuations
commands = []
command = ''
for j,line in enumerate(lines):
lstrip = line.lstrip()
# Get the first alnum word:
first_word=[]
for i in lstrip:
if i.isalnum():
first_word.append(i)
else:
break
first_word = ''.join(first_word)
# Continue the command if it is blank, has indentation,
# starts with else, elif,except, or finally
# or previous line had a line continuation \
if j==0:
stringCont = False
lineCont=False
else:
stringCont = stringContinuationList[j-1]
lineCont = lineContinuationList[j-1]
if line.strip() == '' or lstrip != line or \
first_word in ['else','elif','except','finally'] or \
stringCont or lineCont:
# Multiline command. Add to the command.
command += '\n'
command += line
else:
# New command.
if command:
# Add the previous command to the list.
commands.append(command)
# Start a new command, which may be multiline.
command = line
commands.append(command)
return commands
def MarkerSet(self,line,markerBitsSet):
"""MarkerSet is the Set command for MarkerGet"""
markerBits=self.MarkerGet(line)
numMarkers=14
for i in range(numMarkers):
if (markerBitsSet & (1<<i)) and not (markerBits & (1<<i)):
self.MarkerAdd(line,i)
elif not (markerBitsSet & (1<<i)) and (markerBits & (1<<i)):
self.MarkerDelete(line,i)
def GetGroupingSlice(self,line_num=None):
"""Get the start/stop lines for the slice based on any line in the slice"""
if line_num==None:
line_num=self.GetCurrentLine()
num_lines=self.GetLineCount()
for i in range(line_num,-1,-1):
if self.MarkerGet(i) & (1<<GROUPING_START | 1<<GROUPING_START_FOLDED):
break
start_line=i
addition=0
for i in range(line_num,num_lines):
if self.MarkerGet(i) & 1<<GROUPING_END:
break
elif (i>line_num) and ( self.MarkerGet(i)
& (1<<GROUPING_START | 1<<GROUPING_START_FOLDED) ):
addition=-1
break # the solo case...
stop_line=i+addition
return start_line,stop_line
def GetIOSlice(self,line_num=None):
"""Get the start/stop lines for the slice based on any line in the slice"""
if line_num==None:
line_num=self.GetCurrentLine()
num_lines=self.GetLineCount()
for i in range(line_num,-1,-1):
if self.MarkerGet(i) & IO_ANY_START_MASK:
break
start_line=i
addition=0
for i in range(line_num,num_lines):
if self.MarkerGet(i) & IO_END_MASK:
break
elif (i>line_num) and (self.MarkerGet(i) & IO_ANY_START_MASK):
addition=-1
break # the solo case...
stop_line=i+addition
return start_line,stop_line
def FoldGroupingSlice(self,line_num=None):
if line_num==None:
line_num=self.GetCurrentLine()
start,end=self.GetGroupingSlice(line_num)
self.HideLines(start+1,end)
marker=self.MarkerGet(start)
self.clearGroupingMarkers(start)
self.MarkerAdd(start,GROUPING_START_FOLDED)
self.clearIOMarkers(start)
if marker & ( 1<<INPUT_START | 1<<INPUT_START_FOLDED ):
self.MarkerAdd(start,INPUT_START_FOLDED)
elif marker & ( 1<<OUTPUT_START | 1<<OUTPUT_START_FOLDED ):
self.MarkerAdd(start,OUTPUT_START_FOLDED)
self.MarkerAdd(start,OUTPUT_BG)
else:
pass #print 'Bad Markers!!!'
def FoldIOSlice(self,line_num=None):
if line_num==None:
line_num=self.GetCurrentLine()
start,end=self.GetIOSlice(line_num)
self.HideLines(start+1,end)
marker=self.MarkerGet(start)
if (self.MarkerGet(start) & \
(1<<GROUPING_START | 1<<GROUPING_START_FOLDED )) and \
(self.MarkerGet(end) & 1<<GROUPING_END):
self.clearGroupingMarkers(start)
self.MarkerAdd(start,GROUPING_START_FOLDED)
self.clearIOMarkers(start)
if marker & ( 1<<INPUT_START | 1<<INPUT_START_FOLDED ):
self.MarkerAdd(start,INPUT_START_FOLDED)
elif marker & ( 1<<OUTPUT_START | 1<<OUTPUT_START_FOLDED ):
self.MarkerAdd(start,OUTPUT_START_FOLDED)
self.MarkerAdd(start,OUTPUT_BG)
else:
pass #print 'Bad Markers!!!'
def UnFoldGroupingSlice(self,line_num=None):
if line_num==None:
line_num=self.GetCurrentLine()
start,end=self.GetGroupingSlice(line_num)
self.ShowLines(start+1,end)
self.clearGroupingMarkers(start)
self.MarkerAdd(start,GROUPING_START)
for i in range(start,end):
marker=self.MarkerGet(i)
if marker & (1<<INPUT_START | 1<<INPUT_START_FOLDED):
self.clearIOMarkers(i)
self.MarkerAdd(i,INPUT_START)
elif marker & (1<<OUTPUT_START | 1<<OUTPUT_START_FOLDED):
self.clearIOMarkers(i)
self.MarkerAdd(i,OUTPUT_START)
self.MarkerAdd(i,OUTPUT_BG)
def UnFoldIOSlice(self,line_num=None):
if line_num==None:
line_num=self.GetCurrentLine()
start,end=self.GetIOSlice(line_num)
self.ShowLines(start+1,end)
marker=self.MarkerGet(start)
if (self.MarkerGet(start) & \
(1<<GROUPING_START | 1<<GROUPING_START_FOLDED )) and \
(self.MarkerGet(end) & 1<<GROUPING_END):
self.clearGroupingMarkers(start)
self.MarkerAdd(start,GROUPING_START)
self.clearIOMarkers(start)
if marker & 1<<INPUT_START_FOLDED:
self.MarkerAdd(start,INPUT_START)
elif marker & 1<<OUTPUT_START_FOLDED:
self.MarkerAdd(start,OUTPUT_START)
self.MarkerAdd(start,OUTPUT_BG)
def DeleteOutputSlicesAfter(self,line_num=None):
"""Delete all outputs after an input"""
if line_num==None:
line_num=self.GetCurrentLine()
num_lines=self.GetLineCount()
if self.MarkerGet(line_num) & OUTPUT_MASK:
#print 'You can only run "DeleteOutputSlicesAfter" from an Input slice!'
return
startIn,endIn=self.GetIOSlice(line_num)
startGrouping,endGrouping=self.GetGroupingSlice(line_num)
if endIn<endGrouping:
self.SetSelection(self.PositionFromLine(endIn+1),
self.PositionFromLine(endGrouping+1))
self.ReplaceSelection('',sliceDeletion=True)
new_pos=self.GetLineEndPosition(line_num)
self.SetCurrentPos(new_pos)
self.SetSelection(new_pos,new_pos)
def SplitSlice(self,line_num=None):
if line_num==None:
line_num=self.GetCurrentLine()
start_num,end_num=self.GetIOSlice(line_num)
if self.MarkerGet(line_num) & INPUT_MASK:
type='Input'
start=INPUT_START
end=INPUT_END
splitGrouping=True
elif self.MarkerGet(line_num) & OUTPUT_MASK:
type='Output'
start=OUTPUT_START
end=OUTPUT_END
splitGrouping=False
if start_num==end_num:
return # Can't split one line!
elif start_num==line_num:
self.clearIOMarkers(line_num+1)
self.MarkerAdd(line_num+1,start)
if type=='Output': self.MarkerAdd(line_num+1,OUTPUT_BG)
if splitGrouping:
self.clearGroupingMarkers(line_num+1)
self.MarkerAdd(line_num+1,GROUPING_START)
else:
self.clearIOMarkers(line_num)
self.MarkerAdd(line_num,start)
if type=='Output': self.MarkerAdd(line_num,OUTPUT_BG)
if splitGrouping:
self.clearGroupingMarkers(line_num)
self.MarkerAdd(line_num,GROUPING_START)
if line_num-1>start_num:
self.clearIOMarkers(line_num-1)
self.MarkerAdd(line_num-1,end)
if type=='Output': self.MarkerAdd(line_num-1,OUTPUT_BG)
if splitGrouping:
self.clearGroupingMarkers(line_num-1)
self.MarkerAdd(line_num-1,GROUPING_END)
def BackspaceWMarkers(self,force=False):
# Warning: This is not good at checking for bad markers!
c_before=self.GetCharAt(self.GetCurrentPos() - 1)
c_after=self.GetCharAt(self.GetCurrentPos())
if c_before==0:
# Disallow deleting the first line or it will destroy the markers...
return False
elif c_before in (ord('\n'),ord('\r')):
line_num=self.GetCurrentLine()
marker=self.MarkerGet(line_num)
marker_before=self.MarkerGet(line_num-1)
marker_after=self.MarkerGet(line_num+1)
if marker_before & ( 1<<GROUPING_END ) :
return False # Disallow deleting lines between slices...
elif marker & ( 1<<GROUPING_START | 1<<GROUPING_START_FOLDED ) :
return False # Disallow deleting lines between slices...
else:
if marker_before & ( 1<<GROUPING_START | 1<<GROUPING_START_FOLDED ) :
self.clearGroupingMarkers(line_num)
elif marker & ( 1<<GROUPING_END ) :
self.clearGroupingMarkers(line_num-1)
if (marker_before & 1<<INPUT_END) and force:
# Special case for use in processLine
self.clearIOMarkers(line_num)
elif marker_before & (1<<INPUT_END | 1<<OUTPUT_END):
return False # Disallow deleting lines between slices...
elif marker & ( 1<<INPUT_START | 1<<INPUT_START_FOLDED ) :
return False # Disallow deleting lines between slices...
else:
if marker_before & (1<<INPUT_START | 1<<INPUT_START_FOLDED |
1<<OUTPUT_START | 1<<OUTPUT_START_FOLDED):
self.clearIOMarkers(line_num)
elif marker & ( 1<<INPUT_END | 1<<OUTPUT_END ) :
self.clearIOMarkers(line_num-1)
return True # If everything went well, return True and do the delete...
def ForwardDeleteWMarkers(self):
c_before=self.GetCharAt(self.GetCurrentPos() - 1)
c_after=self.GetCharAt(self.GetCurrentPos())
if c_after==0:
# Disallow deleting the first line or it will destroy the markers...
return False
elif c_after in (ord('\n'),ord('\r')):
line_num=self.GetCurrentLine()
marker=self.MarkerGet(line_num)
marker_before=self.MarkerGet(line_num-1)
marker_after=self.MarkerGet(line_num+1)
if marker & ( 1<<GROUPING_END ) :
return False # Disallow deleting lines between slices...
elif marker_after & ( 1<<GROUPING_START | 1<<GROUPING_START_FOLDED ) :
return False # Disallow deleting lines between slices...
else:
if marker & ( 1<<GROUPING_START | 1<<GROUPING_START_FOLDED ) :
self.clearGroupingMarkers(line_num+1)
elif marker_after & ( 1<<GROUPING_END ) :
self.clearGroupingMarkers(line_num)
if marker & ( 1<<INPUT_END | 1<<OUTPUT_END ) :
return False # Disallow deleting lines between slices...
elif marker_after & ( 1<<INPUT_START | 1<<INPUT_START_FOLDED ) :
return False # Disallow deleting lines between slices...
else:
if marker & (1<<INPUT_START | 1<<INPUT_START_FOLDED |
1<<OUTPUT_START | 1<<OUTPUT_START_FOLDED) :
self.clearIOMarkers(line_num+1)
elif marker_after & ( 1<<INPUT_END | 1<<OUTPUT_END ) :
self.clearIOMarkers(line_num)
return True
def GetIOSelection(self):
started=False
start=0
end=self.GetLineCount()-1
type=None
for i in range(self.GetLineCount()):
if self.MarkerGet(i) & 1<<IO_SELECTING:
if started==False:
start=i
if self.MarkerGet(i) & INPUT_MASK:
type='input'
elif self.MarkerGet(i) & OUTPUT_MASK:
type='output'
else:
if self.MarkerGet(i) & INPUT_MASK:
if type=='output':
end=i-1
break
elif self.MarkerGet(i) & OUTPUT_MASK:
if type=='input':
end=i-1
break
started=True
elif started==True:
end=i-1
break
if started==False:
#print 'No Selection!!'
self.SliceSelection=False
return start,end
def MergeAdjacentSlices(self):
"""This function merges all adjacent selected slices.\n""" + \
"""Right now, only IO Merging is allowed."""
started=False
start=0
end=self.GetLineCount()-1
type=None
for i in range(self.GetLineCount()):
if self.MarkerGet(i) & 1<<IO_SELECTING:
if started==False:
start=i
if self.MarkerGet(i) & INPUT_MASK:
type='input'
elif self.MarkerGet(i) & OUTPUT_MASK:
type='output'
else:
if self.MarkerGet(i) & INPUT_MASK:
if type=='output':
end=i-1
break
else:
self.clearIOMarkers(i)
self.clearGroupingMarkers(i)
self.MarkerAdd(i,INPUT_MIDDLE)
self.MarkerAdd(i,GROUPING_MIDDLE)
elif self.MarkerGet(i) & OUTPUT_MASK:
if type=='input':
end=i-1
break
else:
self.clearIOMarkers(i)
self.clearGroupingMarkers(i)
self.MarkerAdd(i,OUTPUT_MIDDLE)
self.MarkerAdd(i,OUTPUT_BG)
self.MarkerAdd(i,GROUPING_MIDDLE)
started=True
elif started==True:
end=i-1
break
if started and end!=start:
self.clearIOMarkers(end)
self.clearGroupingMarkers(end)
if type=='input':
self.MarkerAdd(end,INPUT_END)
if end+1<self.GetLineCount():
if self.MarkerGet(end+1) & OUTPUT_MASK:
self.MarkerAdd(end,GROUPING_MIDDLE)
else:
self.MarkerAdd(end,GROUPING_END)
else:
self.MarkerAdd(end,GROUPING_END)
else:
if self.MarkerGet(start) & 1<<GROUPING_END:
self.clearGroupingMarkers(start)
self.MarkerAdd(start,GROUPING_MIDDLE)
self.MarkerAdd(end,OUTPUT_END)
self.MarkerAdd(end,OUTPUT_BG)
self.MarkerAdd(end,GROUPING_END)
def SliceSelectionDelete(self):
"""Deletion of any selected and possibly discontinuous slices."""
if not self.SliceSelection:
return
# collect the line numbers to be deleted...
selectedSlices=[]
start,end=None,None
for i in range(self.GetLineCount()):
if self.MarkerGet(i) & (1<<GROUPING_SELECTING | 1<<IO_SELECTING):
if start==None:
start=i
end=i
elif start!=None:
selectedSlices.append([start,end])
start,end=None,None
if start!=None:
selectedSlices.append([start,end])
# Unselect everything
self.MarginUnselectAll()
self.SliceSelection=False
# Going in reverse, delete the selections, fixing the markers as we go...
for i in range(len(selectedSlices)-1,-1,-1):
self.SetSelection(self.PositionFromLine(selectedSlices[i][0]),
self.GetLineEndPosition(selectedSlices[i][1])+1)
markerNext = self.MarkerGet(selectedSlices[i][1]+1)
self.ReplaceSelection('',sliceDeletion=True)
cur_line=self.GetCurrentLine()
# If we've made a mess of the grouping markers, clean it up...
if ((self.MarkerGet(cur_line-1) & 1<<GROUPING_END) and
(self.MarkerGet(cur_line) & ( 1<<GROUPING_MIDDLE | 1<<GROUPING_END ) )):
self.clearGroupingMarkers(cur_line)
self.MarkerAdd(cur_line,GROUPING_START)
elif (( self.MarkerGet(cur_line-1) & 1<<GROUPING_MIDDLE ) and
( self.MarkerGet(cur_line) &
( 1<<GROUPING_START | 1<<GROUPING_START_FOLDED ) )):
self.clearGroupingMarkers(cur_line-1)
self.MarkerAdd(cur_line-1,GROUPING_END)
if markerNext & 1<<OUTPUT_START:
self.clearIOMarkers(cur_line)
self.MarkerAdd(cur_line,OUTPUT_START)
self.MarkerAdd(cur_line,OUTPUT_BG)
elif markerNext & 1<<OUTPUT_START_FOLDED:
self.clearIOMarkers(cur_line)
self.MarkerAdd(cur_line,OUTPUT_START_FOLDED)
self.MarkerAdd(cur_line,OUTPUT_BG)
return
def OnChar(self, event):
"""Keypress event handler.
Only receives an event if OnKeyDown calls event.Skip() for the
corresponding event."""
if self.noteMode:
event.Skip()
return
# Prevent modification of output slices
if not self.CanEdit():
return
key = event.GetKeyCode()
currpos = self.GetCurrentPos()
stoppos = self.PositionFromLine(self.GetCurrentLine())
# Return (Enter) needs to be ignored in this handler.
if key in [wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER]:
pass
elif key in self.autoCompleteKeys:
# Usually the dot (period) key activates auto completion.
# Get the command between the prompt and the cursor. Add
# the autocomplete character to the end of the command.
if self.AutoCompActive():
self.AutoCompCancel()
command = self.GetTextRange(stoppos, currpos) + chr(key)
# write with undo wrapper...
cpos=self.GetCurrentPos()
s=chr(key)
#ADD UNDO
self.UpdateUndoHistoryBefore('insert',s,cpos,cpos+len(s),
forceNewAction=False)
self.write(s,type='Input')
self.UpdateUndoHistoryAfter()
if self.autoComplete:
self.autoCompleteShow(command)
elif key == ord('('):
# The left paren activates a call tip and cancels an
# active auto completion.
if self.AutoCompActive():
self.AutoCompCancel()
# Get the command between the prompt and the cursor. Add
# the '(' to the end of the command.
self.ReplaceSelection('')
command = self.GetTextRange(stoppos, currpos) + '('
# write with undo wrapper...
cpos=self.GetCurrentPos()
s='('
self.UpdateUndoHistoryBefore('insert',s,cpos,cpos+len(s),
forceNewAction=True)
self.undoHistory[self.undoIndex]['allowsAppend']=True
self.write(s,type='Input')
self.UpdateUndoHistoryAfter()
self.autoCallTipShow(command,
self.GetCurrentPos() == self.GetTextLength())
else:
# Allow the normal event handling to take place.
# Use undo wrapper
cpos=self.GetCurrentPos()
try:
s=chr(key)
self.UpdateUndoHistoryBefore('insert',s,cpos,cpos+len(s))
event.Skip()
self.UpdateUndoHistoryAfter()
except:
event.Skip()
def AutoCompActiveCallback(self):
numChars=self.GetTextLength()-self.TotalLengthForAutoCompActiveCallback
if numChars==0:
self.undoIndex-=1
del(self.undoHistory[-1])
else:
uH=self.undoHistory
uI=self.undoIndex
cpos=uH[uI]['posStart']
s=''.join([chr(self.GetCharAt(cpos+i)) for i in range(numChars)])
s.replace(os.linesep,'\n')
self.undoHistory[self.undoIndex]['charList'] = s
self.undoHistory[self.undoIndex]['posEnd'] = cpos + numChars
self.undoHistory[self.undoIndex]['numLines'] = len(s.split('\n'))
self.UpdateUndoHistoryAfter()
def OnKeyDown(self, event):
"""Key down event handler."""
key = event.GetKeyCode()
# If the auto-complete window is up let it do its thing.
if self.AutoCompActive():
if key in [wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER]:
cpos=self.GetCurrentPos()
self.UpdateUndoHistoryBefore('insert','dummy',cpos,cpos+5,
forceNewAction=True)
self.undoHistory[self.undoIndex]['allowsAppend'] = True
self.TotalLengthForAutoCompActiveCallback=self.GetTextLength()
event.Skip()
wx.CallAfter(self.AutoCompActiveCallback)
if key in [wx.WXK_DELETE,wx.WXK_BACK]:
self.AutoCompCancel()
else:
event.Skip()
return
#DNM
# Prevent modification of output slices
controlDown = event.ControlDown()
altDown = event.AltDown()
shiftDown = event.ShiftDown()
currpos = self.GetCurrentPos()
endpos = self.GetTextLength()
selecting = self.GetSelectionStart() != self.GetSelectionEnd()
if key == wx.WXK_F12: #seb
if self.noteMode:
# self.promptPosStart not used anyway - or ?
## # We don't need to do this any more!
## self.promptPosEnd = self.PositionFromLine(self.GetLineCount()-1 ) +
## len(str(sys.ps1))
## self.GotoLine(self.GetLineCount())
## self.GotoPos(self.promptPosEnd)
## self.prompt() #make sure we have a prompt
self.SetCaretForeground("black")
self.SetCaretWidth(1) #default
self.SetCaretPeriod(500) #default
else:
self.SetCaretForeground("red")
self.SetCaretWidth(4)
self.SetCaretPeriod(0) #steady
self.noteMode = not self.noteMode
return
if self.noteMode:
event.Skip()
return
doLineBreak=False
doSubmitCommand=False
doPass=False
# Return is used to insert a line break.
# In Shell Mode, hit Return or Enter twice to submit a command
if ((not controlDown and not shiftDown and not altDown) and
key in [wx.WXK_RETURN,]):
if self.mode=='SlicesMode':
doLineBreak=True
elif self.mode=='ShellMode':
startLine,endLine = self.GetIOSlice()
startpos = self.PositionFromLine(startLine)
endpos = self.GetLineEndPosition(endLine)
command = self.GetTextRange(startpos, endpos)
strCont,indentBlock,lineCont,parenCont = testForContinuations(command,ignoreErrors=True)
lastLine = command.split('\n')[-1]
if lastLine.lstrip()=='': # all whitespace...
stillIndented=False
elif lastLine[0]==' ':
stillIndented=True
else:
stillIndented=False
if strCont[-1] or indentBlock[-1] or lineCont[-1] or \
parenCont[-1]:
doLineBreak=True
elif stillIndented:
new_pos=self.GetLineEndPosition(endLine)
self.SetCurrentPos(new_pos)
self.SetSelection(new_pos,new_pos)
doLineBreak=True
elif self.GetCurrentLine()!=endLine:
new_pos=self.GetLineEndPosition(endLine)
self.SetCurrentPos(new_pos)
self.SetSelection(new_pos,new_pos)
doPass = True
else:
doSubmitCommand=True
# Enter (Shift/Ctrl + Enter/Return) submits a command to the interpreter.
# In Shell Mode, hit Return or Enter twice to submit a command
elif ( key in [wx.WXK_NUMPAD_ENTER,] or
( (shiftDown or controlDown) and key in [wx.WXK_RETURN,
wx.WXK_NUMPAD_ENTER] ) ):
if self.mode=='SlicesMode':
doSubmitCommand=True
elif self.mode=='ShellMode':
doLineBreak=True
#Only relevant in ShellMode...
if doPass:
pass
elif doLineBreak or doSubmitCommand:
if self.CallTipActive():
self.CallTipCancel()
elif self.SliceSelection:
for i in range(self.GetLineCount()):
if self.MarkerGet(i) & 1<<GROUPING_SELECTING:
self.DoMarginClick(i, 2, shiftDown, controlDown)
break
elif self.MarkerGet(i) & 1<<IO_SELECTING:
self.DoMarginClick(i, 3, shiftDown, controlDown)
break
elif doLineBreak:
self.insertLineBreak()
#Only relevant in ShellMode...
elif doSubmitCommand:
self.DeleteOutputSlicesAfter()
self.processLine()
# Let Ctrl-Alt-* get handled normally.
elif controlDown and altDown:
event.Skip()
# Clear the current, unexecuted command.
elif key == wx.WXK_ESCAPE:
if self.CallTipActive():
event.Skip()
# Clear the current command
elif key == wx.WXK_BACK and controlDown and shiftDown:
self.clearCommand()
# Increase font size.
elif controlDown and key in (ord(']'), wx.WXK_NUMPAD_ADD):
dispatcher.send(signal='FontIncrease')
# Decrease font size.
elif controlDown and key in (ord('['), wx.WXK_NUMPAD_SUBTRACT):
dispatcher.send(signal='FontDecrease')
# Default font size.
elif controlDown and key in (ord('='), wx.WXK_NUMPAD_DIVIDE):
dispatcher.send(signal='FontDefault')
# Cut to the clipboard.
elif (controlDown and key in (ord('X'), ord('x'))) \
or (shiftDown and key == wx.WXK_DELETE):
self.Cut()
# Copy to the clipboard.
elif controlDown and not shiftDown \
and key in (ord('C'), ord('c'), wx.WXK_INSERT):
self.Copy()
# Copy to the clipboard, including prompts.
elif controlDown and shiftDown \
and key in (ord('C'), ord('c'), wx.WXK_INSERT):
self.CopyWithPrompts()
# Copy to the clipboard, including prefixed prompts.
elif altDown and not controlDown \
and key in (ord('C'), ord('c'), wx.WXK_INSERT):
self.CopyWithPromptsPrefixed()
# Home needs to be aware of the prompt.
elif controlDown and key == wx.WXK_HOME:
# Go to the beginning of the IO Slice
curLine = self.GetCurrentLine()
IOstart = self.GetIOSlice(curLine)[0]
home = self.PositionFromLine(IOstart)
if currpos == home and \
IOstart > 0:
home = self.PositionFromLine(self.GetIOSlice(curLine-1)[0])
self.SetCurrentPos(home)
if not selecting and not shiftDown:
self.SetAnchor(home)
self.EnsureCaretVisible()
elif controlDown and key == wx.WXK_END:
curLine = self.GetCurrentLine()
IOend = self.GetIOSlice(curLine)[1]
end = self.GetLineEndPosition(IOend)
if currpos == end and \
IOend < self.GetLineCount()-1:
end = self.GetLineEndPosition(self.GetIOSlice(curLine+1)[1])
self.SetCurrentPos(end)
if not selecting and not shiftDown:
self.SetAnchor(end)
self.EnsureCaretVisible()
elif controlDown and key == wx.WXK_PAGEUP:
pos=0
if currpos > pos:
self.SetCurrentPos(pos)
if not selecting and not shiftDown:
self.SetAnchor(pos)
self.EnsureCaretVisible()
elif controlDown and key == wx.WXK_PAGEDOWN:
pos = self.GetLineEndPosition(self.GetLineCount()-1)
if currpos < pos:
self.SetCurrentPos(pos)
if not selecting and not shiftDown:
self.SetAnchor(pos)
self.EnsureCaretVisible()
elif selecting and key not in NAVKEYS and not self.CanEdit():
pass
# Paste from the clipboard.
elif (controlDown and not shiftDown and key in (ord('V'), ord('v'))) \
or (shiftDown and not controlDown and key == wx.WXK_INSERT):
self.Paste()
# Paste from the clipboard, run commands.
elif controlDown and shiftDown and \
key in (ord('V'), ord('v')) and self.CanEdit():
self.PasteAndRun()
# Replace with the previous command from the history buffer.
elif (controlDown and not shiftDown and key == wx.WXK_UP) \
or (altDown and key in (ord('P'), ord('p'))) and self.CanEdit():
self.OnHistoryReplace(step=+1)
# Replace with the next command from the history buffer.
elif (controlDown and not shiftDown and key == wx.WXK_DOWN) \
or (altDown and key in (ord('N'), ord('n'))) and self.CanEdit():
self.OnHistoryReplace(step=-1)
# Insert the previous command from the history buffer.
elif (controlDown and shiftDown and key == wx.WXK_UP) and \
self.CanEdit():
self.OnHistoryInsert(step=+1)
# Insert the next command from the history buffer.
elif (controlDown and shiftDown and key == wx.WXK_DOWN) and \
self.CanEdit():
self.OnHistoryInsert(step=-1)
# Ctrl-Space shows Auto Completion
# Ctrl-Shift-Space shows CallTips
elif controlDown and key == wx.WXK_SPACE:
self.OnCallTipAutoCompleteManually(shiftDown)
# Ctrl+Shift+H is used to complete Text (from already typed words)
elif controlDown and shiftDown and key in [ord('H')]:
self.OnShowCompHistory()
# Search up the history for the text in front of the cursor.
elif key == wx.WXK_F8:
self.OnHistorySearch()
# Don't backspace over the latest non-continuation prompt.
elif key == wx.WXK_BACK:
if self.SliceSelection:
self.SliceSelectionDelete()
wx.CallAfter(self.RestoreFirstMarker)
elif selecting and self.CanEdit():
self.ReplaceSelection('')
#event.Skip()
elif self.CanEdit():
doDelete=True
cur_line=self.GetCurrentLine()
if not cur_line==0 and \
self.GetCurrentPos()==self.PositionFromLine(cur_line):
if self.MarkerGet(cur_line-1) & OUTPUT_MASK:
doDelete=False
if doDelete:
cpos=self.GetCurrentPos()
s=chr(self.GetCharAt(cpos-1))
self.UpdateUndoHistoryBefore('delete',s,cpos-1,cpos)
if self.BackspaceWMarkers():
event.Skip()
wx.CallAfter(self.RestoreFirstMarker)
elif key == wx.WXK_DELETE:
if self.SliceSelection:
self.SliceSelectionDelete()
wx.CallAfter(self.RestoreFirstMarker)
elif selecting and self.CanEdit():
self.ReplaceSelection('')
#event.Skip()
elif self.CanEdit():
doDelete=True
cur_line=self.GetCurrentLine()
if not cur_line==self.GetLineCount()-1 and \
self.GetCurrentPos()==self.GetLineEndPosition(cur_line):
if self.MarkerGet(cur_line+1) & OUTPUT_MASK:
doDelete=False
if doDelete:
cpos=self.GetCurrentPos()
s=chr(self.GetCharAt(cpos))
self.UpdateUndoHistoryBefore('delete',s,cpos,cpos+1)
if self.ForwardDeleteWMarkers():
event.Skip()
wx.CallAfter(self.RestoreFirstMarker)
# Only allow these keys after the latest prompt.
elif key == wx.WXK_TAB and self.CanEdit():
# use the same mechanism as with autocmplete...
cpos=self.GetCurrentPos()
self.UpdateUndoHistoryBefore('insert','dummy',cpos,cpos+5,
forceNewAction=True)
self.undoHistory[self.undoIndex]['allowsAppend'] = True
self.TotalLengthForAutoCompActiveCallback=self.GetTextLength()
event.Skip()
wx.CallAfter(self.AutoCompActiveCallback)
# Don't toggle between insert mode and overwrite mode.
elif key == wx.WXK_INSERT:
pass
# Don't allow line deletion.
#elif controlDown and key in (ord('L'), ord('l')):
# TODO : Allow line deletion eventually ??
#event.Skip()
# pass
# Don't allow line transposition.
# Toggle Shell Mode / Slices Mode
elif controlDown and key in (ord('T'), ord('t')):
self.ToggleShellMode()
#Open and Save now work when using CrustSlicesFrames
elif controlDown and key in (ord('L'), ord('l')):
#print 'Load it'
file=wx.FileSelector("Load File As New Slice")
if file!=u'':
fid=open(file,'r')
self.LoadPyFileAsSlice(fid)
fid.close()
elif controlDown and key in (ord('D'), ord('d')):
#Disallow line duplication in favor of divide slices
if self.MarkerGet(self.GetCurrentLine()) & INPUT_MASK:
#ADD UNDO
cpos=self.GetCurrentPos()
start,end = map(self.PositionFromLine,
self.GetGroupingSlice(self.LineFromPosition(cpos)))
self.UpdateUndoHistoryBefore('marker','',start,end,
forceNewAction=True)
self.SplitSlice()
# Turn off selecting
self.SetSelection(cpos,cpos)
self.ReplaceSelection('')
self.UpdateUndoHistoryAfter()
elif controlDown and key in (ord('M'), ord('m')):
#ADD UNDO
if self.SliceSelection:
cpos=self.GetCurrentPos()
ioSel=self.GetIOSelection()
if self.SliceSelection:
start,end = map(self.PositionFromLine,ioSel)
self.UpdateUndoHistoryBefore('marker','',start,end,
forceNewAction=True)
self.MergeAdjacentSlices()
# Turn off selecting
self.SetSelection(cpos,cpos)
self.ReplaceSelection('')
self.UpdateUndoHistoryAfter()
# Change arrow keys to allow margin behaviors...
elif self.SliceSelection and \
key in [wx.WXK_UP,wx.WXK_DOWN,wx.WXK_RIGHT,wx.WXK_LEFT]:
# TODO : This is useful, but not optimal!
if key==wx.WXK_UP:
for i in range(self.GetLineCount()):
if self.MarkerGet(i) & 1<<GROUPING_SELECTING:
if i>0: #Grouping
self.DoMarginClick(i-1, 2, shiftDown, controlDown)
break
elif self.MarkerGet(i) & 1<<IO_SELECTING:
if i>0: #IO
self.DoMarginClick(i-1, 3, shiftDown, controlDown)
break
elif key==wx.WXK_DOWN:
for i in range(self.GetLineCount()-1,-1,-1):
if self.MarkerGet(i) & 1<<GROUPING_SELECTING:
if i<self.GetLineCount()-1: #Grouping
self.DoMarginClick(i+1, 2, shiftDown, controlDown)
break
elif self.MarkerGet(i) & 1<<IO_SELECTING:
if i<self.GetLineCount()-1: #IO
self.DoMarginClick(i+1, 3, shiftDown, controlDown)
break
elif key==wx.WXK_RIGHT:
for i in range(self.GetLineCount()):
if self.MarkerGet(i) & 1<<GROUPING_SELECTING:
self.DoMarginClick(i, 3, shiftDown, controlDown)
break
elif self.MarkerGet(i) & 1<<IO_SELECTING:
self.MarginUnselectAll()
# Go to the beginning of the IO Slice
self.SetCurrentPos(self.PositionFromLine(i))
if not selecting and not shiftDown:
self.SetAnchor(self.PositionFromLine(i))
self.EnsureCaretVisible()
break
elif key==wx.WXK_LEFT:
for i in range(self.GetLineCount()):
if self.MarkerGet(i) & 1<<GROUPING_SELECTING:
break
elif self.MarkerGet(i) & 1<<IO_SELECTING:
self.DoMarginClick(i, 2, shiftDown, controlDown)
break
# Basic navigation keys should work anywhere.
elif key in NAVKEYS:
event.Skip()
# Protect the readonly portion of the slices shell.
elif not self.CanEdit():
pass
else:
# Check to see if we're selecting
if self.GetSelectionEnd()>self.GetSelectionStart():
# Check to see if a normal input took place
if not controlDown and not altDown and key<256:
self.ReplaceSelection('') # This seems to work...
event.Skip()
if self.SliceSelection:
if key not in [wx.WXK_UP,wx.WXK_DOWN,wx.WXK_RIGHT,wx.WXK_LEFT,
wx.WXK_ALT,wx.WXK_COMMAND,wx.WXK_CONTROL,wx.WXK_SHIFT]:
self.MarginUnselectAll()
def MarginSelectAll(self):
num_lines=self.GetLineCount()
for i in range(num_lines):
self.MarkerAdd(i,GROUPING_SELECTING)
self.MarkerDelete(i,IO_SELECTING)
def MarginUnselectAll(self):
num_lines=self.GetLineCount()
for i in range(num_lines):
self.MarkerDelete(i,IO_SELECTING)
self.MarkerDelete(i,GROUPING_SELECTING)
self.SliceSelection=False
def DoMarginClick(self, lineClicked, margin, shiftDown, controlDown):
num_lines=self.GetLineCount()
if margin==1:
pass # these events are not sent right now...
if margin==2:
self.SliceSelection=True
start,end=self.GetGroupingSlice(lineClicked)
startPos=self.PositionFromLine(start)
self.SetCurrentPos(startPos)
self.SetSelection(startPos,startPos)
start_marker=self.MarkerGet(start)
if self.MarkerGet(lineClicked) & 1<<GROUPING_SELECTING:
toggle=self.MarkerDelete
if not shiftDown:
if start_marker & 1<<GROUPING_START:
self.FoldGroupingSlice(lineClicked)
elif start_marker & 1<<GROUPING_START_FOLDED:
self.UnFoldGroupingSlice(lineClicked)
else:
toggle=self.MarkerAdd
if not shiftDown:
self.MarginUnselectAll()
for i in range(start,end+1):
toggle(i,GROUPING_SELECTING)
elif margin==3:
self.SliceSelection=True
start,end=self.GetIOSlice(lineClicked)
startPos=self.PositionFromLine(start)
self.SetCurrentPos(startPos)
self.SetSelection(startPos,startPos)
start_marker=self.MarkerGet(start)
if self.MarkerGet(lineClicked) & 1<<IO_SELECTING:
toggle=self.MarkerDelete
if not shiftDown:
if start_marker & IO_START_MASK:
self.FoldIOSlice(lineClicked)
elif start_marker & IO_START_FOLDED_MASK:
self.UnFoldIOSlice(lineClicked)
else:
toggle=self.MarkerAdd
if not shiftDown:
self.MarginUnselectAll()
for i in range(start,end+1):
toggle(i,IO_SELECTING)
#print start,end
elif margin==4:
# TODO : Folding ??
if 1:#self.MarkerGet(lineClicked) & ( 1<<7 | 1<<8 ):
if shiftDown:
self.SetFoldExpanded(lineClicked, True)
self.Expand(lineClicked, True, True, 1)
elif controlDown:
if self.GetFoldExpanded(lineClicked):
self.SetFoldExpanded(lineClicked, False)
self.Expand(lineClicked, False, True, 0)
else:
self.SetFoldExpanded(lineClicked, True)
self.Expand(lineClicked, True, True, 100)
else:
self.ToggleFold(lineClicked)
else:
self.MarginUnselectAll()
if margin in [2,3]:
if toggle==self.MarkerDelete and not shiftDown:
self.SliceSelection=False
else:
self.SliceSelection=True
def OnMarginClick(self, evt):
# fold and unfold as neededNAVKEYS
lineClicked = self.LineFromPosition(evt.GetPosition())
self.DoMarginClick(lineClicked,evt.GetMargin(),evt.GetShift(),evt.GetControl())
evt.Skip()
def OnShowCompHistory(self):
"""Show possible autocompletion Words from already typed words."""
#copy from history
his = self.history[:]
#put together in one string
joined = " ".join (his)
import re
#sort out only "good" words
newlist = re.split("[ \.\[\]=}(\)\,0-9\"]", joined)
#length > 1 (mix out "trash")
thlist = []
for i in newlist:
if len (i) > 1:
thlist.append (i)
#unique (no duplicate words
#oneliner from german python forum => unique list
unlist = [thlist[i] for i in xrange(len(thlist)) if thlist[i] not in thlist[:i]]
#sort lowercase
unlist.sort(lambda a, b: cmp(a.lower(), b.lower()))
#this is more convenient, isn't it?
self.AutoCompSetIgnoreCase(True)
#join again together in a string
stringlist = " ".join(unlist)
#pos von 0 noch ausrechnen
#how big is the offset?
cpos = self.GetCurrentPos() - 1
while chr (self.GetCharAt (cpos)).isalnum():
cpos -= 1
#the most important part
self.AutoCompShow(self.GetCurrentPos() - cpos -1, stringlist)
def ReplaceSelection(self,text,sliceDeletion=False,*args,**kwds):
startIO,endIO=self.GetIOSlice()
startGrouping,endGrouping=self.GetGroupingSlice()
startSel = self.LineFromPosition(self.GetSelectionStart())
endSel = self.LineFromPosition(self.GetSelectionEnd())
#ADD UNDO
cpos=self.GetSelectionStart()
s=self.GetSelectedText()
if s!='':
self.UpdateUndoHistoryBefore('delete',s,cpos,cpos+len(s),
forceNewAction=True)
editwindow.EditWindow.ReplaceSelection(self,'',*args,**kwds)
if s!='' and not sliceDeletion:
self.UpdateUndoHistoryAfter()
if endSel-startSel>0 and not sliceDeletion:
if endSel==endIO and startIO!=self.GetCurrentLine():
self.clearIOMarkers()
self.MarkerAdd(self.GetCurrentLine(),INPUT_END)
if endSel==endGrouping and startGrouping!=self.GetCurrentLine():
self.clearGroupingMarkers()
self.MarkerAdd(self.GetCurrentLine(),GROUPING_END)
cpos=self.GetSelectionStart()
s=text
if s!='':
self.UpdateUndoHistoryBefore('insert',s,cpos,cpos+len(s),
forceNewAction=True)
self.write(text)
self.UpdateUndoHistoryAfter()
self.ensureSingleGroupingMarker()
self.ensureSingleIOMarker()
def clearCommand(self):
"""Delete the current, unexecuted command."""
if not self.CanEdit():
return
start,end=self.GetIOSlice()
startpos = self.PositionFromLine(start)
endpos = self.GetLineEndPosition(end)
self.SetSelection(startpos, endpos)
self.ReplaceSelection('')
self.more = False
def OnHistoryReplace(self, step):
"""Replace with the previous/next command from the history buffer."""
if not self.CanEdit():
return
self.clearCommand()
self.replaceFromHistory(step)
def replaceFromHistory(self, step):
"""Replace selection with command from the history buffer."""
if not self.CanEdit():
return
self.ReplaceSelection('')
newindex = self.historyIndex + step
if -1 <= newindex <= len(self.history):
self.historyIndex = newindex
if 0 <= newindex <= len(self.history)-1:
command = self.history[self.historyIndex]
command = command.replace('\n', os.linesep)# + ps2)
self.ReplaceSelection(command)
def OnHistoryInsert(self, step):
"""Insert the previous/next command from the history buffer."""
if not self.CanEdit():
return
startpos = self.GetCurrentPos()
self.replaceFromHistory(step)
endpos = self.GetCurrentPos()
self.SetSelection(endpos, startpos)
# TODO: Fix Me!
def OnHistorySearch(self):
"""Search up the history buffer for the text in front of the cursor."""
if not self.CanEdit():
return
startpos = self.GetCurrentPos()
# The text up to the cursor is what we search for.
numCharsAfterCursor = self.GetTextLength() - startpos
searchText = self.getCommand(rstrip=False)
#print 'history search', startpos,numCharsAfterCursor,searchText
if numCharsAfterCursor > 0:
searchText = searchText[:-numCharsAfterCursor]
if not searchText:
return
# Search upwards from the current history position and loop
# back to the beginning if we don't find anything.
if (self.historyIndex <= -1) \
or (self.historyIndex >= len(self.history)-2):
searchOrder = range(len(self.history))
else:
searchOrder = range(self.historyIndex+1, len(self.history)) + \
range(self.historyIndex)
for i in searchOrder:
command = self.history[i]
if command[:len(searchText)] == searchText:
# Replace the current selection with the one we found.
self.ReplaceSelection(command[len(searchText):])
endpos = self.GetCurrentPos()
self.SetSelection(endpos, startpos)
# We've now warped into middle of the history.
self.historyIndex = i
break
def setStatusText(self, text):
"""Display status information."""
# This method will likely be replaced by the enclosing app to
# do something more interesting, like write to a status bar.
print text
def insertLineBreak(self):
"""Insert a new line break."""
if not self.CanEdit():
return
elif self.reader.isreading:
self.processLine()
return
# write with undo wrapper...
cpos=self.GetCurrentPos()
s=os.linesep
self.UpdateUndoHistoryBefore('insert',s,cpos,cpos+1)
self.write(s,type='Input')
self.UpdateUndoHistoryAfter()
self.more = True
self.prompt()
def processLine(self):
"""Process the line of text at which the user hit Enter or Shift+RETURN."""
# The user hit ENTER (Shift+RETURN) (Shift+ENTER) and we need to
# decide what to do. They could be sitting on any line in the slices shell.
thepos = self.GetCurrentPos()
cur_line = self.GetCurrentLine()
marker=self.MarkerGet(cur_line)
if marker & INPUT_MASK:
pass
elif marker & OUTPUT_MASK:
return
else:
pass #print 'BLANK LINE!!'
startline,endline=self.GetIOSlice(cur_line)
if startline==0:
startpos=0
else:
startpos=self.PositionFromLine(startline)
endpos=self.GetLineEndPosition(endline)
# If they hit ENTER inside the current command, execute the command.
if self.CanEdit():
self.SetCurrentPos(endpos)
self.interp.more = False
command = self.GetTextRange(startpos, endpos)
lines = command.split(os.linesep)
lines = [line.rstrip() for line in lines]
command = '\n'.join(lines)
if self.reader.isreading:
if not command:
# Match the behavior of the standard Python shell
# when the user hits return without entering a value.
command = '\n'
self.reader.input = command
self.write(os.linesep,'Input')
self.MarkerSet(self.GetCurrentLine(),READLINE_BG)
self.MarkerSet(self.GetCurrentLine(),INPUT_READLINE)
else:
self.runningSlice = (startline,endline)
self.push(command,useMultiCommand=True)
#print 'command: ',command
wx.FutureCall(1, self.EnsureCaretVisible)
self.runningSlice=None
skip=self.BackspaceWMarkers(force=True)
if skip:
self.DeleteBack()
if self.GetCurrentLine()==self.GetLineCount()-1:
self.write(os.linesep,type='Input')
cpos=self.GetCurrentLine()
if self.MarkerGet(cpos-1) & OUTPUT_MASK:
self.MarkerAdd(cpos-1,OUTPUT_BG)
self.SplitSlice()
else:
cur_line=self.GetCurrentLine()
new_pos=self.GetLineEndPosition(cur_line+1)
self.SetSelection(new_pos,new_pos)
self.SetCurrentPos(new_pos)
self.EmptyUndoBuffer()
self.NeedsCheckForSave=True
if self.hasSyntaxError:
pos=self.GetLineEndPosition(self.syntaxErrorRealLine)
self.SetCurrentPos(pos)
self.SetSelection(pos,pos)
# Not Used!!
def getMultilineCommand(self, rstrip=True):
"""Extract a multi-line command from the editor.
The command may not necessarily be valid Python syntax."""
# DNM
# XXX Need to extract real prompts here. Need to keep track of
# the prompt every time a command is issued.
text = self.GetCurLine()[0]
line = self.GetCurrentLine()
# Add Marker testing here...
while text == '' and line > 0: # Need to add markers handling...
line -= 1
self.GotoLine(line)
text = self.GetCurLine()[0]
if text=='':
line = self.GetCurrentLine()
self.GotoLine(line)
startpos = self.GetCurrentPos()
line += 1
self.GotoLine(line)
while self.GetCurLine()[0]=='':
line += 1
self.GotoLine(line)
stoppos = self.GetCurrentPos()
command = self.GetTextRange(startpos, stoppos)
command = command.replace(os.linesep, '\n')
command = command.rstrip()
command = command.replace('\n', os.linesep)
else:
command = ''
if rstrip:
command = command.rstrip()
return command
def getCommand(self, text=None, rstrip=True):
"""Extract a command from text which may include a shell prompt.
The command may not necessarily be valid Python syntax."""
if not text:
text = self.GetCurLine()[0]
# Strip the prompt off the front leaving just the command.
command = self.lstripPrompt(text)
# Change this -- Nothing has prompts!
#if command == text:
# command = '' # Real commands have prompts.
if rstrip:
command = command.rstrip()
return command
def lstripPrompt(self, text):
"""Return text without a leading prompt."""
ps1 = str(sys.ps1)
ps1size = len(ps1)
ps2 = str(sys.ps2)
ps2size = len(ps2)
# Strip the prompt off the front of text.
if text[:ps1size] == ps1:
text = text[ps1size:]
elif text[:ps2size] == ps2:
text = text[ps2size:]
return text
def push(self, command, silent = False,useMultiCommand=False):
"""Send command to the interpreter for execution."""
if not silent:
self.write(os.linesep,type='Output')
# TODO : What other magic might we insert here?
# TODO : Is there a good reason not to include magic?
if USE_MAGIC:
command=magic(command)
# Allows multi-component commands...
self.hasSyntaxError=False
if useMultiCommand:
result = self.BreakTextIntoCommands(command)
if result[0] == None:
commands=[command]
self.hasSyntaxError=True
syntaxErrorLine=result[1]+1
self.syntaxErrorRealLine = self.GetCurrentLine()+result[1]-len(command.split('\n'))
else:
commands=result
else:
commands=[command]
busy = wx.BusyCursor()
self.waiting = True
self.lastUpdate=None
for i in commands:
if self.hasSyntaxError:
lineno=syntaxErrorLine
offset=0 # not sure how to easily recover this information...
self.write(' File "<input>", line '+str(lineno)+'\n '+i.split('\n')[lineno-1]+'\n'+' '*offset+' ^\nSyntaxError: invalid syntax\n','Error')
else:
self.more = self.interp.push(i+'\n')
# (the \n stops many things from bouncing at the interpreter)
# I could do the following, but I don't really like it!
#if useMultiCommand:
# self.SplitSlice()
self.lastUpdate=None
if not silent:
self.MarkerAdd(self.GetIOSlice()[0],OUTPUT_BG)
self.waiting = False
del busy
if not self.more: # could loop-add to history, too, but I don't like it!
self.addHistory(command.rstrip())
if not silent:
self.prompt()
def addHistory(self, command):
"""Add command to the command history."""
# Reset the history position.
self.historyIndex = -1
# Insert this command into the history, unless it's a blank
# line or the same as the last command.
if command!='' and ( len(self.history)==0 or command!=self.history[0] ):
self.history.insert(0, command)
dispatcher.send(signal="SlicesShell.addHistory", command=command)
def clearGroupingMarkers(self,line_num=None):
if line_num==None:
line_num=self.GetCurrentLine()
self.MarkerDelete(line_num,GROUPING_START)
self.MarkerDelete(line_num,GROUPING_START_FOLDED)
self.MarkerDelete(line_num,GROUPING_MIDDLE)
self.MarkerDelete(line_num,GROUPING_END)
def clearIOMarkers(self,line_num=None):
if line_num==None:
line_num=self.GetCurrentLine()
self.MarkerDelete(line_num,INPUT_START)
self.MarkerDelete(line_num,INPUT_START_FOLDED)
self.MarkerDelete(line_num,INPUT_MIDDLE)
self.MarkerDelete(line_num,INPUT_END)
self.MarkerDelete(line_num,OUTPUT_START)
self.MarkerDelete(line_num,OUTPUT_START_FOLDED)
self.MarkerDelete(line_num,OUTPUT_MIDDLE)
self.MarkerDelete(line_num,OUTPUT_END)
self.MarkerDelete(line_num,OUTPUT_BG)
self.MarkerDelete(line_num,READLINE_BG)
self.MarkerDelete(line_num,INPUT_READLINE)
def ensureSingleGroupingMarker(self,line_num=None):
if line_num==None:
line_num=self.GetCurrentLine()
marker=self.MarkerGet(line_num)
if marker & 1<<GROUPING_START:
self.MarkerDelete(line_num,GROUPING_START_FOLDED)
self.MarkerDelete(line_num,GROUPING_MIDDLE)
self.MarkerDelete(line_num,GROUPING_END)
elif marker & 1<<GROUPING_START_FOLDED:
self.MarkerDelete(line_num,GROUPING_MIDDLE)
self.MarkerDelete(line_num,GROUPING_END)
elif marker & 1<<GROUPING_MIDDLE:
self.MarkerDelete(line_num,GROUPING_END)
elif marker & 1<<GROUPING_END:
pass
else:
#print 'ERROR! NO GROUPING MARKERS!'
return 1 # Blank marker
return 0
def ensureSingleIOMarker(self,line_num=None):
if line_num==None:
line_num=self.GetCurrentLine()
marker=self.MarkerGet(line_num)
if marker & INPUT_MASK:
self.MarkerDelete(line_num,OUTPUT_START)
self.MarkerDelete(line_num,OUTPUT_START_FOLDED)
self.MarkerDelete(line_num,OUTPUT_MIDDLE)
self.MarkerDelete(line_num,OUTPUT_END)
self.MarkerDelete(line_num,OUTPUT_BG)
[start,start_folded] = [INPUT_START,INPUT_START_FOLDED]
[middle,end] = [INPUT_MIDDLE,INPUT_END]
elif marker & OUTPUT_MASK:
self.MarkerDelete(line_num,INPUT_START)
self.MarkerDelete(line_num,INPUT_START_FOLDED)
self.MarkerDelete(line_num,INPUT_MIDDLE)
self.MarkerDelete(line_num,INPUT_END)
[start,start_folded] = [OUTPUT_START,OUTPUT_START_FOLDED]
[middle,end] = [OUTPUT_MIDDLE,OUTPUT_END]
else:
#print 'ERROR! NO IO MARKERS!'
return 1 # Blank marker
if marker & 1<<start:
self.MarkerDelete(line_num,start_folded)
self.MarkerDelete(line_num,middle)
self.MarkerDelete(line_num,end)
elif marker & 1<<start_folded:
self.MarkerDelete(line_num,middle)
self.MarkerDelete(line_num,end)
elif marker & 1<<middle:
self.MarkerDelete(line_num,end)
elif marker & 1<<end:
pass
return 0
def RestoreFirstMarker(self):
first_marker=self.MarkerGet(0)
self.clearGroupingMarkers(0)
self.clearIOMarkers(0)
if first_marker & 1<<GROUPING_START :
self.MarkerAdd(0,GROUPING_START)
elif first_marker & 1<<GROUPING_START_FOLDED :
self.MarkerAdd(0,GROUPING_START_FOLDED)
else:
self.MarkerAdd(0,GROUPING_START)
if first_marker & 1<<INPUT_START :
self.MarkerAdd(0,INPUT_START)
elif first_marker & 1<<INPUT_START_FOLDED :
self.MarkerAdd(0,INPUT_START_FOLDED)
elif first_marker & 1<<OUTPUT_START :
self.MarkerAdd(0,OUTPUT_START)
#self.MarkerAdd(0,OUTPUT_BG) # More harm than good??
elif first_marker & 1<<OUTPUT_START_FOLDED :
self.MarkerAdd(0,OUTPUT_START_FOLDED)
#self.MarkerAdd(0,OUTPUT_BG) # More harm than good??
else:
self.MarkerAdd(0,INPUT_START)
if self.doHistUpdate:
self.UpdateUndoHistoryAfter()
def IsAllowedPair(self,m1,m2):
"""This testing function ensures that two adjacent markers are valid"""
i_s = 1<<INPUT_START | 1<<INPUT_START_FOLDED
o_s = 1<<OUTPUT_START | 1<<OUTPUT_START_FOLDED
g_s = 1<<GROUPING_START | 1<<GROUPING_START_FOLDED
i_m,o_m,g_m = 1<<INPUT_MIDDLE, 1<<OUTPUT_MIDDLE, 1<<GROUPING_MIDDLE
i_e,o_e,g_e = 1<<INPUT_END, 1<<OUTPUT_END, 1<<GROUPING_END
if (m1 & i_s) and (m1 & g_s): #1
if (m2 & i_s) and (m2 & g_s): return True #1
elif (m2 & i_m) and (m2 & g_m): return True #2
elif (m2 & i_e) and (m2 & g_m): return True #3
elif (m2 & i_e) and (m2 & g_e): return True #4
elif (m2 & o_s) and (m2 & g_s): return False #5
elif (m2 & o_s) and (m2 & g_m): return True #6
elif (m2 & o_s) and (m2 & g_e): return True #7
elif (m2 & o_m) and (m2 & g_m): return False #8
elif (m2 & o_e) and (m2 & g_e): return False #9
else: return False
elif (m1 & i_m) and (m1 & g_m): #2
if (m2 & i_m) and (m2 & g_m): return True #2
elif (m2 & i_e) and (m2 & g_m): return True #3
elif (m2 & i_e) and (m2 & g_e): return True #4
else: return False
elif (m1 & i_e) and (m1 & g_m): #3
if (m2 & o_s) and (m2 & g_m): return True #6
elif (m2 & o_s) and (m2 & g_e): return True #7
else: return False
elif (m1 & i_e) and (m1 & g_e): #4
if (m2 & i_s) and (m2 & g_s): return True #1
elif (m2 & o_s) and (m2 & g_s): return True #5
else: return False
elif (m1 & o_s) and (m1 & g_s): #5
if (m2 & i_s) and (m2 & g_s): return True #1
elif (m2 & i_m) and (m2 & g_m): return False #2
elif (m2 & i_e) and (m2 & g_m): return False #3
elif (m2 & i_e) and (m2 & g_e): return False #4
elif (m2 & o_s) and (m2 & g_s): return True #5
elif (m2 & o_s) and (m2 & g_m): return False #6
elif (m2 & o_s) and (m2 & g_e): return False #7
elif (m2 & o_m) and (m2 & g_m): return True #8
elif (m2 & o_e) and (m2 & g_e): return True #9
else: return False
elif (m1 & o_s) and (m1 & g_m): #6
if (m2 & o_m) and (m2 & g_m): return True #8
elif (m2 & o_e) and (m2 & g_e): return True #9
else: return False
elif (m1 & o_s) and (m1 & g_e): #7
if (m2 & i_s) and (m2 & g_s): return True #1
elif (m2 & o_s) and (m2 & g_s): return True #5
else: return False
elif (m1 & o_m) and (m1 & g_m): #8
if (m2 & o_m) and (m2 & g_m): return True #8
elif (m2 & o_e) and (m2 & g_e): return True #9
else: return False
elif (m1 & o_e) and (m1 & g_e): #9
if (m2 & i_s) and (m2 & g_s): return True #1
elif (m2 & o_s) and (m2 & g_s): return True #5
else: return False
else:
return False
def CleanAllMarkers(self):
self.RestoreFirstMarker()
first_marker=self.MarkerGet(0)
last_line_num=self.GetLineCount()-1
for i in range(1,last_line_num):
self.ensureSingleGroupingMarker(i)
self.ensureSingleIOMarker(i)
previous_marker=self.MarkerGet(i-1)
marker=self.MarkerGet(i)
if not self.IsAllowedPair(previous_marker,marker):
pass # FIX MARKER!!
# FIX ME
def write(self, text,type='Input',silent=False):
"""Display text in the slices shell.
Replace line endings with OS-specific endings."""
text = self.fixLineEndings(text)
split=text.split(os.linesep)
self.AddText(text)
# This part handles all the marker stuff that accompanies
# adding or removing new lines of text...
# Get the total number of lines in the Document == last line number
last_line_num=self.GetLineCount()-1
# Get the line number we ended on in the write
end_line_num=self.GetCurrentLine()
# Get the number of returns we are using == number of lines we pasted -1
num_new_lines=text.count(os.linesep)
# So if num_new_lines==0, start_line_num and end_line_num are the same
start_line_num=end_line_num-num_new_lines+1
# This is a little unnecessary because there will always
# be a line before if we just inserted a newline!
if start_line_num == 0:
previous_line_num=None
else:
previous_line_num=start_line_num-1
#However, this is very important...
if end_line_num == last_line_num:
next_line_num=None
else:
next_line_num=end_line_num+1
if type=='Input':
start = INPUT_START
start_folded = INPUT_START_FOLDED
middle = INPUT_MIDDLE
end = INPUT_END
# preparation for more io types...
opposite_start_mask = 1<<OUTPUT_START
opposite_start_folded_mask = 1<<OUTPUT_START_FOLDED
opposite_middle_mask = 1<<OUTPUT_MIDDLE # To test for bad writes...
opposite_end_mask = 1<<OUTPUT_END # To test for bad writes...
elif type in ['Output','Error']:
#self.MarkerAdd(start_line_num,GROUPING_START_FOLDED)
start=OUTPUT_START
start_folded=OUTPUT_START_FOLDED
middle=OUTPUT_MIDDLE
end=OUTPUT_END
# preparation for more io types...
opposite_start_mask = 1<<INPUT_START
opposite_start_folded_mask = 1<<INPUT_START_FOLDED
opposite_middle_mask = 1<<INPUT_MIDDLE # To test for bad writes...
opposite_end_mask = 1<<INPUT_END # To test for bad writes...
if num_new_lines>0: #Do nothing if typing within a line...
# Update the Grouping Markers
# For the previous line and the start_line
# Test to make sure we can write ... but not here ...
# test this before we call write or before we add text...
# So we assume it already obeys the rules
badMarkers=False
fixIOEnd=True
if previous_line_num==None:
# This is an impossible case, here just for completeness...
self.clearGroupingMarkers(start_line_num)
self.MarkerAdd(start_line_num,GROUPING_START)
self.clearIOMarkers(start_line_num)
self.MarkerAdd(start_line_num,start)
if type in ['Output','Error']: self.MarkerAdd(start_line_num,OUTPUT_BG)
else:
previous_marker=self.MarkerGet(previous_line_num)
if previous_marker & opposite_middle_mask:
badMarkers=True
if next_line_num==None:
self.MarkerAdd(end_line_num,GROUPING_END)
self.MarkerAdd(end_line_num,end)
if type in ['Output','Error']: self.MarkerAdd(end_line_num,OUTPUT_BG)
fixEndMarkers=False
# May be overwritten below if start_line_num==end_line_num...
else:
next_marker=self.MarkerGet(next_line_num)
fixEndMarkers=True
if next_marker & ( opposite_middle_mask | opposite_end_mask ):
badMarkers=True
if not badMarkers:
# ensure previous_line only has one marker & turn end into middle
if previous_line_num!=None:
# Adjust previous line appropriately, ensure only one marker
# Only print errors if we are on input!
blank=False
blank=blank or self.ensureSingleGroupingMarker(previous_line_num)
blank=blank or self.ensureSingleIOMarker(previous_line_num)
if blank:
#if type=='Input' and not silent: print 'BLANK LINE!' # BAD CASE
pass
if previous_marker & 1<<GROUPING_END :
# Make GROUPING slice continue unless we hit
# an output end and are starting a new input...
if (previous_marker & OUTPUT_MASK) and type=='Input':
pass
else:
self.MarkerDelete(previous_line_num,GROUPING_END)
# ONLY CHANGING CASE
self.MarkerAdd(previous_line_num,GROUPING_MIDDLE)
if previous_marker & 1<<end :
self.MarkerDelete(previous_line_num,end)
self.MarkerAdd(previous_line_num,middle) # ONLY CHANGING CASE
if type in ['Output','Error']: self.MarkerAdd(previous_line_num,OUTPUT_BG)
elif previous_marker & opposite_middle_mask :
# BAD CASE
if type=='Input' and not silent:
#print 'Should have been a bad marker!'
pass
# We can only add input to an input slice
# And can only add output to an output slice
if previous_marker & ( opposite_start_mask |
opposite_start_folded_mask |
opposite_end_mask ):
if type=='Input':
self.clearGroupingMarkers(start_line_num)
self.MarkerAdd(start_line_num,GROUPING_START)
if start_line_num==end_line_num:
fixEndMarkers=False
else:
if start_line_num==end_line_num:
fixIOEnd=False
self.clearIOMarkers(start_line_num)
self.MarkerAdd(start_line_num,start)
if type in ['Output','Error']: self.MarkerAdd(start_line_num,OUTPUT_BG)
else:
if next_line_num!=None:
self.clearGroupingMarkers(start_line_num)
self.clearIOMarkers(start_line_num)
self.MarkerAdd(start_line_num,GROUPING_MIDDLE)
self.MarkerAdd(start_line_num,middle)
if type in ['Output','Error']: self.MarkerAdd(start_line_num,OUTPUT_BG)
# This may be overwritten if start_line_num==end_line_num
# Take care of all the middle lines...
# Does nothing for only one line...
for i in range(start_line_num,end_line_num):
self.clearGroupingMarkers(i)
self.MarkerAdd(i,GROUPING_MIDDLE)
self.clearIOMarkers(i)
self.MarkerAdd(i,middle)
if type in ['Output','Error']: self.MarkerAdd(i,OUTPUT_BG)
if fixEndMarkers:
# Take care of the end_line if we haven't already done so...
blank=False
blank=blank or self.ensureSingleGroupingMarker(next_line_num)
blank=blank or self.ensureSingleIOMarker(next_line_num)
if blank:
if type=='Input' and not silent:
#print 'BLANK LINE!' # BAD CASE
pass
self.clearGroupingMarkers(end_line_num)
if fixIOEnd:
self.clearIOMarkers(end_line_num)
if next_marker & ( 1<<GROUPING_START | 1<<GROUPING_START_FOLDED ) :
self.MarkerAdd(end_line_num,GROUPING_END)
elif next_marker & ( 1<<GROUPING_MIDDLE | 1<<GROUPING_END ) :
self.MarkerAdd(end_line_num,GROUPING_MIDDLE)
if fixIOEnd:
if next_marker & ( 1<<start | 1<<start_folded ) :
self.MarkerAdd(end_line_num,end)
if type in ['Output','Error']: self.MarkerAdd(end_line_num,OUTPUT_BG)
elif next_marker & ( 1<<middle | 1<<end ) :
self.MarkerAdd(end_line_num,middle)
if type in ['Output','Error']: self.MarkerAdd(end_line_num,OUTPUT_BG)
elif next_marker & ( opposite_start_mask |
opposite_start_folded_mask ):
self.MarkerAdd(end_line_num,end)
if type in ['Output','Error']: self.MarkerAdd(end_line_num,OUTPUT_BG)
else:
self.MarkerAdd(end_line_num,start_folded)
if type in ['Output','Error']: self.MarkerAdd(end_line_num,OUTPUT_BG)
if type=='Input' and not silent:
#print 'BAD MARKERS!'
pass
else:
if type=='Input' and not silent:
#print 'BAD MARKERS!!!'
pass
self.EnsureCaretVisible()
if self.waiting:
if self.lastUpdate==None:
self.lastUpdate=time.time()
if time.time()-self.lastUpdate > PRINT_UPDATE_MAX_TIME:
self.Update()
self.lastUpdate=time.time()
def fixLineEndings(self, text):
"""Return text with line endings replaced by OS-specific endings."""
lines = text.split('\r\n')
for l in range(len(lines)):
chunks = lines[l].split('\r')
for c in range(len(chunks)):
chunks[c] = os.linesep.join(chunks[c].split('\n'))
lines[l] = os.linesep.join(chunks)
text = os.linesep.join(lines)
return text
def prompt(self): # Autoindent added!!!
"""Display proper prompt for the context: ps1, ps2 or ps3.
If this is a continuation line, autoindent as necessary."""
# TODO : How much of this can I do away with now without prompts??
isreading = self.reader.isreading
skip = True
if isreading:
prompt = str(sys.ps3)
elif self.more:
prompt = str(sys.ps2)
else:
prompt = str(sys.ps1)
pos = self.GetCurLine()[1]
if pos > 0:
if isreading:
skip = True
else:
self.write(os.linesep,type='Input')
if not self.more:
# Not needed anymore! # self.promptPosStart = self.GetCurrentPos()
pass
if not skip:
self.write(prompt,type='Input')
if not self.more:
# Not needed anymore! # self.promptPosEnd = self.GetCurrentPos()
# Clear the undo history after running a command.
self.EmptyUndoBuffer()
#DNM/CP
# Autoindent magic
# Match the indent of the line above
# UNLESS the line above ends in a colon...then add four spaces
# (after valid keywords (if, else, etc...) only)
if self.more:
line_num=self.GetCurrentLine()
currentLine=self.GetLine(line_num)
previousLine=self.GetLine(line_num-1)
pstrip=previousLine.strip()
lstrip=previousLine.lstrip()
if pstrip == '':
# because it is all whitespace!
indent=previousLine.strip('\n').strip('\r')
else:
indent=previousLine[:(len(previousLine)-len(lstrip))]
if testForContinuations(previousLine,ignoreErrors=True)[1][0]:
indent+=' '*4
#ADD UNDO
cpos=self.GetCurrentPos()
s=indent
self.UpdateUndoHistoryBefore('insert',s,cpos,cpos+len(s))
self.write(s,type='Input')
self.UpdateUndoHistoryAfter()
self.EnsureCaretVisible()
self.ScrollToColumn(0)
def readline(self):
"""Replacement for stdin.readline()."""
input = ''
reader = self.reader
reader.isreading = True
self.prompt()
# Ensure that we get a new line and that it's got an input marker...
# Also need to temporarily block any other action...
cLine = self.GetCurrentLine()
self.clearIOMarkers(cLine)
self.MarkerAdd(cLine,INPUT_START)
self.MarkerAdd(cLine,READLINE_BG)
self.MarkerAdd(cLine,INPUT_READLINE)
try:
while not reader.input:
wx.YieldIfNeeded()
input = reader.input
finally:
start,end = self.GetIOSlice()
start = self.runningSlice[1] + 1
for i in range(start,end+1):
self.clearIOMarkers(i)
self.clearGroupingMarkers(i)
self.MarkerAdd(i,OUTPUT_BG)
if i == start: self.MarkerAdd(i,OUTPUT_START)
elif i==end: self.MarkerAdd(i,OUTPUT_END)
else: self.MarkerAdd(i,OUTPUT_MIDDLE)
if i==end: self.MarkerAdd(i,GROUPING_END)
else: self.MarkerAdd(i,GROUPING_MIDDLE)
reader.input = ''
reader.isreading = False
input = str(input) # In case of Unicode.
return input
def readlines(self):
"""Replacement for stdin.readlines()."""
lines = []
while lines[-1:] != ['\n']:
lines.append(self.readline())
return lines
def raw_input(self, prompt=''):
"""Return string based on user input."""
if prompt:
self.write(prompt,type='Output')
return self.readline()
def ask(self, prompt='Please enter your response:'):
"""Get response from the user using a dialog box."""
dialog = wx.TextEntryDialog(None, prompt,
'Input Dialog (Raw)', '')
try:
if dialog.ShowModal() == wx.ID_OK:
text = dialog.GetValue()
return text
finally:
dialog.Destroy()
return ''
def pause(self):
"""Halt execution pending a response from the user."""
self.ask('Press enter to continue:')
def clear(self):
"""Delete all text from the slices shell."""
self.ClearAll()
self.MarkerAdd(0,GROUPING_START)
self.MarkerAdd(0,INPUT_START)
def run(self, command, prompt=True, verbose=True):
"""Execute command as if it was typed in directly.
>>> shell.run('print "this"')
>>> print "this"
this
>>>
"""
# Go to the very bottom of the text.
endpos = self.GetTextLength()
self.SetCurrentPos(endpos)
command = command.rstrip()
if prompt: self.prompt()
if verbose: self.write(command,type='Input')
self.push(command)
# TODO : Will have to fix this to handle other kinds of errors mentioned before...
def runfile(self, filename):
"""Execute all commands in file as if they were typed into the shell."""
file = open(filename)
try:
self.prompt()
for command in file.readlines():
if command[:6] == 'shell.':
# Run shell methods silently.
self.run(command, prompt=False, verbose=False)
else:
self.run(command, prompt=False, verbose=True)
finally:
file.close()
def autoCompleteShow(self, command, offset = 0):
"""Display auto-completion popup list."""
self.AutoCompSetAutoHide(self.autoCompleteAutoHide)
self.AutoCompSetIgnoreCase(self.autoCompleteCaseInsensitive)
list = self.interp.getAutoCompleteList(command,
includeMagic=self.autoCompleteIncludeMagic,
includeSingle=self.autoCompleteIncludeSingle,
includeDouble=self.autoCompleteIncludeDouble)
if list:
options = ' '.join(list)
#offset = 0
self.AutoCompShow(offset, options)
def autoCallTipShow(self, command, insertcalltip = True, forceCallTip = False):
"""Display argument spec and docstring in a popup window."""
if self.CallTipActive():
self.CallTipCancel()
(name, argspec, tip) = self.interp.getCallTip(command)
if tip:
dispatcher.send(signal='SlicesShell.calltip', sender=self, calltip=tip)
if not self.autoCallTip and not forceCallTip:
return
startpos = self.GetCurrentPos()
if argspec and insertcalltip and self.callTipInsert:
# write with undo history...
cpos=self.GetCurrentPos()
s=argspec + ')'
self.UpdateUndoHistoryBefore('insert',s,cpos,cpos+len(s))
self.write(s,type='Input')
self.UpdateUndoHistoryAfter()
endpos = self.GetCurrentPos()
self.SetSelection(startpos, endpos)
if tip:
tippos = startpos - (len(name) + 1)
fallback = startpos - self.GetColumn(startpos)
# In case there isn't enough room, only go back to the fallback.
tippos = max(tippos, fallback)
self.CallTipShow(tippos, tip)
def OnCallTipAutoCompleteManually (self, shiftDown):
"""AutoComplete and Calltips manually."""
if self.AutoCompActive():
self.AutoCompCancel()
currpos = self.GetCurrentPos()
stoppos = self.PositionFromLine(self.GetIOSlice()[0])
cpos = currpos
#go back until '.' is found
pointavailpos = -1
while cpos >= stoppos:
if self.GetCharAt(cpos) == ord ('.'):
pointavailpos = cpos
break
cpos -= 1
#word from non whitespace until '.'
if pointavailpos != -1:
#look backward for first whitespace char
textbehind = self.GetTextRange (pointavailpos + 1, currpos)
pointavailpos += 1
if not shiftDown:
#call AutoComplete
stoppos = self.PositionFromLine(self.GetIOSlice()[0])
textbefore = self.GetTextRange(stoppos, pointavailpos)
self.autoCompleteShow(textbefore, len (textbehind))
else:
#call CallTips
cpos = pointavailpos
begpos = -1
while cpos > stoppos:
if chr(self.GetCharAt(cpos)).isspace():
begpos = cpos
break
cpos -= 1
if begpos == -1:
begpos = cpos
ctips = self.GetTextRange (begpos, currpos)
ctindex = ctips.find ('(')
if ctindex != -1 and not self.CallTipActive():
#insert calltip, if current pos is '(', otherwise show it only
self.autoCallTipShow( ctips[:ctindex + 1],
self.GetCharAt(currpos - 1) == ord('(') and
self.GetCurrentPos() == self.GetTextLength(),
True )
def writeOut(self, text):
"""Replacement for stdout."""
self.write(text,type='Output')
# TODO : FLUSH?? How to make this update real-time...
def writeErr(self, text):
"""Replacement for stderr."""
self.write(text,type='Error')
def redirectStdin(self, redirect=True):
"""If redirect is true then sys.stdin will come from the shell."""
if redirect:
sys.stdin = self.reader
else:
sys.stdin = self.stdin
def redirectStdout(self, redirect=True):
"""If redirect is true then sys.stdout will go to the shell."""
if redirect:
sys.stdout = PseudoFileOut(self.writeOut)
else:
sys.stdout = self.stdout
def redirectStderr(self, redirect=True):
"""If redirect is true then sys.stderr will go to the shell."""
if redirect:
sys.stderr = PseudoFileErr(self.writeErr)
else:
sys.stderr = self.stderr
# Take a spashot of the WHOLE grouping slice (or slices)
# The argument s is either what got added or deleted
def UpdateUndoHistoryBefore(self,actionType,s,posStart,posEnd,
forceNewAction=False):
uH=self.undoHistory
uI=self.undoIndex
s=s.replace(os.linesep,'\n')
startLine=self.LineFromPosition(posStart)
if actionType=='marker':
numLines = self.LineFromPosition(posEnd) - startLine
else:
numLines=s.count('\n')
makeNewAction=forceNewAction
if forceNewAction:
makeNewAction=True
elif self.undoIndex==-1:
makeNewAction=True
elif not uH[uI]['allowsAppend']:
makeNewAction=True
elif actionType!=uH[uI]['actionType']:
makeNewAction=True
elif actionType=='insert':
if posStart!=uH[uI]['posEnd']:
makeNewAction=True
else: # This is a continuation of the previous insert
uH[uI]['charList'] = uH[uI]['charList']+s
uH[uI]['posEnd'] = posEnd # posStart cannot move
uH[uI]['numLines'] = uH[uI]['numLines']+numLines
elif actionType=='delete':
# This is a forward continuation of the previous delete
if posStart==uH[uI]['posStart']:
uH[uI]['charList'] = uH[uI]['charList']+s
uH[uI]['posEnd'] = posEnd
uH[uI]['numLines'] = uH[uI]['numLines']+numLines
# This is a backward continuation of the previous delete
elif posEnd==uH[uI]['posStart']:
uH[uI]['charList'] = s+uH[uI]['charList']
uH[uI]['posStart'] = posStart
uH[uI]['startLine'] = startLine
uH[uI]['numLines'] = uH[uI]['numLines']+numLines
else:
makeNewAction=True
elif actionType=='marker':
makeNewAction=True
else:
pass #print 'Unsupported Action Type!!'
if makeNewAction:
del(self.undoHistory[uI+1:]) # remove actions after undoIndex
uH.append({
'actionType' : actionType, # Action type ('insert','delete','marker')
'allowsAppend': not forceNewAction, # Can action be joined with others?
'charList' : s, # Character list
'posStart' : posStart, # Cursor poition at the start of the action
'posEnd' : posEnd, # Cursor position at the end of the action
'startLine' : startLine, # Start line number,
'numLines' : numLines, # Number of newlines involved
'mBStart' : None, # Starting line for markers BEFORE action
'mAStart' : None, # Starting line for markers AFTER action
'markersBefore' : None, # [markers BEFORE action]
'markersAfter' : None # [markers AFTER action]
})
self.undoIndex+=1
# Only update the before when starting a new action
start = startLine
if actionType=='insert':
end = start
else:
end = start + numLines
# Update Marker Info
newStart=self.GetGroupingSlice(start)[0]
newEnd=self.GetGroupingSlice(end)[1]
self.undoHistory[self.undoIndex]['markersBefore'] = \
[self.MarkerGet(i) for i in range(newStart,newEnd+1)]
self.undoHistory[self.undoIndex]['mBStart']=newStart
self.doHistUpdate=True
def UpdateUndoHistoryAfter(self): # s is either what got added or deleted
start = self.undoHistory[self.undoIndex]['startLine']
if self.undoHistory[self.undoIndex]['actionType']=='delete':
end = start
else:
end = start + self.undoHistory[self.undoIndex]['numLines']
newStart=min(self.GetGroupingSlice(start)[0]-1, 0)
newEnd=max(self.GetGroupingSlice(end)[1]+1, self.GetLineCount()-1)
self.undoHistory[self.undoIndex]['markersAfter'] = \
[self.MarkerGet(i) for i in range(newStart,newEnd+1)]
self.undoHistory[self.undoIndex]['mAStart']=newStart
self.doHistUpdate=False
def Undo(self):
#ADD UNDO
#Skip undo if there are no actions...
if self.undoIndex==-1:
return
uHI=self.undoHistory[self.undoIndex]
if uHI['actionType'] in ['insert','delete']:
# This will perform the opposite of the action given
editwindow.EditWindow.Undo(self)
elif uHI['actionType']=='marker': # No text changed, don't pass to STC
pass
else:
#print 'Unsupported actionType in undoHistory!!'
return
numLines=len(uHI['markersBefore'])
for i in range(numLines):
self.MarkerSet( uHI['mBStart']+i , uHI['markersBefore'][i] )
self.undoIndex-=1
def Redo(self):
#ADD UNDO
# First check to see if there are any redo operations available
# Note that for redo, undoIndex=-1 is a valid input
if self.undoIndex >= len(self.undoHistory)-1:
return
self.undoIndex+=1
uHI=self.undoHistory[self.undoIndex]
if uHI['actionType'] in ['insert','delete']:
# This will re-perform the given action
editwindow.EditWindow.Redo(self)
elif uHI['actionType']=='marker': # No text changed, don't pass to STC
pass
else:
#print 'Unsupported actionType in undoHistory!!'
return
numLines=len(uHI['markersAfter'])
for i in range(numLines):
self.MarkerSet( uHI['mAStart']+i , uHI['markersAfter'][i] )
def EmptyUndoBuffer(self):
editwindow.EditWindow.EmptyUndoBuffer(self)
self.undoIndex=-1
self.undoHistory=[]
self.doHistUpdate=False
def CanCut(self):
return self.CanEdit() and \
(self.GetSelectionStart() != self.GetSelectionEnd())
def CanPaste(self):
"""Return true if a paste should succeed."""
if self.CanEdit() and editwindow.EditWindow.CanPaste(self):
return True
else:
return False
def CanEdit(self):
"""Return true if editing should succeed."""
marker=self.MarkerGet(self.GetCurrentLine())
if marker & OUTPUT_MASK:
return False
elif marker & INPUT_MASK:
if self.reader.isreading and not \
(self.MarkerGet(self.GetCurrentLine()) & 1<<INPUT_READLINE ):
return False
start,end=self.GetIOSlice()
sliceStartPos=self.PositionFromLine(start)
sliceEndPos=self.GetLineEndPosition(end)
"""Return true if text is selected and can be cut."""
if self.GetSelectionStart() == self.GetSelectionEnd():
return True
elif self.GetSelectionStart() != self.GetSelectionEnd() \
and self.GetSelectionStart() >= sliceStartPos \
and self.GetSelectionEnd() >= sliceStartPos \
and self.GetSelectionStart() <= sliceEndPos \
and self.GetSelectionEnd() <= sliceEndPos:
return True
else:
return False
def Cut(self):
"""Remove selection and place it on the clipboard."""
if self.CanCut() and self.CanCopy():
if self.AutoCompActive():
self.AutoCompCancel()
if self.CallTipActive():
self.CallTipCancel()
self.Copy()
self.ReplaceSelection('')
def Copy(self):
"""Copy selection and place it on the clipboard."""
if self.CanCopy():
ps1 = str(sys.ps1)
ps2 = str(sys.ps2)
command = self.GetSelectedText()
command = command.replace(os.linesep + ps2, os.linesep)
command = command.replace(os.linesep + ps1, os.linesep)
command = self.lstripPrompt(text=command)
data = wx.TextDataObject(command)
self._clip(data)
def CopyWithPrompts(self):
"""Copy selection, including prompts, and place it on the clipboard."""
if self.CanCopy():
command = self.GetSelectedText()
data = wx.TextDataObject(command)
self._clip(data)
def CopyWithPromptsPrefixed(self):
"""Copy selection, including prompts prefixed with four
spaces, and place it on the clipboard."""
if self.CanCopy():
command = self.GetSelectedText()
spaces = ' ' * 4
command = spaces + command.replace(os.linesep,
os.linesep + spaces)
data = wx.TextDataObject(command)
self._clip(data)
def _clip(self, data):
if wx.TheClipboard.Open():
wx.TheClipboard.UsePrimarySelection(False)
wx.TheClipboard.SetData(data)
wx.TheClipboard.Flush()
wx.TheClipboard.Close()
def Paste(self):
"""Replace selection with clipboard contents."""
#ADD UNDO
if self.CanPaste() and wx.TheClipboard.Open():
ps2 = str(sys.ps2)
if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)):
data = wx.TextDataObject()
if wx.TheClipboard.GetData(data):
self.ReplaceSelection('')
command = data.GetText()
command = command.rstrip()
command = self.fixLineEndings(command)
command = self.lstripPrompt(text=command)
# TODO : This is still useful... Add it back other places?
command = command.replace(os.linesep + ps2, '\n')
command = command.replace(os.linesep, '\n')
#DNM--Don't use '... '
command = command.replace('\n', os.linesep)# + ps2)
cpos=self.GetCurrentPos()
s=command
self.UpdateUndoHistoryBefore('insert', s, cpos,
cpos+len(s), forceNewAction=True)
self.write(s,type='Input')
self.UpdateUndoHistoryAfter()
# Makes paste -> type -> undo consistent with other STC apps
self.ReplaceSelection('')
wx.TheClipboard.Close()
def PasteAndRun(self):
"""Replace selection with clipboard contents, run commands."""
text = ''
if wx.TheClipboard.Open():
if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)):
data = wx.TextDataObject()
if wx.TheClipboard.GetData(data):
text = data.GetText()
wx.TheClipboard.Close()
if text:
self.Execute(text)
def Execute(self, text):
"""Replace selection with text and run commands."""
start,end=self.GetIOSlice()
startpos=self.PositionFromLine(start)
endpos=self.GetLineEndPosition(end)
self.SetCurrentPos(endpos)
self.SetSelection(startpos, endpos)
self.ReplaceSelection('')
hasSyntaxError=False
result = self.BreakTextIntoCommands(command)
if result[0] == None:
commands=[command]
hasSyntaxError=True
else:
commands=result
for command in commands:
command = command.replace('\n', os.linesep)
self.write(command)
self.processLine()
def wrap(self, wrap=True):
"""Sets whether text is word wrapped."""
try:
self.SetWrapMode(wrap)
except AttributeError:
return 'Wrapping is not available in this version.'
def zoom(self, points=0):
"""Set the zoom level.
This number of points is added to the size of all fonts. It
may be positive to magnify or negative to reduce."""
self.SetZoom(points)
def LoadSettings(self, config):
self.autoComplete = \
config.ReadBool('Options/AutoComplete', True)
self.autoCompleteIncludeMagic = \
config.ReadBool('Options/AutoCompleteIncludeMagic', True)
self.autoCompleteIncludeSingle = \
config.ReadBool('Options/AutoCompleteIncludeSingle', True)
self.autoCompleteIncludeDouble = \
config.ReadBool('Options/AutoCompleteIncludeDouble', True)
self.autoCallTip = \
config.ReadBool('Options/AutoCallTip', True)
self.callTipInsert = \
config.ReadBool('Options/CallTipInsert', True)
self.SetWrapMode(config.ReadBool('View/WrapMode', True))
self.lineNumbers = \
config.ReadBool('View/ShowLineNumbers', True)
self.setDisplayLineNumbers (self.lineNumbers)
zoom = config.ReadInt('View/Zoom/Shell', -99)
if zoom != -99:
self.SetZoom(zoom)
def SaveSettings(self, config):
config.WriteBool('Options/AutoComplete', self.autoComplete)
config.WriteBool('Options/AutoCompleteIncludeMagic',
self.autoCompleteIncludeMagic)
config.WriteBool('Options/AutoCompleteIncludeSingle',
self.autoCompleteIncludeSingle)
config.WriteBool('Options/AutoCompleteIncludeDouble',
self.autoCompleteIncludeDouble)
config.WriteBool('Options/AutoCallTip', self.autoCallTip)
config.WriteBool('Options/CallTipInsert', self.callTipInsert)
config.WriteBool('View/WrapMode', self.GetWrapMode())
config.WriteBool('View/ShowLineNumbers', self.lineNumbers)
config.WriteInt('View/Zoom/Shell', self.GetZoom())
def GetContextMenu(self):
"""
Create and return a context menu for the slices shell.
This is used instead of the scintilla default menu
in order to correctly respect our immutable buffer.
"""
menu = wx.Menu()
menu.Append(wx.ID_UNDO, "Undo")
menu.Append(wx.ID_REDO, "Redo")
menu.AppendSeparator()
menu.Append(wx.ID_CUT, "Cut")
menu.Append(wx.ID_COPY, "Copy")
menu.Append(frame.ID_COPY_PLUS, "Copy Plus")
menu.Append(wx.ID_PASTE, "Paste")
menu.Append(frame.ID_PASTE_PLUS, "Paste Plus")
menu.Append(wx.ID_CLEAR, "Clear")
menu.AppendSeparator()
menu.Append(wx.ID_SELECTALL, "Select All")
return menu
def OnContextMenu(self, evt):
menu = self.GetContextMenu()
self.PopupMenu(menu)
def OnUpdateUI(self, evt):
id = evt.Id
if id in (wx.ID_CUT, wx.ID_CLEAR):
evt.Enable(self.CanCut())
elif id in (wx.ID_COPY, frame.ID_COPY_PLUS):
evt.Enable(self.CanCopy())
elif id in (wx.ID_PASTE, frame.ID_PASTE_PLUS):
evt.Enable(self.CanPaste())
elif id == wx.ID_UNDO:
evt.Enable(self.CanUndo())
elif id == wx.ID_REDO:
evt.Enable(self.CanRedo())
def LoadPySlicesFile(self,fid):
invalidFileString = 'Not a valid input format'
lineCount=0
groupingStartLines=[0]
ioStartLines=[0]
ioStartTypes=[]
removeComment=False
# Read the initial three (or four) lines that have version and marker information
line=fid.readline()
if line == usrBinEnvPythonText:
line=fid.readline() # Add the option to place #!/usr/bin/env python at the top
if line not in pyslicesFormatHeaderText: print invalidFileString ; return
line=fid.readline()
if line != groupingStartText: print invalidFileString ; return
line=fid.readline()
if line == inputStartText: ioStartTypes.append('input');removeComment=False
elif line == outputStartText: ioStartTypes.append('output');removeComment=True
else: print invalidFileString ; return
self.ClearAll()
# Write the file's text to the text area
# Capture Marker information to
for i in fid:
if i==groupingStartText:
groupingStartLines.append(lineCount)
elif i==inputStartText:
ioStartLines.append(lineCount)
ioStartTypes.append('input')
removeComment=False
elif i==outputStartText:
ioStartLines.append(lineCount)
ioStartTypes.append('output')
removeComment=True
else:
if removeComment: w=i[1:].replace(os.linesep,'\n')
else: w=i.replace(os.linesep,'\n')
self.write(w,'Input',silent=True)
lineCount+=1
if w[-1]=='\n':
lineCount+=1
for i in range(lineCount+1):
self.clearGroupingMarkers(i)
self.clearIOMarkers(i)
doMiddle=False
doEnd=False
if groupingStartLines!=[]:
if i == groupingStartLines[0]:
self.MarkerAdd(i,GROUPING_START)
del groupingStartLines[0]
elif i+1 == groupingStartLines[0]:
doEnd=True
else:
doMiddle=True
elif i==lineCount-1:
doEnd=True
else:
doMiddle=True
if doMiddle:
self.MarkerAdd(i,GROUPING_MIDDLE)
elif doEnd:
self.MarkerAdd(i,GROUPING_END)
doMiddle=False
doEnd=False
if ioStartLines!=[]:
if i == ioStartLines[0]:
# Delete the old ioStartTypes (keep the current copy for later use)
if i>0: del ioStartTypes[0]
if ioStartTypes[0]=='input':
self.MarkerAdd(i,INPUT_START)
elif ioStartTypes[0]=='output':
self.MarkerAdd(i,OUTPUT_START)
self.MarkerAdd(i,OUTPUT_BG)
else:
#print 'Invalid Type!';
return
# Only delete markers we are totally finished with...
# Keep one more "StartTypes" than "StartLines"
del ioStartLines[0]
elif i+1 == ioStartLines[0]:
doEnd=True
else:
doMiddle=True
elif i==lineCount-1:
doEnd=True
else:
doMiddle=True
if doMiddle:
if ioStartTypes[0]=='input':
self.MarkerAdd(i,INPUT_MIDDLE)
elif ioStartTypes[0]=='output':
self.MarkerAdd(i,OUTPUT_MIDDLE)
self.MarkerAdd(i,OUTPUT_BG)
else:
#print 'Invalid Type!';
return
elif doEnd:
if ioStartTypes[0]=='input':
self.MarkerAdd(i,INPUT_END)
elif ioStartTypes[0]=='output':
self.MarkerAdd(i,OUTPUT_END)
self.MarkerAdd(i,OUTPUT_BG)
else:
#print 'Invalid Type!';
return
self.EmptyUndoBuffer() # maybe not?
def SavePySlicesFile(self,fid):
addComment=False
fid.write(usrBinEnvPythonText.replace('\n',os.linesep))
fid.write(pyslicesFormatHeaderText[-1].replace('\n',os.linesep))
for i in range(self.GetLineCount()):
markers=self.MarkerGet(i)
if markers & ( 1<<GROUPING_START | 1<<GROUPING_START_FOLDED ):
fid.write(groupingStartText.replace('\n',os.linesep))
if markers & ( 1<<INPUT_START | 1<<INPUT_START_FOLDED ):
fid.write(inputStartText.replace('\n',os.linesep))
addComment=False
if markers & ( 1<<OUTPUT_START | 1<<OUTPUT_START_FOLDED ):
fid.write(outputStartText.replace('\n',os.linesep))
addComment=True
if addComment: fid.write('#')
fid.write(self.GetLine(i).replace('\n',os.linesep))
# FIX ME!!
def LoadPyFileAsSlice(self,fid):
curpos=self.GetCurrentPos()
start,end = self.GetGroupingSlice()
endpos=self.GetLineEndPosition(end)
self.SetCurrentPos(endpos)
self.SetSelection(endpos, endpos)
text='\n'+fid.read()
self.write(text,'Input')
newpos=self.GetCurrentPos()
self.SetCurrentPos(curpos)
self.SetSelection(curpos,curpos)
self.SplitSlice()
#self.SetCurrentPos(newpos)
#self.SetSelection(newpos,newpos)
def hasChanged(self):
"""Return True if contents have changed."""
return self.GetModify() or self.NeedsCheckForSave
## NOTE: The DnD of file names is disabled until we can figure out how
## best to still allow DnD of text.
## #seb : File drag and drop
## class FileDropTarget(wx.FileDropTarget):
## def __init__(self, obj):
## wx.FileDropTarget.__init__(self)
## self.obj = obj
## def OnDropFiles(self, x, y, filenames):
## if len(filenames) == 1:
## txt = 'r\"%s\"' % filenames[0]
## else:
## txt = '( '
## for f in filenames:
## txt += 'r\"%s\" , ' % f
## txt += ')'
## self.obj.AppendText(txt)
## pos = self.obj.GetCurrentPos()
## self.obj.SetCurrentPos( pos )
## self.obj.SetSelection( pos, pos )
## class TextAndFileDropTarget(wx.DropTarget):
## def __init__(self, sliceshell):
## wx.DropTarget.__init__(self)
## self.sliceshell = sliceshell
## self.compdo = wx.DataObjectComposite()
## self.textdo = wx.TextDataObject()
## self.filedo = wx.FileDataObject()
## self.compdo.Add(self.textdo)
## self.compdo.Add(self.filedo, True)
## self.SetDataObject(self.compdo)
## def OnDrop(self, x, y):
## return True
## def OnData(self, x, y, result):
## self.GetData()
## if self.textdo.GetTextLength() > 1:
## text = self.textdo.GetText()
## # *** Do somethign with the dragged text here...
## self.textdo.SetText('')
## else:
## filenames = str(self.filename.GetFilenames())
## if len(filenames) == 1:
## txt = 'r\"%s\"' % filenames[0]
## else:
## txt = '( '
## for f in filenames:
## txt += 'r\"%s\" , ' % f
## txt += ')'
## self.sliceshell.AppendText(txt)
## pos = self.sliceshell.GetCurrentPos()
## self.sliceshell.SetCurrentPos( pos )
## self.sliceshell.SetSelection( pos, pos )
## return result
|