1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665
|
#!/bin/bash
# Note: bind (INITIALIZE_DEFMAP) の中から再帰的に呼び出されうるので、
# 先に ble-edit/bind/load-editing-mode:vi を上書きする必要がある。
ble/is-function ble-edit/bind/load-editing-mode:vi && return 0
function ble-edit/bind/load-editing-mode:vi { return 0; }
# 2020-04-29 force update (rename ble-decode/keymap/.register)
# 2021-01-25 force update (change mapping of C-w and M-w)
# 2021-04-26 force update (rename ble/decode/keymap#.register)
# 2021-09-23 force update (change to nsearch and bind-history)
source -- "$_ble_base/lib/keymap.vi_digraph.sh"
## @bleopt keymap_vi_macro_depth
bleopt/declare -n keymap_vi_macro_depth 64
## @fn ble/keymap:vi/k2c key
## @var[out] ret
function ble/keymap:vi/k2c {
local key=$1
local flag=$((key&_ble_decode_MaskFlag)) char=$((key&_ble_decode_MaskChar))
if ((flag==0&&(32<=char&&char<_ble_decode_FunctionKeyBase))); then
ret=$char
return 0
elif ((flag==_ble_decode_Ctrl&&63<=char&&char<128&&(char&0x1F)!=0)); then
((char=char==63?127:char&0x1F))
ret=$char
return 0
else
return 1
fi
}
#------------------------------------------------------------------------------
# utils
## @fn ble/string#index-of-chars text chars [index]
## 文字集合に含まれる文字を、文字列中で順方向に探索します。
## @fn ble/string#last-index-of-chars text chars [index]
## 文字集合に含まれる文字を、文字列中で逆方向に探索します。
##
## @param[in] text
## 検索する対象の文字列を指定します。
## @param[in] chars
## 検索する文字の集合を指定します
## @param[in] index
## text の内の検索開始位置を指定します。
## @var[out] ret
##
function ble/string#index-of-chars {
local chars=$2 index=${3:-0}
local text=${1:index}
local cut=${text%%["$chars"]*}
if ((${#cut}<${#text})); then
((ret=index+${#cut}))
return 0
else
ret=-1
return 1
fi
}
function ble/string#last-index-of-chars {
local text=$1 chars=$2 index=$3
[[ $index ]] && text=${text::index}
local cut=${text%["$chars"]*}
if ((${#cut}<${#text})); then
((ret=${#cut}))
return 0
else
ret=-1
return 1
fi
}
## @fn ble-edit/content/nonbol-eolp text
## @var[out] ret
function ble-edit/content/nonbol-eolp {
local pos=${1:-$_ble_edit_ind}
! ble-edit/content/bolp "$pos" && ble-edit/content/eolp "$pos"
}
## @fn ble/keymap:vi/string#encode-rot13 text
## @var[out] ret
function ble/keymap:vi/string#encode-rot13 {
local text=$1
local -a buff=() ch
for ((i=0;i<${#text};i++)); do
ch=${text:i:1}
if ble/string#isupper "$ch"; then
ch=${_ble_util_string_upper_list%%"$ch"*}
ch=${_ble_util_string_upper_list:(${#ch}+13)%26:1}
elif ble/string#islower "$ch"; then
ch=${_ble_util_string_lower_list%%"$ch"*}
ch=${_ble_util_string_lower_list:(${#ch}+13)%26:1}
fi
ble/array#push buff "$ch"
done
IFS= builtin eval 'ret="${buff[*]-}"'
}
#------------------------------------------------------------------------------
# constants
_ble_keymap_vi_REX_WORD=$'[_a-zA-Z0-9]+|[!-/:-@[-`{-~]+|[^ \t\na-zA-Z0-9!-/:-@[-`{-~]+'
#------------------------------------------------------------------------------
# vi_imap/__default__, vi-command/decompose-meta
function ble/widget/vi_imap/__default__ {
local flag=$((KEYS[0]&_ble_decode_MaskFlag)) code=$((KEYS[0]&_ble_decode_MaskChar))
# メタ修飾付きの入力 M-key は ESC + key に分解する
if ((flag&_ble_decode_Meta)); then
ble/keymap:vi/imap-repeat/pop
local esc=27 # ESC
# local esc=$((_ble_decode_Ctrl|0x5b)) # もしくは C-[
((flag&=~_ble_decode_Meta))
((flag==_ble_decode_Shft&&0x61<=code&&code<=0x7A&&(flag=0,code-=0x20)))
ble/decode/widget/redispatch-by-keys "$esc" "$((flag|code))" "${KEYS[@]:1}"
return 0
fi
# Control 修飾された文字 C-@ - C-\, C-? は制御文字 \000 - \037, \177 に戻して挿入
if local ret; ble/keymap:vi/k2c "${KEYS[0]}"; then
local -a KEYS; KEYS=("$ret")
ble/widget/self-insert
return 0
fi
return 125
}
function ble/widget/vi-command/decompose-meta {
local flag=$((KEYS[0]&_ble_decode_MaskFlag)) code=$((KEYS[0]&_ble_decode_MaskChar))
# メタ修飾付きの入力 M-key は ESC + key に分解する
if ((flag&_ble_decode_Meta)); then
local esc=$((_ble_decode_Ctrl|0x5b)) # C-[ (もしくは esc=27 ESC?)
((flag&=~_ble_decode_Meta))
((flag==_ble_decode_Shft&&0x61<=code&&code<=0x7A&&(flag=0,code-=0x20)))
ble/decode/widget/redispatch-by-keys "$esc" "$((flag|code))" "${KEYS[@]:1}"
return 0
fi
return 125
}
function ble/widget/vi_omap/__default__ {
ble/widget/vi-command/decompose-meta || ble/widget/vi-command/bell
return 0
}
function ble/widget/vi_omap/cancel {
ble/keymap:vi/adjust-command-mode
return 0
}
#------------------------------------------------------------------------------
# repeat
## @var _ble_keymap_vi_irepeat_count
## 挿入モードに入る時に指定された引数を記録する。
_ble_keymap_vi_irepeat_count=
## @arr _ble_keymap_vi_irepeat
## 挿入モードに入るときに指定された引数が 1 より大きい時、
## 後で操作を繰り返すために操作内容を記録する配列。
##
## 各要素は keys:widget の形式を持つ。
## keys は空白区切りの key (整数値) の列、つまり ${KEYS[*]} である。
## widget は実際に呼び出す WIDGET の内容である。
##
_ble_keymap_vi_irepeat=()
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_irepeat_count \
_ble_keymap_vi_irepeat
function ble/keymap:vi/imap-repeat/pop {
local top_index=$((${#_ble_keymap_vi_irepeat[*]}-1))
((top_index>=0)) && builtin unset -v '_ble_keymap_vi_irepeat[top_index]'
}
function ble/keymap:vi/imap-repeat/push {
local IFS=$_ble_term_IFS
ble/array#push _ble_keymap_vi_irepeat "${KEYS[*]-}:$WIDGET"
}
function ble/keymap:vi/imap-repeat/reset {
local count=${1-}
_ble_keymap_vi_irepeat_count=
_ble_keymap_vi_irepeat=()
((count>1)) && _ble_keymap_vi_irepeat_count=$count
}
function ble/keymap:vi/imap-repeat/process {
if ((_ble_keymap_vi_irepeat_count>1)); then
local repeat=$_ble_keymap_vi_irepeat_count
local -a widgets; widgets=("${_ble_keymap_vi_irepeat[@]}")
local i widget
for ((i=1;i<repeat;i++)); do
for widget in "${widgets[@]}"; do
ble/decode/widget/call "${widget#*:}" ${widget%%:*}
done
done
fi
}
function ble/keymap:vi/imap/invoke-widget {
local WIDGET=$1
local -a KEYS; KEYS=("${@:2}")
ble/keymap:vi/imap-repeat/push
builtin eval -- "$WIDGET"
}
## @arr _ble_keymap_vi_imap_white_list
## 引数を指定して入った挿入モードを抜けるときの繰り返しで許されるコマンドのリスト
_ble_keymap_vi_imap_white_list=(
self-insert
batch-insert
nop
magic-space magic-slash
delete-backward-{c,f,s,u}word
copy{,-forward,-backward}-{c,f,s,u}word
copy-region{,-or}
clear-screen
command-help
display-shell-version
redraw-line
)
function ble/keymap:vi/imap/is-command-white {
if [[ $1 == ble/widget/self-insert ]]; then
# frequently used command is checked first
return 0
elif [[ $1 == ble/widget/* ]]; then
local IFS=$_ble_term_IFS
local cmd=${1#ble/widget/}; cmd=${cmd%%["$_ble_term_IFS"]*}
[[ $cmd == vi_imap/* || " ${_ble_keymap_vi_imap_white_list[*]} " == *" $cmd "* ]] && return 0
fi
return 1
}
function ble/widget/vi_imap/__before_widget__ {
if ble/keymap:vi/imap/is-command-white "$WIDGET"; then
ble/keymap:vi/imap-repeat/push
else
if ((_ble_keymap_vi_mark_edit_dbeg>=0)); then
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/repeat/record-insert
ble/keymap:vi/mark/start-edit-area
fi
ble/keymap:vi/imap-repeat/reset
fi
}
# Note: The following widgets need to have the name of the form "vi_imap/*" to
# suppress the invokation of "ble-edit/undo/add".
function ble/widget/vi_imap/undo { ble/widget/undo "$@"; }
function ble/widget/vi_imap/redo { ble/widget/redo "$@"; }
function ble/widget/vi_imap/revert { ble/widget/revert "$@"; }
#------------------------------------------------------------------------------
# vi_imap/complete
function ble/widget/vi_imap/complete {
ble/keymap:vi/imap-repeat/pop
ble/keymap:vi/undo/add more
ble/widget/complete "$@"
}
function ble/widget/vi_imap/menu-complete {
ble/widget/vi_imap/complete "enter_menu:insert_unique:$@"
}
function ble/widget/vi_nmap/complete {
# We reset the edit argument for "ble/widget/complete".
local ARG FLAG REG; ble/keymap:vi/get-arg ''
_ble_edit_arg=$ARG
# Note: We adjust the cursor position unless we are at the end of a line. To
# be consistent with Bash's "bash-vi-complete" and "vi-complete", we do not
# adjust the cursor position also when the current position is tab or
# space. This behavior seems also natural for the user's perspective,
# though it introduces a non-trivial behavioral variation.
ble-edit/content/eolp ||
[[ ${_ble_edit_str:_ble_edit_ind:1} == ["$_ble_term_IFS"] ]] ||
((_ble_edit_ind++))
local keymap=$_ble_decode_keymap
ble/widget/complete "$@"; local ext=$?
if [[ $_ble_decode_keymap == "$keymap" ]]; then
if [[ :$1: == *:vi_nmap/insert-mode:* ]]; then
ble/widget/vi_nmap/.insert-mode
else
# Note: We record the editing area `[`] through
# "ble/keymap:vi/complete/insert.hook", so we do not need to manually set
# up the edit area by calling "ble/keymap:vi/mark/{start,end}-edit-area".
# "ble-edit/undo/add" is also called through
# "ble/keymap:vi/mark/set-previous-edit-area" called from
# "ble/keymap:vi/complete/insert.hook".
# Note: if "ble/widget/complete" enters another mode such as the
# menu-complete mode, we do not try to adjust the state here. Instead,
# we adjust the state in "ble/complete/menu_complete/exit" after the
# corresponding "ble/decode/keymap/pop".
ble-edit/content/bolp || ((_ble_edit_ind--))
ble/keymap:vi/adjust-command-mode
fi
fi
return "$ext"
}
function ble/widget/vi_nmap/menu-complete {
ble/widget/vi_nmap/complete "enter_menu:insert_unique:$@"
}
function ble/widget/vi_nmap/complete-insert {
local original=$1 insert=$2 suffix=$3
ble-edit/content/eolp || ((_ble_edit_ind++))
local ind=$_ble_edit_ind
if ble/widget/complete-insert "$@"; then
local beg=$((ind-${#original}))
local end=$((beg+${#insert}+${#suffix}))
ble/keymap:vi/mark/set-previous-edit-area "$beg" "$end"
fi
ble-edit/content/bolp || ((_ble_edit_ind--))
}
function ble/keymap:vi/complete/insert.hook {
local keymap
ble/decode/keymap/get-major-keymap
[[ $keymap == vi_[in]map ]] || return 0
local q="'" Q="'\''"
local original=${comp_text:insert_beg:insert_end-insert_beg}
local WIDGET="ble/widget/complete-insert '${original//$q/$Q}' '${insert//$q/$Q}' '${suffix//$q/$Q}'"
case $keymap in
(vi_imap)
ble/keymap:vi/imap-repeat/push # @var[in] WIDGET
[[ $_ble_decode_keymap == vi_imap ]] &&
ble/keymap:vi/undo/add more ;;
(vi_nmap)
local beg=$insert_beg end=$((insert_beg+${#insert}+${#suffix}))
ble/keymap:vi/mark/set-previous-edit-area "$beg" "$end"
local KEYMAP=vi_nmap ARG= FLAG= REG=
WIDGET=ble/widget/vi_nmap/${WIDGET#ble/widget/}
ble/keymap:vi/repeat/record-normal ;; # @var[in] KEYMAP WIDGET ARG FLAG REG
esac
}
blehook complete_insert!=ble/keymap:vi/complete/insert.hook
function ble-decode/keymap:vi_imap/bind-complete {
ble-bind -f 'C-i' 'vi_imap/complete'
ble-bind -f 'TAB' 'vi_imap/complete'
ble-bind -f 'C-TAB' 'vi_imap/menu-complete'
ble-bind -f 'S-C-i' 'vi_imap/menu-complete backward'
ble-bind -f 'S-TAB' 'vi_imap/menu-complete backward'
ble-bind -f 'ac_enter' 'auto-complete-enter'
ble-bind -f 'C-x /' 'vi_imap/menu-complete context=filename'
ble-bind -f 'C-x ~' 'vi_imap/menu-complete context=username'
ble-bind -f 'C-x $' 'vi_imap/menu-complete context=variable'
ble-bind -f 'C-x @' 'vi_imap/menu-complete context=hostname'
ble-bind -f 'C-x !' 'vi_imap/menu-complete context=command'
ble-bind -f 'C-]' 'sabbrev-expand'
ble-bind -f 'C-x C-r' 'dabbrev-expand'
ble-bind -f 'C-x *' 'vi_imap/complete insert_all:context=glob'
ble-bind -f 'C-x g' 'vi_imap/complete show_menu:context=glob'
}
function ble/widget/vi-rlfunc/vi-complete {
local key=0
((${#KEYS[@]})) && key=${KEYS[${#KEYS[@]}-1]:-0}
if ((key==0x2a)); then # '*'
ble/widget/vi_nmap/complete "insert-all:vi_nmap/insert-mode:$@"
elif ((key==0x3d)); then # '='
ble/widget/vi_nmap/complete "show-menu:$@"
elif ((key==0x5c)); then # '\'
ble/complete/menu/clear
ble/widget/vi_nmap/complete "vi_nmap/insert-mode:$@"
else
# Note: Bash does not enter the insert mode in the other cases, but it
# seems inconsistent. We here enter the insert mode also with the other
# types of completions. In our implementation, '=' is the only exception,
# which makes sense because "show-menu" only shows the status in the info
# panel and doesn't change the command line. The users who want the Bash
# behavior that doesn't enter the insert mode should use widget
# "vi_nmap/complete".
ble/widget/vi_nmap/complete "vi_nmap/insert-mode:$@"
fi
}
function ble/widget/vi-rlfunc/bash-vi-complete {
local key=0
((${#KEYS[@]})) && key=${KEYS[${#KEYS[@]}-1]:-0}
if ((key==0x2a)); then # '*'
ble/widget/vi_nmap/complete "context=glob:insert-all:vi_nmap/insert-mode:$@"
elif ((key==0x3d)); then # '='
ble/widget/vi_nmap/complete "context=glob:show-menu:$@"
elif ((key==0x5c)); then # '\'
ble/complete/menu/clear
ble/widget/vi_nmap/complete "context=glob:vi_nmap/insert-mode:$@"
else
# Note: This implementation enters the insert mode in this case. See the
# code comment in ble/widget/vi-rlfunc/vi-complete.
ble/widget/vi_nmap/complete "vi_nmap/insert-mode:$@"
fi
}
#------------------------------------------------------------------------------
# modes
## @var _ble_keymap_vi_insert_overwrite
## 挿入モードに入った時の上書き文字
_ble_keymap_vi_insert_overwrite=
## @var _ble_keymap_vi_insert_leave
## 挿入モードから抜ける時に実行する関数を設定します
_ble_keymap_vi_insert_leave=
## @var _ble_keymap_vi_single_command
## ノーマルモードにおいて 1 つコマンドを実行したら
## 元の挿入モードに戻るモード (C-o) にいるかどうかを表します。
_ble_keymap_vi_single_command=
_ble_keymap_vi_single_command_overwrite=
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_insert_overwrite \
_ble_keymap_vi_insert_leave \
_ble_keymap_vi_single_command \
_ble_keymap_vi_single_command_overwrite
## @bleopt keymap_vi_mode_string_nmap
## ノーマルモードの時に表示する文字列を指定します。
## 空文字列を指定したときは何も表示しません。
bleopt/declare -n keymap_vi_mode_string_nmap $'\e[1m~\e[m'
bleopt/declare -o keymap_vi_nmap_name keymap_vi_mode_string_nmap
bleopt/declare -v term_vi_imap ''
bleopt/declare -v term_vi_nmap ''
bleopt/declare -v term_vi_omap ''
bleopt/declare -v term_vi_xmap ''
bleopt/declare -v term_vi_smap ''
bleopt/declare -v term_vi_cmap ''
bleopt/declare -v keymap_vi_imap_cursor ''
bleopt/declare -v keymap_vi_nmap_cursor ''
bleopt/declare -v keymap_vi_omap_cursor ''
bleopt/declare -v keymap_vi_xmap_cursor ''
bleopt/declare -v keymap_vi_smap_cursor ''
bleopt/declare -v keymap_vi_cmap_cursor ''
function ble/keymap:vi/.process-cursor-options {
local keymap=${FUNCNAME[1]#bleopt/check:keymap_}; keymap=${keymap%_cursor}
ble-bind -m "$keymap" --cursor "$value"
local locate=$'\e[32m'$bleopt_source:$bleopt_lineno$'\e[m'
ble/util/print-lines \
"bleopt ($locate): The option 'keymap_${keymap}_cursor' has been removed." \
" Please use 'ble-bind -m $keymap --cursor $value' instead." >&2
}
function bleopt/check:keymap_vi_imap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/check:keymap_vi_nmap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/check:keymap_vi_omap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/check:keymap_vi_xmap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/check:keymap_vi_smap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/check:keymap_vi_cmap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/obsolete:keymap_vi_imap_cursor { return 0; }
function bleopt/obsolete:keymap_vi_nmap_cursor { return 0; }
function bleopt/obsolete:keymap_vi_omap_cursor { return 0; }
function bleopt/obsolete:keymap_vi_xmap_cursor { return 0; }
function bleopt/obsolete:keymap_vi_smap_cursor { return 0; }
function bleopt/obsolete:keymap_vi_cmap_cursor { return 0; }
bleopt/declare -v keymap_vi_mode_show 1
function bleopt/check:keymap_vi_mode_show {
local bleopt_keymap_vi_mode_show=$value
[[ $_ble_attached ]] &&
ble/keymap:vi/update-mode-indicator
return 0
}
bleopt/declare -v keymap_vi_mode_update_prompt ''
bleopt/declare -v keymap_vi_mode_name_insert 'INSERT'
bleopt/declare -v keymap_vi_mode_name_replace 'REPLACE'
bleopt/declare -v keymap_vi_mode_name_vreplace 'VREPLACE'
bleopt/declare -v keymap_vi_mode_name_visual 'VISUAL'
bleopt/declare -v keymap_vi_mode_name_select 'SELECT'
bleopt/declare -v keymap_vi_mode_name_linewise 'LINE'
bleopt/declare -v keymap_vi_mode_name_blockwise 'BLOCK'
function bleopt/check:keymap_vi_mode_name_insert { ble/keymap:vi/update-mode-indicator; }
function bleopt/check:keymap_vi_mode_name_replace { ble/keymap:vi/update-mode-indicator; }
function bleopt/check:keymap_vi_mode_name_vreplace { ble/keymap:vi/update-mode-indicator; }
function bleopt/check:keymap_vi_mode_name_visual { ble/keymap:vi/update-mode-indicator; }
function bleopt/check:keymap_vi_mode_name_select { ble/keymap:vi/update-mode-indicator; }
function bleopt/check:keymap_vi_mode_name_linewise { ble/keymap:vi/update-mode-indicator; }
function bleopt/check:keymap_vi_mode_name_blockwise { ble/keymap:vi/update-mode-indicator; }
## @fn ble/keymap:vi/script/get-vi-keymap
## 現在の vi キーマップ名 (vi_?map) を取得します。
## もし現在 vi キーマップにない場合には失敗します。
function ble/keymap:vi/script/get-vi-keymap {
ble/prompt/unit/add-hash '$_ble_decode_keymap,${_ble_decode_keymap_stack[*]}'
local i=${#_ble_decode_keymap_stack[@]}
keymap=$_ble_decode_keymap
while [[ $keymap != vi_?map && $keymap != emacs ]]; do
((i--)) || return 1
keymap=${_ble_decode_keymap_stack[i]}
done
[[ $keymap == vi_?map ]]
}
## @fn ble/keymap:vi/script/get-mode
## @var[out] mode
function ble/keymap:vi/script/get-mode {
ble/prompt/unit/add-hash '$_ble_decode_keymap,${_ble_decode_keymap_stack[*]}'
ble/prompt/unit/add-hash '$_ble_keymap_vi_single_command,$_ble_edit_mark_active'
mode=
local keymap; ble/keymap:vi/script/get-vi-keymap
# /[iR^R]?/
if [[ $_ble_keymap_vi_single_command || $keymap == vi_imap ]]; then
local overwrite=
if [[ $keymap == vi_imap ]]; then
overwrite=$_ble_edit_overwrite_mode
elif [[ $keymap == vi_[noxs]map ]]; then
overwrite=$_ble_keymap_vi_single_command_overwrite
fi
case $overwrite in
('') mode=i ;;
(R) mode=R ;;
(*) mode=$'\x12' ;; # C-r
esac
fi
# /[nvV^VsS^S]?/
case $keymap:${_ble_edit_mark_active%+} in
(vi_xmap:vi_line) mode=$mode'V' ;;
(vi_xmap:vi_block)mode=$mode$'\x16' ;; # C-v
(vi_xmap:*) mode=$mode'v' ;;
(vi_smap:vi_line) mode=$mode'S' ;;
(vi_smap:vi_block)mode=$mode$'\x13' ;; # C-s
(vi_smap:*) mode=$mode's' ;;
(vi_[no]map:*) mode=$mode'n' ;;
(vi_cmap:*) mode=$mode'c' ;;
(vi_imap:*) ;;
(*:*) mode=$mode'?' ;;
esac
}
_ble_keymap_vi_mode_name_dirty=
function ble/keymap:vi/info_reveal.hook {
[[ $_ble_keymap_vi_mode_name_dirty ]] || return 0
_ble_keymap_vi_mode_name_dirty=
ble/keymap:vi/update-mode-indicator
}
blehook info_reveal!=ble/keymap:vi/info_reveal.hook
bleopt/declare -v prompt_vi_mode_indicator '\q{keymap:vi/mode-indicator}'
function bleopt/check:prompt_vi_mode_indicator {
local bleopt_prompt_vi_mode_indicator=$value
[[ $_ble_attached ]] && ble/keymap:vi/update-mode-indicator
return 0
}
_ble_keymap_vi_mode_indicator_data=()
function ble/prompt/unit:_ble_keymap_vi_mode_indicator/update {
local trace_opts=truncate:relative:noscrc:ansi
local prompt_rows=1
local prompt_cols=${COLUMNS:-80}
((prompt_cols&&prompt_cols--))
local "${_ble_prompt_cache_vars[@]/%/=}" # WA #D1570 checked
ble/prompt/unit:{section}/update _ble_keymap_vi_mode_indicator "$bleopt_prompt_vi_mode_indicator" "$trace_opts"
}
function ble/keymap:vi/update-mode-indicator {
if [[ ! $_ble_attached ]] || ble/edit/is-command-layout; then
_ble_keymap_vi_mode_name_dirty=1
return 0
fi
local keymap
ble/keymap:vi/script/get-vi-keymap || return 0
if [[ $keymap == vi_imap ]]; then
ble/util/buffer "$bleopt_term_vi_imap"
elif [[ $keymap == vi_nmap ]]; then
ble/util/buffer "$bleopt_term_vi_nmap"
elif [[ $keymap == vi_xmap ]]; then
ble/util/buffer "$bleopt_term_vi_xmap"
elif [[ $keymap == vi_smap ]]; then
ble/util/buffer "$bleopt_term_vi_smap"
elif [[ $keymap == vi_omap ]]; then
ble/util/buffer "$bleopt_term_vi_omap"
elif [[ $keymap == vi_cmap ]]; then
ble/edit/info/default text ''
ble/util/buffer "$bleopt_term_vi_cmap"
return 0
fi
[[ $bleopt_keymap_vi_mode_update_prompt ]] && ble/prompt/clear
# prompt_vi_mode_indicator
local prompt_vi_keymap=$keymap
local version=$COLUMNS,$_ble_edit_lineno,$_ble_history_count,$_ble_edit_CMD
local prompt_hashref_base='$version'
ble/prompt/unit#update _ble_keymap_vi_mode_indicator
local ret; ble/prompt/unit:{section}/get _ble_keymap_vi_mode_indicator; local mode=$ret
local str=$mode
if [[ $_ble_keymap_vi_reg_record ]]; then
str=$str${str:+' '}$'\e[1;31mREC @'$_ble_keymap_vi_reg_record_char$'\e[m'
elif [[ $_ble_edit_kbdmacro_record ]]; then
str=$str${str:+' '}$'\e[1;31mREC\e[m'
fi
# Note: If the completion menu is activated, we deactivate the menu before
# updating the mode indicator in the info panel.
if [[ $_ble_complete_menu_active ]]; then
ble/complete/menu/clear
fi
# Note #D2062: mc-4.8.29 以降ではコマンド終了直後に "-- INSERT --" 等の mode
# indicator を出力すると、それをプロンプトと勘違いして抽出してしまう。仕方が
# ないので mc の中では imap に対しては mode indicator は表示しない様にする。
if [[ $_ble_edit_integration_mc_precmd_stop && $keymap == vi_imap ]]; then
ble/edit/info/clear
else
ble/edit/info/default ansi "$str" # 6ms
fi
}
blehook internal_PRECMD!=ble/keymap:vi/update-mode-indicator
## @fn ble/prompt/backslash:keymap:vi/mode-indicator
## @var[in,opt] prompt_vi_keymap
## ble/keymap:vi/script/get-vi-keymap のキャッシュ
function ble/prompt/backslash:keymap:vi/mode-indicator {
[[ $bleopt_keymap_vi_mode_show ]] || return 0
local keymap=${prompt_vi_keymap-}
if [[ $keymap ]]; then
ble/prompt/unit/add-hash '$_ble_decode_keymap,${_ble_decode_keymap_stack[*]}'
else
ble/keymap:vi/script/get-vi-keymap || return 0
fi
local name= show= overwrite=
ble/prompt/unit/add-hash '$_ble_edit_overwrite_mode,$_ble_keymap_vi_single_command,$_ble_keymap_vi_single_command_overwrite'
if [[ $keymap == vi_imap ]]; then
show=1 overwrite=$_ble_edit_overwrite_mode
elif [[ $_ble_keymap_vi_single_command && ( $keymap == vi_nmap || $keymap == vi_omap ) ]]; then
show=1 overwrite=$_ble_keymap_vi_single_command_overwrite
elif [[ $keymap == vi_[xs]map ]]; then
show=x overwrite=$_ble_keymap_vi_single_command_overwrite
else
name=$bleopt_keymap_vi_mode_string_nmap
fi
if [[ $show ]]; then
if [[ $overwrite == R ]]; then
name=$bleopt_keymap_vi_mode_name_replace
elif [[ $overwrite ]]; then
name=$bleopt_keymap_vi_mode_name_vreplace
else
name=$bleopt_keymap_vi_mode_name_insert
fi
if [[ $_ble_keymap_vi_single_command ]]; then
local ret; ble/string#tolower "$name"; name="($ret)"
fi
if [[ $show == x ]]; then
ble/prompt/unit/add-hash '${_ble_edit_mark_active%+}'
local mark_type=${_ble_edit_mark_active%+}
local visual_name=$bleopt_keymap_vi_mode_name_visual
[[ $keymap == vi_smap ]] && visual_name=$bleopt_keymap_vi_mode_name_select
if [[ $mark_type == vi_line ]]; then
visual_name=$visual_name' '$bleopt_keymap_vi_mode_name_linewise
elif [[ $mark_type == vi_block ]]; then
visual_name=$visual_name' '$bleopt_keymap_vi_mode_name_blockwise
fi
if [[ $_ble_keymap_vi_single_command ]]; then
name="$name $visual_name"
else
name=$visual_name
fi
fi
name=$'\e[1m-- '$name$' --\e[m'
fi
[[ ! $name ]] || ble/prompt/print "$name"
}
function ble/widget/vi_imap/normal-mode.impl {
local opts=$1
# finalize insert mode
ble/keymap:vi/mark/set-local-mark 94 "$_ble_edit_ind" # `^
ble/keymap:vi/mark/end-edit-area
[[ :$opts: == *:InsertLeave:* ]] && builtin eval -- "$_ble_keymap_vi_insert_leave"
# set up normal mode
_ble_edit_mark_active=
_ble_edit_overwrite_mode=
_ble_keymap_vi_insert_leave=
_ble_keymap_vi_single_command=
_ble_keymap_vi_single_command_overwrite=
if [[ :$opts: == *:SingleCommand:* ]]; then
ble-edit/content/nonbol-eolp && ((_ble_edit_ind--))
else
ble-edit/content/bolp || ((_ble_edit_ind--))
fi
ble/decode/keymap/push vi_nmap
}
function ble/widget/vi_imap/normal-mode {
ble-edit/content/clear-arg
ble/keymap:vi/imap-repeat/pop
ble/keymap:vi/imap-repeat/process
ble/keymap:vi/repeat/record-insert
ble/widget/vi_imap/normal-mode.impl InsertLeave
ble/keymap:vi/update-mode-indicator
return 0
}
function ble/widget/vi_imap/normal-mode-without-insert-leave {
ble-edit/content/clear-arg
ble/keymap:vi/imap-repeat/pop
ble/keymap:vi/repeat/record-insert
ble/widget/vi_imap/normal-mode.impl
ble/keymap:vi/update-mode-indicator
return 0
}
function ble/widget/vi_imap/single-command-mode {
ble-edit/content/clear-arg
local single_command=1
local single_command_overwrite=$_ble_edit_overwrite_mode
ble-edit/content/eolp && _ble_keymap_vi_single_command=2
ble/keymap:vi/imap-repeat/pop
ble/widget/vi_imap/normal-mode.impl SingleCommand
_ble_keymap_vi_single_command=$single_command
_ble_keymap_vi_single_command_overwrite=$single_command_overwrite
ble/keymap:vi/update-mode-indicator
return 0
}
## @fn ble/keymap:vi/needs-eol-fix
##
## Note: この関数を使った後は ble/keymap:vi/adjust-command-mode を呼び出す必要がある。
## そうしないとノーマルモードにおいてありえない位置にカーソルが来ることになる。
##
function ble/keymap:vi/needs-eol-fix {
[[ $_ble_decode_keymap == vi_nmap || $_ble_decode_keymap == vi_omap ]] || return 1
[[ $_ble_keymap_vi_single_command ]] && return 1
local index=${1:-$_ble_edit_ind}
ble-edit/content/nonbol-eolp "$index"
}
function ble/keymap:vi/adjust-command-mode {
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
# 移動コマンドが来たら末尾拡張を無効にする。
# 移動コマンドはここを通るはず…
ble/keymap:vi/xmap/remove-eol-extension
fi
local kmap_popped=
if [[ $_ble_decode_keymap == vi_omap ]]; then
ble/decode/keymap/pop
kmap_popped=1
fi
# search による mark の設定・解除
if [[ $_ble_keymap_vi_search_activate ]]; then
if [[ $_ble_decode_keymap != vi_[xs]map ]]; then
_ble_edit_mark_active=$_ble_keymap_vi_search_activate
fi
_ble_keymap_vi_search_matched=1
_ble_keymap_vi_search_activate=
else
[[ $_ble_edit_mark_active == vi_search ]] && _ble_edit_mark_active=
((_ble_keymap_vi_search_matched)) && _ble_keymap_vi_search_matched=
fi
if [[ $_ble_decode_keymap == vi_nmap && $_ble_keymap_vi_single_command ]]; then
if ((_ble_keymap_vi_single_command==2)); then
local index=$((_ble_edit_ind+1))
ble-edit/content/nonbol-eolp "$index" && _ble_edit_ind=$index
fi
ble/widget/vi_nmap/.insert-mode 1 "$_ble_keymap_vi_single_command_overwrite" resume
ble/keymap:vi/repeat/clear-insert
elif [[ $kmap_popped ]]; then
ble/keymap:vi/update-mode-indicator
fi
return 0
}
function ble/widget/vi-command/bell {
ble/widget/.bell "$1"
ble/keymap:vi/adjust-command-mode
return 0
}
## @fn ble/widget/vi_nmap/.insert-mode [arg [overwrite [opts]]]
## @param[in] arg
## @param[in] overwrite
## @param[in] opts
function ble/widget/vi_nmap/.insert-mode {
[[ $_ble_decode_keymap == vi_[xs]map ]] && ble/decode/keymap/pop
[[ $_ble_decode_keymap == vi_omap ]] && ble/decode/keymap/pop
local arg=$1 overwrite=$2
ble/keymap:vi/imap-repeat/reset "$arg"
_ble_edit_mark_active=
_ble_edit_overwrite_mode=$overwrite
_ble_keymap_vi_insert_leave=
_ble_keymap_vi_insert_overwrite=$overwrite
_ble_keymap_vi_single_command=
_ble_keymap_vi_single_command_overwrite=
ble/keymap:vi/search/clear-matched
ble/decode/keymap/pop
ble/keymap:vi/update-mode-indicator
ble/keymap:vi/mark/start-edit-area
if [[ :$opts: != *:resume:* ]]; then
ble/keymap:vi/mark/commit-edit-area "$_ble_edit_ind" "$_ble_edit_ind"
fi
}
function ble/widget/vi_nmap/insert-mode {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/append-mode {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
if ! ble-edit/content/eolp; then
((_ble_edit_ind++))
fi
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/append-mode-at-end-of-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret; ble-edit/content/find-logical-eol
_ble_edit_ind=$ret
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/insert-mode-at-beginning-of-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret; ble-edit/content/find-logical-bol
_ble_edit_ind=$ret
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/insert-mode-at-first-non-space {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/first-non-space
[[ ${_ble_edit_str:_ble_edit_ind:1} == [$' \t'] ]] &&
((_ble_edit_ind++)) # 逆eol補正
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
# nmap: gi
function ble/widget/vi_nmap/insert-mode-at-previous-point {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret
ble/keymap:vi/mark/get-local-mark 94 && _ble_edit_ind=$ret
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/replace-mode {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi_nmap/.insert-mode "$ARG" R
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/virtual-replace-mode {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi_nmap/.insert-mode "$ARG" 1
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/accept-line {
ble/keymap:vi/clear-arg
ble/widget/vi_nmap/.insert-mode
ble/keymap:vi/repeat/clear-insert
[[ $_ble_keymap_vi_reg_record ]] &&
ble/widget/vi_nmap/record-register
ble/widget/default/accept-line
}
function ble/widget/vi-command/edit-and-execute-command {
ble/keymap:vi/clear-arg
ble/widget/vi_nmap/.insert-mode
ble/keymap:vi/repeat/clear-insert
[[ $_ble_keymap_vi_reg_record ]] &&
ble/widget/vi_nmap/record-register
ble/widget/edit-and-execute-command vi
}
#------------------------------------------------------------------------------
# args
#
# arg : 0-9 d y c
# command : dd yy cc [dyc]0 Y S
_ble_keymap_vi_oparg=
_ble_keymap_vi_opfunc=
_ble_keymap_vi_reg=
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_oparg \
_ble_keymap_vi_opfunc \
_ble_keymap_vi_reg
# ble/keymap:vi における _ble_edit_kill_ring の扱いついて
#
# _ble_edit_kill_type=L のとき
# 行指向の切り取り文字列であることを表し、
# _ble_edit_kill_ring の末端には必ず改行文字が来ると仮定して良い。
#
# _ble_edit_kill_type=B:* の形式をしているとき、
# 矩形の切り取り文字列であることを表し、
# _ble_edit_kill_ring は改行区切りで各行を連結したものである。
# 末端には改行文字は付加しない。末端に改行文字があるときは、
# それは付加された改行ではなく、最後に空行があることを意味する。
#
# _ble_edit_kill_type の 2 文字目以降は数字を空白区切りで並べたもので、
# 各数字は _ble_edit_kill_ring 内の各行に対応する。
# 意味は、行の途中に挿入する際に矩形を保つために右に補填するべき空白の数である。
# 行末に挿入する際にはこの空白の補填は起こらないことに注意する。
#
# _ble_edit_kill_type= (空文字列) もしくは それ意外の場合は
# 通常の切り取り文字列であることを表す。
# _ble_edit_kill_ring は任意の文字列である。
#
_ble_keymap_vi_register=()
_ble_keymap_vi_register_onplay=
## @fn ble/keymap:vi/clear-arg
function ble/keymap:vi/clear-arg {
_ble_edit_arg=
_ble_keymap_vi_oparg=
_ble_keymap_vi_opfunc=
_ble_keymap_vi_reg=
}
## @fn ble/keymap:vi/get-arg [default_value]; ARG FLAG REG
##
## 引数の内容について
## vi_nmap, vi_xmap, vi_smap においては FLAG は空であると仮定して良い。
## vi_omap においては FLAG は非空である。
## get-arg{,-reg} を呼び出すことによって空になる。
## つまり vi_omap においてこの関数を呼び出したとき、vi_omap から vi_nmap に戻る必要がある。
## これは通例 ble/keymap:vi/adjust-command-mode によって実施される。
##
function ble/keymap:vi/get-arg {
local default_value=$1
REG=$_ble_keymap_vi_reg
FLAG=$_ble_keymap_vi_opfunc
if [[ ! $_ble_edit_arg && ! $_ble_keymap_vi_oparg ]]; then
ARG=$default_value
else
ARG=$((10#0${_ble_edit_arg:-1}*10#0${_ble_keymap_vi_oparg:-1}))
fi
ble/keymap:vi/clear-arg
}
## @fn ble/keymap:vi/register#load reg
function ble/keymap:vi/register#load {
local reg=$1
if [[ $reg ]] && ((reg!=34)); then
if [[ $reg == 37 ]]; then # "%
ble-edit/content/push-kill-ring "$HISTFILE" ''
return 0
fi
local value=${_ble_keymap_vi_register[reg]}
if [[ $value == */* ]]; then
ble-edit/content/push-kill-ring "${value#*/}" "${value%%/*}"
return 0
else
ble-edit/content/push-kill-ring
return 1
fi
fi
}
## @fn ble/keymap:vi/register#set reg type content
function ble/keymap:vi/register#set {
local reg=$1 type=$2 content=$3
# type = L は行指向の値
# type = B は矩形指向の値
# type = '' は文字指向の値
# type = q はキーボードマクロ
#
# Note: 実際に記録される type は以下の何れかである。
# type = L
# type = B:*
# type = ''
# 追記の場合
if [[ $reg == +* ]]; then
local value=${_ble_keymap_vi_register[reg]}
if [[ $value == */* ]]; then
local otype=${value%%/*}
local oring=${value#*/}
if [[ $otype == L ]]; then
if [[ $type == q ]]; then
type=L content=${oring%$'\n'}$content # V + * → V
else
type=L content=$oring$content # V + * → V
fi
elif [[ $type == L ]]; then
type=L content=$oring$'\n'$content # C-v + V, v + V → V
elif [[ $otype == B:* ]]; then
if [[ $type == B:* ]]; then
type=$otype' '${type#B:}
content=$oring$'\n'$content # C-v + C-v → C-v
elif [[ $type == q ]]; then
local ret; ble/string#count-char "$content" $'\n'
ble/string#repeat ' 0' "$ret"
type=$otype$ret
content=$oring$$content # C-v + q → C-v
else
local ret; ble/string#count-char "$content" $'\n'
ble/string#repeat ' 0' "$((ret+1))"
type=$otype$ret
content=$oring$'\n'$content # C-v + v → C-v
fi
else
type= content=$oring$content # v + C-v, v + v, v + q → v
fi
fi
fi
[[ $type == L && $content != *$'\n' ]] && content=$content$'\n'
local suppress_default=
[[ $type == q ]] && type= suppress_default=1
if [[ ! $reg ]] || ((reg==34)); then # ""
# unnamed register
ble-edit/content/push-kill-ring "$content" "$type"
return 0
elif ((reg==58||reg==46||reg==37||reg==126)); then # ": ". "% "~
# read only register
ble/widget/.bell "attempted to write on a read-only register #$reg"
return 1
elif ((reg==95)); then # "_
# black hole register
return 0
else
if [[ ! $suppress_default ]]; then
ble-edit/content/push-kill-ring "$content" "$type"
fi
_ble_keymap_vi_register[reg]=$type/$content
return 0
fi
}
## @fn ble/keymap:vi/register#set-yank reg type content
## レジスタ "0 に文字列を登録します。
##
function ble/keymap:vi/register#set-yank {
ble/keymap:vi/register#set "$@" || return 1
local reg=$1 type=$2 content=$3
if [[ $reg == '' || $reg == 34 ]]; then
ble/keymap:vi/register#set 48 "$type" "$content" # "0
fi
}
## @fn ble/keymap:vi/register#set-edit reg type content
## レジスタ "1 に文字列を登録します。
##
## content に改行が含まれる場合、または、特定の WIDGET の時、
## 元々レジスタ "1 - "8 にあった内容をレジスタ "2 - "9 に移動し、
## 新しい文字列をレジスタ "1 に登録します。
## それ以外の時、新しい文字列はレジスタ "- に登録します。
##
_ble_keymap_vi_register_49_widget_list=(
# %
ble/widget/vi-command/search-matchpair-or
ble/widget/vi-command/percentage-line
# `
ble/widget/vi-command/goto-mark
# / ? n N
ble/widget/vi-command/search-forward
ble/widget/vi-command/search-backward
ble/widget/vi-command/search-repeat
ble/widget/vi-command/search-reverse-repeat
# ( ) { }
# ToDo
)
function ble/keymap:vi/register#set-edit {
ble/keymap:vi/register#set "$@" || return 1
local reg=$1 type=$2 content=$3
if [[ $reg == '' || $reg == 34 ]]; then
local IFS=$_ble_term_IFS
local widget=${WIDGET%%["$_ble_term_IFS"]*}
if [[ $content == *$'\n'* || " $widget " == " ${_ble_keymap_vi_register_49_widget_list[*]} " ]]; then
local n
for ((n=9;n>=2;n--)); do
_ble_keymap_vi_register[48+n]=${_ble_keymap_vi_register[48+n-1]}
done
ble/keymap:vi/register#set 49 "$type" "$content" # "1
else
ble/keymap:vi/register#set 45 "$type" "$content" # "-
fi
fi
}
function ble/keymap:vi/register#play {
local reg=$1 value
if [[ $reg ]] && ((reg!=34)); then
value=${_ble_keymap_vi_register[reg]}
if [[ $value == */* ]]; then
value=${value#*/}
else
value=
return 1
fi
else
value=$_ble_edit_kill_ring
fi
local _ble_keymap_vi_register_onplay=1
local ret; ble/decode/charlog#decode "$value"
ble/widget/.MACRO "${ret[@]}"
return 0
}
function ble/keymap:vi/register#dump {
local k ret out=
local value type content
for k in 34 "${!_ble_keymap_vi_register[@]}"; do
if ((k==34)); then
type=$_ble_edit_kill_type
content=$_ble_edit_kill_ring
else
value=${_ble_keymap_vi_register[k]}
type=${value%%/*} content=${value#*/}
fi
ble/util/c2s "$k"; k=$ret
case $type in
(L) type=line ;;
(B:*) type=block ;;
(*) type=char ;;
esac
ble/string#escape-for-display "$content" sgr1="$_ble_term_rev":sgr0="$_ble_term_sgr0"
out=$out'"'$k' ('$type') '$ret$'\n'
done
ble/edit/info/show ansi "$out"
return 0
}
function ble/widget/vi-command:registers { ble/keymap:vi/register#dump; }
function ble/widget/vi-command/append-arg {
local ret ch=$1
if [[ ! $ch ]]; then
local n=${#KEYS[@]}
local code=$((KEYS[n?n-1:0]&_ble_decode_MaskChar))
((code==0)) && return 1
ble/util/c2s "$code"; ch=$ret
fi
ble/util/assert '[[ ! ${ch//[0-9]} ]]'
# 0
if [[ $ch == 0 && ! $_ble_edit_arg ]]; then
ble/widget/vi-command/beginning-of-line
return "$?"
fi
_ble_edit_arg="$_ble_edit_arg$ch"
return 0
}
function ble/widget/vi-command/register {
_ble_decode_key__hook="ble/widget/vi-command/register.hook"
}
function ble/widget/vi-command/register.hook {
local key=$1
ble/keymap:vi/clear-arg
local ret
if ble/keymap:vi/k2c "$key" && local c=$ret; then
if ((65<=c&&c<91)); then # A-Z
_ble_keymap_vi_reg=+$((c+32))
return 0
elif ((97<=c&&c<123||48<=c&&c<58||c==45||c==58||c==46||c==37||c==35||c==61||c==42||c==43||c==126||c==95||c==47)); then # a-z 0-9 - : . % # = * + ~ _ /
_ble_keymap_vi_reg=$c
return 0
elif ((c==34)); then # ""
# Note: vim の内部的には "" を指定するのと何も指定しないのは区別される。
# 例えば diw"y. は "y に記録されるが ""diw"y. は "" に記録される。
_ble_keymap_vi_reg=$c
return 0
fi
fi
ble/widget/vi-command/bell
return 1
}
_ble_keymap_vi_reg_record=
_ble_keymap_vi_reg_record_char=
_ble_keymap_vi_reg_record_play=0
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_reg_record \
_ble_keymap_vi_reg_record_char \
_ble_keymap_vi_reg_record_play
# nmap q
function ble/widget/vi_nmap/record-register {
# レジスタに含まれる q は再生中には何も起こさない
if [[ $_ble_keymap_vi_register_onplay ]]; then
ble/keymap:vi/clear-arg
ble/keymap:vi/adjust-command-mode
return 0
fi
if [[ $_ble_keymap_vi_reg_record ]]; then
ble/keymap:vi/clear-arg
local -a ret
ble/decode/charlog#end-exclusive-depth1
ble/decode/charlog#encode "${ret[@]}"
ble/keymap:vi/register#set "$_ble_keymap_vi_reg_record" q "$ret"
_ble_keymap_vi_reg_record=
ble/keymap:vi/update-mode-indicator
else
_ble_decode_key__hook="ble/widget/vi_nmap/record-register.hook"
fi
}
function ble/widget/vi_nmap/record-register.hook {
local key=$1 ret
ble/keymap:vi/clear-arg
# check register
local reg= c=
if ble/keymap:vi/k2c "$key" && c=$ret; then
if ((65<=c&&c<91)); then # q{A-Z}
reg=+$((c+32))
elif ((48<=c&&c<58||97<=c&&c<123)); then # q{0-9a-z}
reg=$c
elif ((c==34)); then # q"
reg=$c
fi
fi
if [[ ! $reg ]]; then
ble/widget/vi-command/bell "invalid register key=$key"
return 1
fi
# start logging
if ! ble/decode/charlog#start vi-macro; then
ble/widget/.bell 'vi-macro: the logging system is currently busy'
return 1
fi
# update status
ble/util/c2s "$c"
_ble_keymap_vi_reg_record=$reg
_ble_keymap_vi_reg_record_char=$ret
ble/keymap:vi/update-mode-indicator
return 0
}
# nmap @
function ble/widget/vi_nmap/play-register {
_ble_decode_key__hook="ble/widget/vi_nmap/play-register.hook"
}
function ble/widget/vi_nmap/play-register.hook {
ble/keymap:vi/clear-arg
local depth=$_ble_keymap_vi_reg_record_play
if ((depth>=bleopt_keymap_vi_macro_depth)) || ble/util/is-stdin-ready; then
return 1 # 無限ループを防ぐため
fi
local _ble_keymap_vi_reg_record_play=$((depth+1))
local key=$1
local ret
if ble/keymap:vi/k2c "$key" && local c=$ret; then
((65<=c&&c<91)) && ((c+=32)) # A-Z -> a-z
if ((48<=c&&c<58||97<=c&&c<123)); then # 0-9a-z
ble/keymap:vi/register#play "$c" && return 0
fi
fi
ble/widget/vi-command/bell
return 1
}
function ble/widget/vi-command/operator {
local ret opname=$1
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
local ARG FLAG REG; ble/keymap:vi/get-arg ''
# ※FLAG はユーザにより設定されているかもしれないが無視
local a=$_ble_edit_ind b=$_ble_edit_mark
((a<=b||(a=_ble_edit_mark,b=_ble_edit_ind)))
ble/widget/vi_xmap/.save-visual-state
local ble_keymap_vi_mark_active=$_ble_edit_mark_active # used in call-operator-blockwise
local mark_type=${_ble_edit_mark_active%+}
ble/widget/vi_xmap/exit
local ble_keymap_vi_opmode=$mark_type
if [[ $mark_type == vi_line ]]; then
ble/keymap:vi/call-operator-linewise "$opname" "$a" "$b" "$ARG" "$REG"
elif [[ $mark_type == vi_block ]]; then
ble/keymap:vi/call-operator-blockwise "$opname" "$a" "$b" "$ARG" "$REG"
else
local end=$b
((end<${#_ble_edit_str}&&end++))
ble/keymap:vi/call-operator-charwise "$opname" "$a" "$end" "$ARG" "$REG"
fi; local ext=$?
((ext==147)) && return 147
((ext)) && ble/widget/.bell
ble/keymap:vi/adjust-command-mode
return "$ext"
elif [[ $_ble_decode_keymap == vi_nmap ]]; then
ble/decode/keymap/push vi_omap
_ble_keymap_vi_oparg=$_ble_edit_arg
_ble_keymap_vi_opfunc=$opname
_ble_edit_arg=
ble/keymap:vi/update-mode-indicator
elif [[ $_ble_decode_keymap == vi_omap ]]; then
local opname1=${_ble_keymap_vi_opfunc%%:*}
if [[ $opname == "$opname1" ]]; then
# 2つの同じオペレータ (yy, dd, cc, etc.) = 行指向の処理
ble/widget/vi_nmap/linewise-operator "$_ble_keymap_vi_opfunc"
else
ble/keymap:vi/clear-arg
ble/widget/vi-command/bell
return 1
fi
fi
return 0
}
function ble/widget/vi_nmap/linewise-operator {
local opname=${1%%:*} opflags=${1#*:}
local ARG FLAG REG; ble/keymap:vi/get-arg 1 # _ble_edit_arg is consumed here
if ((ARG==1)) || [[ ${_ble_edit_str:_ble_edit_ind} == *$'\n'* ]]; then
if [[ :$opflags: == *:vi_char:* || :$opflags: == *:vi_block:* ]]; then
local beg=$_ble_edit_ind
local ret; ble-edit/content/find-logical-bol "$beg" "$((ARG-1))"; local end=$ret
((beg<=end)) || local beg=$end end=$beg
if [[ :$opflags: == *:vi_block:* ]]; then
ble/keymap:vi/call-operator-blockwise "$opname" "$beg" "$end" '' "$REG"
else
ble/keymap:vi/call-operator-charwise "$opname" "$beg" "$end" '' "$REG"
fi
else
ble/keymap:vi/call-operator-linewise "$opname" "$_ble_edit_ind" "$_ble_edit_ind:$((ARG-1))" '' "$REG"; local ext=$?
fi
if ((ext==0)); then
ble/keymap:vi/adjust-command-mode
return 0
elif ((ext==147)); then
return 147
fi
fi
ble/widget/vi-command/bell
return 1
}
# nmap Y
function ble/widget/vi_nmap/copy-current-line {
ble/widget/vi_nmap/linewise-operator y
}
function ble/widget/vi_nmap/kill-current-line {
ble/widget/vi_nmap/linewise-operator d
}
# nmap S
function ble/widget/vi_nmap/kill-current-line-and-insert {
ble/widget/vi_nmap/linewise-operator c
}
# nmap: 0, <home>
function ble/widget/vi-command/beginning-of-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret; ble-edit/content/find-logical-bol; local beg=$ret
ble/widget/vi-command/exclusive-goto.impl "$beg" "$FLAG" "$REG" nobell
}
#------------------------------------------------------------------------------
# Operators
## オペレータは以下の形式の関数として定義される。
##
## @fn ble/keymap:vi/operator:名称 a b context [count [reg]]
##
## @param[in] a b
## 範囲の開始点と終了点。終了点は開始点以降にあることが保証される。
## context が 'line' のとき、それぞれ行頭・行末にあることが保証される。
## ただし、行末に改行があるときは b は次の行頭を指す。
##
## @param[in] context
## 範囲の種類を表す文字列。char, line, block の何れか。
##
## @param[in] count
## オペレータの操作に対する引数。
## これはビジュアルモードで指定される。
##
## @var[in,out] beg end
## 範囲の開始点と終了点。a b と同一の値。
## 行指向オペレータのとき範囲が拡大されることがある。
## その時 beg に拡大後の開始点を返す。
##
## @var[out] ble_keymap_vi_operator_index
## オペレータ作用後のカーソル位置を明示するとき、
## オペレータ内部でこの変数に値を設定する。
##
## @exit
## operator 関数が終了ステータス 147 を返したとき、
## operator が非同期に入力を読み取ることを表す。
## 147 を返した operator は、実際に操作が完了した時に:
##
## 1 ble/keymap:vi/mark/end-edit-area を呼び出す必要がある。
## 2 適切な位置にカーソルを移動する必要がある。
##
##
## オペレータは現在以下の4箇所で呼び出されている。
##
## - ble/widget/vi-command/linewise-range.impl
## - ble/keymap:vi/call-operator
## - ble/keymap:vi/call-operator-charwise
## - ble/keymap:vi/call-operator-linewise
## - ble/keymap:vi/call-operator-blockwise
## @fn ble/keymap:vi/call-operator op beg end type arg reg
## @fn ble/keymap:vi/call-operator-charwise op beg end arg reg
## @fn ble/keymap:vi/call-operator-linewise op beg end arg reg
## @fn ble/keymap:vi/call-operator-blockwise op beg end arg reg
##
## @var[in] ble_keymap_vi_mark_active
## オペレータ作用前の $_ble_edit_mark_active を指定する。
## call-operator-blockwise での矩形領域を決定するのに用いる。
## 演算子の呼び出し時には既に $_ble_edit_mark_active は
## 作用後の値に変わっていることに注意する。
##
function ble/keymap:vi/call-operator {
ble/keymap:vi/mark/start-edit-area
local _ble_keymap_vi_mark_suppress_edit=1
ble/keymap:vi/operator:"$@"; local ext=$?
ble/util/unlocal _ble_keymap_vi_mark_suppress_edit
ble/keymap:vi/mark/end-edit-area
if ((ext==0)); then
if ble/is-function ble/keymap:vi/operator:"$1".record; then
ble/keymap:vi/operator:"$1".record
else
ble/keymap:vi/repeat/record
fi
fi
return "$ext"
}
function ble/keymap:vi/call-operator-charwise {
local ch=$1 beg=$2 end=$3 arg=$4 reg=$5
((beg<=end||(beg=$3,end=$2)))
if ble/is-function ble/keymap:vi/operator:"$ch"; then
local ble_keymap_vi_operator_index=
ble/keymap:vi/call-operator "$ch" "$beg" "$end" char "$arg" "$reg"; local ext=$?
((ext==147)) && return 147
local index=${ble_keymap_vi_operator_index:-$beg}
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
_ble_edit_ind=$index
return 0
else
return 1
fi
}
function ble/keymap:vi/call-operator-linewise {
local ch=$1 a=$2 b=$3 arg=$4 reg=$5 ia=0 ib=0
[[ $a == *:* ]] && local a=${a%%:*} ia=${a#*:}
[[ $b == *:* ]] && local b=${b%%:*} ib=${b#*:}
local ret
ble-edit/content/find-logical-bol "$a" "$ia"; local beg=$ret
ble-edit/content/find-logical-eol "$b" "$ib"; local end=$ret
if ble/is-function ble/keymap:vi/operator:"$ch"; then
local ble_keymap_vi_operator_index=
((end<${#_ble_edit_str}&&end++))
ble/keymap:vi/call-operator "$ch" "$beg" "$end" line "$arg" "$reg"; local ext=$?
((ext==147)) && return 147
# index
if [[ $ble_keymap_vi_operator_index ]]; then
local index=$ble_keymap_vi_operator_index
else
ble-edit/content/find-logical-bol "$beg"; beg=$ret # operator 中で beg が変更されているかも
ble-edit/content/find-non-space "$beg"; local index=$ret
fi
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
_ble_edit_ind=$index
return 0
else
return 1
fi
}
function ble/keymap:vi/call-operator-blockwise {
local ch=$1 beg=$2 end=$3 arg=$4 reg=$5
if ble/is-function ble/keymap:vi/operator:"$ch"; then
local mark_active=${ble_keymap_vi_mark_active:-vi_block}
local sub_ranges sub_x1 sub_x2
_ble_edit_mark_active=$mark_active ble/keymap:vi/extract-block "$beg" "$end"
local nrange=${#sub_ranges[@]}
((nrange)) || return 1
local ble_keymap_vi_operator_index=
local beg=${sub_ranges[0]}; beg=${beg%%:*}
local end=${sub_ranges[nrange-1]}; end=${end#*:}; end=${end%%:*}
ble/keymap:vi/call-operator "$ch" "$beg" "$end" block "$arg" "$reg"
((ext==147)) && return 147
local index=${ble_keymap_vi_operator_index:-$beg}
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
_ble_edit_ind=$index
return 0
else
return 1
fi
}
function ble/keymap:vi/operator:d {
local context=$3 arg=$4 reg=$5 # beg end は上書きする
if [[ $context == line ]]; then
ble/keymap:vi/register#set-edit "$reg" L "${_ble_edit_str:beg:end-beg}" || return 1
# 最後の行が削除される時は前の行の非空白行頭まで後退
if ((end==${#_ble_edit_str}&&beg>0)); then
# fix start position
local ret
((beg--))
ble-edit/content/find-logical-bol "$beg"
ble-edit/content/find-non-space "$ret"
ble_keymap_vi_operator_index=$ret
fi
ble/widget/.delete-range "$beg" "$end"
elif [[ $context == block ]]; then
local -a afill=() atext=() arep=()
local sub shift=0 slpad0=
local smin smax slpad srpad sfill stext
for sub in "${sub_ranges[@]}"; do
stext=${sub#*:*:*:*:*:}
ble/string#split sub : "$sub"
smin=${sub[0]} smax=${sub[1]}
slpad=${sub[2]} srpad=${sub[3]}
sfill=${sub[4]}
[[ $slpad0 ]] || slpad0=$slpad # 最初の slpad
ble/array#push afill "$sfill"
ble/array#push atext "$stext"
local ret; ble/string#repeat ' ' "$((slpad+srpad))"
ble/array#push arep "$((smin+shift)):$((smax+shift)):$ret"
((shift+=(slpad+srpad)-(smax-smin)))
done
# yank
IFS=$'\n' builtin eval 'local yank_content="${atext[*]-}"'
local IFS=$_ble_term_IFS
local yank_type=B:"${afill[*]-}"
ble/keymap:vi/register#set-edit "$reg" "$yank_type" "$yank_content" || return 1
# delete
local rep
for rep in "${arep[@]}"; do
smin=${rep%%:*}; rep=${rep:${#smin}+1}
smax=${rep%%:*}; rep=${rep:${#smax}+1}
ble/widget/.replace-range "$smin" "$smax" "$rep"
done
((beg+=slpad)) # fix start position
else
if ((beg<end)); then
if [[ $ble_keymap_vi_opmode != vi_char && ${_ble_edit_str:beg:end-beg} == *$'\n'* ]]; then
# d の例外動作: 文字単位で開始点と終了点が異なる行で、
# 開始点より前・終了点より後に空白しかない時、行指向で処理する。
if local rex=$'(^|\n)([ \t]*)$'; [[ ${_ble_edit_str::beg} =~ $rex ]]; then
local prefix=${BASH_REMATCH[2]}
if rex=$'^[ \t]*(\n|$)'; [[ ${_ble_edit_str:end} =~ $rex ]]; then
local suffix=$BASH_REMATCH
((beg-=${#prefix},end+=${#suffix}))
ble/keymap:vi/operator:d "$beg" "$end" line "$arg" "$reg"
return "$?"
fi
fi
fi
ble/keymap:vi/register#set-edit "$reg" '' "${_ble_edit_str:beg:end-beg}" || return 1
ble/widget/.delete-range "$beg" "$end"
fi
fi
return 0
}
function ble/keymap:vi/operator:c {
local context=$3 arg=$4 reg=$5 # beg は上書き対象
if [[ $context == line ]]; then
ble/keymap:vi/register#set-edit "$reg" L "${_ble_edit_str:beg:end-beg}" || return 1
local end2=$end
((end2)) && [[ ${_ble_edit_str:end2-1:1} == $'\n' ]] && ((end2--))
local indent= ret
ble-edit/content/find-non-space "$beg"; local nol=$ret
((beg<nol)) && indent=${_ble_edit_str:beg:nol-beg}
ble/widget/.replace-range "$beg" "$end2" "$indent"
ble/widget/vi_nmap/.insert-mode
elif [[ $context == block ]]; then
ble/keymap:vi/operator:d "$@" || return 1 # @var beg will be overwritten here
# operator:d によってずれた矩形領域を修正する。
# 一から計算し直すのは面倒なので sub_ranges を直接弄る。
# 実のところ block-insert-mode insert は sub_ranges[0] の smin と sub_x1
# しか参照しないので、sub_ranges[0] だけ修正すれば良い。
local sub=${sub_ranges[0]}
local smin=${sub%%:*} sub=${sub#*:}
local smax=${sub%%:*} sub=${sub#*:}
local slpad=${sub%%:*} sub=${sub#*:}
((smin+=slpad,smax=smin,slpad=0))
sub_ranges[0]=$smin:$smax:$slpad:$sub
ble/widget/vi_xmap/block-insert-mode.impl insert
else
local ble_keymap_vi_opmode=vi_char
ble/keymap:vi/operator:d "$@" || return 1
ble/widget/vi_nmap/.insert-mode
fi
return 0
}
function ble/keymap:vi/operator:y.record { return 0; }
function ble/keymap:vi/operator:y {
local beg=$1 end=$2 context=$3 arg=$4 reg=$5
local yank_type= yank_content=
if [[ $context == line ]]; then
ble_keymap_vi_operator_index=$_ble_edit_ind # operator:y では現在位置を動かさない
yank_type=L
yank_content=${_ble_edit_str:beg:end-beg}
elif [[ $context == block ]]; then
local sub
local -a afill=() atext=()
for sub in "${sub_ranges[@]}"; do
local sub4=${sub#*:*:*:*:}
local sfill=${sub4%%:*} stext=${sub4#*:}
ble/array#push afill "$sfill"
ble/array#push atext "$stext"
done
IFS=$'\n' builtin eval 'local yank_content="${atext[*]-}"'
local IFS=$_ble_term_IFS
yank_type=B:"${afill[*]-}"
else
yank_type=
yank_content=${_ble_edit_str:beg:end-beg}
fi
ble/keymap:vi/register#set-yank "$reg" "$yank_type" "$yank_content" || return 1
ble/keymap:vi/mark/commit-edit-area "$beg" "$end"
return 0
}
function ble/keymap:vi/operator:tr.impl {
local beg=$1 end=$2 context=$3 filter=$4
if [[ $context == block ]]; then
local isub=${#sub_ranges[@]}
while ((isub--)); do
ble/string#split sub : "${sub_ranges[isub]}"
local smin=${sub[0]} smax=${sub[1]}
local ret; "$filter" "${_ble_edit_str:smin:smax-smin}"
ble/widget/.replace-range "$smin" "$smax" "$ret"
done
else
local ret; "$filter" "${_ble_edit_str:beg:end-beg}"
ble/widget/.replace-range "$beg" "$end" "$ret"
fi
return 0
}
function ble/keymap:vi/operator:u {
ble/keymap:vi/operator:tr.impl "$1" "$2" "$3" ble/string#tolower
}
function ble/keymap:vi/operator:U {
ble/keymap:vi/operator:tr.impl "$1" "$2" "$3" ble/string#toupper
}
function ble/keymap:vi/operator:toggle_case {
ble/keymap:vi/operator:tr.impl "$1" "$2" "$3" ble/string#toggle-case
}
function ble/keymap:vi/operator:rot13 {
ble/keymap:vi/operator:tr.impl "$1" "$2" "$3" ble/keymap:vi/string#encode-rot13
}
## @fn ble/keymap:vi/expand-range-for-linewise-operator
## @var[in,out] beg, end
function ble/keymap:vi/expand-range-for-linewise-operator {
local ret
# 行頭補正:
ble-edit/content/find-logical-bol "$beg"; beg=$ret
# 行末補正:
# 行前進時は非空白行頭以前に end がある場合はその行は無視
# 行後退時は行頭に end (_ble_edit_ind) がある場合はその行は無視
# 同一行内の移動の場合は無条件にその行は含まれる。
ble-edit/content/find-logical-bol "$end"; local bol2=$ret
ble-edit/content/find-non-space "$bol2"; local nol2=$ret
if ((beg<bol2&&_ble_edit_ind<=bol2&&end<=nol2)); then
end=$bol2
else
ble-edit/content/find-logical-eol "$end"; end=$ret
[[ ${_ble_edit_str:end:1} == $'\n' ]] && ((end++))
fi
}
#--------------------------------------
# Indent operators < >
## @fn ble/keymap:vi/string#increase-indent
## @var[out] ret
function ble/keymap:vi/string#increase-indent {
local text=$1 delta=$2
local space=$' \t' it=${bleopt_tab_width:-$_ble_term_it}
local arr; ble/string#split-lines arr "$text"
local -a arr2=()
local line indent i len x r
for line in "${arr[@]}"; do
indent=${line%%[!$space]*}
line=${line:${#indent}}
((x=0))
if [[ $indent ]]; then
((len=${#indent}))
for ((i=0;i<len;i++)); do
if [[ ${indent:i:1} == ' ' ]]; then
((x++))
else
((x=(x+it)/it*it))
fi
done
fi
((x+=delta,x<0&&(x=0)))
indent=
if ((x)); then
if ((bleopt_indent_tabs&&(r=x/it))); then
ble/string#repeat $'\t' "$r"
indent=$ret
((x%=it))
fi
if ((x)); then
ble/string#repeat ' ' "$x"
indent=$indent$ret
fi
fi
ble/array#push arr2 "$indent$line"
done
IFS=$'\n' builtin eval 'ret="${arr2[*]-}"'
}
## @fn ble/keymap:vi/operator:indent.impl/increase-block-indent width
## @param[in] width
## @var[in] sub_ranges
function ble/keymap:vi/operator:indent.impl/increase-block-indent {
local width=$1
local isub=${#sub_ranges[@]}
local sub smin slpad ret
while ((isub--)); do
ble/string#split sub : "${sub_ranges[isub]}"
smin=${sub[0]} slpad=${sub[2]}
ble/string#repeat ' ' "$((slpad+width))"
ble/widget/.replace-range "$smin" "$smin" "$ret"
done
}
## @fn ble/keymap:vi/operator:indent.impl/decrease-graphical-block-indent width
## @param[in] width
## @var[in] sub_ranges
function ble/keymap:vi/operator:indent.impl/decrease-graphical-block-indent {
local width=$1
local it=${bleopt_tab_width:-$_ble_term_it} cols=$_ble_textmap_cols
local sub smin slpad ret
local -a replaces=()
local isub=${#sub_ranges[@]}
while ((isub--)); do
ble/string#split sub : "${sub_ranges[isub]}"
smin=${sub[0]} slpad=${sub[2]}
ble-edit/content/find-non-space "$smin"; local nsp=$ret
((smin<nsp)) || continue
local ax ay bx by
ble/textmap#getxy.out --prefix=a "$smin"
ble/textmap#getxy.out --prefix=b "$nsp"
local w=$(((bx-ax)-(by-ay)*cols-width))
((w<slpad)) && w=$slpad
local ins=
if ((w)); then
local r
if ((bleopt_indent_tabs&&(r=(ax+w)/it-ax/it))); then
ble/string#repeat $'\t' "$r"; ins=$ret
((w=(ax+w)%it))
fi
if ((w)); then
ble/string#repeat ' ' "$w"
ins=$ins$ret
fi
fi
ble/array#push replaces "$smin:$nsp:$ins"
done
local rep
for rep in "${replaces[@]}"; do
ble/string#split rep : "$rep"
ble/widget/.replace-range "${rep[@]::3}"
done
}
## @fn ble/keymap:vi/operator:indent.impl/decrease-logical-block-indent width
## @param[in] width
## @var[in] sub_ranges
function ble/keymap:vi/operator:indent.impl/decrease-logical-block-indent {
# タブは幅 it で固定と見做して削除する
local width=$1
local it=${bleopt_tab_width:-$_ble_term_it}
local sub smin ret nsp
local isub=${#sub_ranges[@]}
while ((isub--)); do
ble/string#split sub : "${sub_ranges[isub]}"
smin=${sub[0]}
ble-edit/content/find-non-space "$smin"; nsp=$ret
((smin<nsp)) || continue
local stext=${_ble_edit_str:smin:nsp-smin}
local i=0 n=${#stext} c=0 pad=0
for ((i=0;i<n;i++)); do
if [[ ${stext:i:1} == $'\t' ]]; then
((c+=it))
else
((c++))
fi
if ((c>=width)); then
pad=$((c-width))
nsp=$((smin+i+1))
break
fi
done
local padding=
((pad)) && { ble/string#repeat ' ' "$pad"; padding=$ret; }
ble/widget/.replace-range "$smin" "$nsp" "$padding"
done
}
function ble/keymap:vi/operator:indent.impl {
local delta=$1 context=$2
((delta)) || return 0
if [[ $context == block ]]; then
if ((delta>=0)); then
ble/keymap:vi/operator:indent.impl/increase-block-indent "$delta"
elif ble/edit/use-textmap; then
ble/keymap:vi/operator:indent.impl/decrease-graphical-block-indent "$((-delta))"
else
ble/keymap:vi/operator:indent.impl/decrease-logical-block-indent "$((-delta))"
fi
else
[[ $context == char ]] && ble/keymap:vi/expand-range-for-linewise-operator
((beg<end)) && [[ ${_ble_edit_str:end-1:1} == $'\n' ]] && ((end--))
local ret
ble/keymap:vi/string#increase-indent "${_ble_edit_str:beg:end-beg}" "$delta"; local content=$ret
ble/widget/.replace-range "$beg" "$end" "$content"
if [[ $context == char ]]; then
ble-edit/content/find-non-space "$beg"
ble_keymap_vi_operator_index=$ret
fi
fi
return 0
}
function ble/keymap:vi/operator:indent-left {
local context=$3 arg=${4:-1}
ble/keymap:vi/operator:indent.impl "$((-bleopt_indent_offset*arg))" "$context"
}
function ble/keymap:vi/operator:indent-right {
local context=$3 arg=${4:-1}
ble/keymap:vi/operator:indent.impl "$((bleopt_indent_offset*arg))" "$context"
}
#--------------------------------------
# Fold operators gq gw
## @fn ble/keymap:vi/string#measure-width text
## 指定した文字列の表示上の幅を計測します。
##
## @param[in] text
## @var[out] ret
##
## 折り返し処理は行いません。
## タブや改行などの特別処理は行いません。
## C0 文字は 2 文字として取り扱います。
## C1 文字は 4 文字として取り扱います。
function ble/keymap:vi/string#measure-width {
local text=$1 iN=${#1} i=0 s=0
while ((i<iN)); do
if ble/util/isprint+ "${text:i}"; then
((s+=${#BASH_REMATCH},
i+=${#BASH_REMATCH}))
else
ble/util/s2c "${text:i:1}"
ble/util/c2w-edit "$ret"
((s+=ret,i++))
fi
done
ret=$s
}
## @fn ble/keymap:vi/string#fold/.get-interval text x
## 単語間のスペースの表示上の幅を計算します。
##
## @param[in] text
## スペース・タブで構成される文字列を指定します。
## @param[in] x
## 画面上の初期位置を指定します。
## @var[out] ret
##
function ble/keymap:vi/string#fold/.get-interval {
local text=$1 x=$2
local it=${bleopt_tab_width:-$_ble_term_it}
local i=0 iN=${#text}
for ((i=0;i<iN;i++)); do
if [[ ${text:i:1} == $'\t' ]]; then
((x=(x/it+1)*it))
else
((x++))
fi
done
ret=$((x-$2))
}
## @fn ble/keymap:vi/string#fold text [cols]
## @param[in] text
## @param[in,opt] cols [既定値 ${COLUMNS:-80}]
## 折り返す幅を指定します。
## cols-1 列以内に表示文字が収まる様に折り返し処理されます。
## 但し長い単語の途中で折り返しは起こりません。
## @var[out] ret
function ble/keymap:vi/string#fold {
local text=$1
local cols=${2:-${COLUMNS-80}}
local sp=$' \t' nl=$'\n'
# 途中状態について
# @var i text 内の現在処理している位置
# @var out 今までに確定した結果文字列
# @var otmp 保留中の単語間スペース。次の単語が来て初めて確定する。
# @var x $out を処理した後の表示横位置
# @var xtmp $out$otmp を処理した後の表示横位置
# @var isfirst 初回の正規表現一致かどうか
# @var indent インデント (改行直後に挿入する空白類)
# @var xindent インデント挿入直後の表示横位置
local i=0 out= otmp= x=0 xtmp=0
local isfirst=1 indent= xindent=0
local rex='^([^'$nl$sp']+)|^(['$sp']+)|^.'
while [[ ${text:i} =~ $rex ]]; do
((i+=${#BASH_REMATCH}))
if [[ ${BASH_REMATCH[1]} ]]; then
# 単語
local word=${BASH_REMATCH[1]}
ble/keymap:vi/string#measure-width "$word"
if ((xtmp+ret<cols||xtmp<=xindent)); then
out=$out$otmp$word
((x=xtmp+=ret))
else
out=$out$'\n'$indent$word
((x=xtmp=xindent+ret))
fi
otmp=
else
local w=1
if [[ ${BASH_REMATCH[2]} ]]; then
[[ $otmp ]] && continue # 改行直後の空白は無視
# 単語間のスペース
otmp=${BASH_REMATCH[2]}
ble/keymap:vi/string#fold/.get-interval "$otmp" "$x"; w=$ret
[[ $isfirst ]] && indent=$otmp xindent=$ret # インデント記録
else
# 改行は空白に置換。既存の空白 otmp は消去。
otmp=' ' w=1
fi
if ((x+w<cols)); then
((xtmp=x+w))
else
((xtmp=xindent))
otmp=$'\n'$indent
fi
fi
isfirst=
done
ret=$out
}
## @fn ble/keymap:vi/operator:fold/.fold-paragraphwise text [cols]
## @var[out] ret
function ble/keymap:vi/operator:fold/.fold-paragraphwise {
local text=$1
local cols=${2:-${COLUMNS:-80}}
local nl=$'\n' sp=$' \t'
local rex_paragraph='^((['$sp']*'$nl')*)(['$sp']*[^'$sp$nl'][^'$nl']*('$nl'|$))+'
local i=0 out=
while [[ ${text:i} =~ $rex_paragraph ]]; do
((i+=${#BASH_REMATCH}))
local rematch1=${BASH_REMATCH[1]}
local len1=${#rematch1}
local paragraph=${BASH_REMATCH:len1}
# fold (実はここだけ変えれば paragraphwise の様々な処理を実装できる)
ble/keymap:vi/string#fold "$paragraph" "$cols"
paragraph=${ret%$'\n'}$'\n'
out=$out$rematch1$paragraph
done
ret=$out${text:i}
}
function ble/keymap:vi/operator:fold.impl {
local context=$1 opts=$2
local ret
[[ $context != line ]] && ble/keymap:vi/expand-range-for-linewise-operator
local old=${_ble_edit_str:beg:end-beg} oind=$_ble_edit_ind
local cols=${COLUMNS:-80}; ((cols>80&&(cols=80)))
ble/keymap:vi/operator:fold/.fold-paragraphwise "$old" "$cols"; local new=$ret
ble/widget/.replace-range "$beg" "$end" "$new"
# 変換後のカーソル位置を修正。
if [[ :$opts: == *:preserve_point:* ]]; then
# gw: もともとカーソルが合った文字に移動。
if ((end<=oind)); then
ble_keymap_vi_operator_index=$((beg+${#new}))
elif ((beg<oind)); then
ble/keymap:vi/operator:fold/.fold-paragraphwise "${old::oind-beg}" "$cols"
ble_keymap_vi_operator_index=$((beg+${#ret}))
fi
else
# gq: 最終行の非空白行頭 (gq) に移動。
if [[ $new ]]; then
ble-edit/content/find-logical-bol "$((beg+${#new}-1))"
ble-edit/content/find-non-space "$ret"
ble_keymap_vi_operator_index=$ret
fi
fi
return 0
}
function ble/keymap:vi/operator:fold {
local context=$3
ble/keymap:vi/operator:fold.impl "$context"
}
function ble/keymap:vi/operator:fold-preserve-point {
local context=$3
ble/keymap:vi/operator:fold.impl "$context" preserve_point
}
#--------------------------------------
# Filter operator: !
_ble_keymap_vi_filter_args=()
_ble_keymap_vi_filter_repeat=()
_ble_keymap_vi_filter_history=()
_ble_keymap_vi_filter_history_edit=()
_ble_keymap_vi_filter_history_dirt=()
_ble_keymap_vi_filter_history_index=0
function ble/highlight/layer:region/mark:vi_filter/get-face {
face=region_target
}
function ble/keymap:vi/operator:filter/.cache-repeat {
local -a _ble_keymap_vi_repeat _ble_keymap_vi_repeat_irepeat
ble/keymap:vi/repeat/record-normal
_ble_keymap_vi_filter_repeat=("${_ble_keymap_vi_repeat[@]}")
}
function ble/keymap:vi/operator:filter/.record-repeat {
ble/keymap:vi/repeat/record-special && return 0
local command=$1
_ble_keymap_vi_repeat=("${_ble_keymap_vi_filter_repeat[@]}")
_ble_keymap_vi_repeat_irepeat=()
_ble_keymap_vi_repeat[10]=$command
}
function ble/keymap:vi/operator:filter {
local context=$3
[[ $context != line ]] && ble/keymap:vi/expand-range-for-linewise-operator
_ble_keymap_vi_filter_args=("$beg" "$end" "${@:3}")
if [[ $_ble_keymap_vi_repeat_invoke ]]; then
# nmap . によって繰り返しが要求された時
local command=${_ble_keymap_vi_repeat[10]}
ble/keymap:vi/operator:filter/.hook "$command"
return "$?"
else
# 通常の呼び出し時
ble/keymap:vi/operator:filter/.cache-repeat
_ble_edit_ind=$beg
_ble_edit_mark=$end
_ble_edit_mark_active=vi_filter
ble/keymap:vi/async-commandline-mode 'ble/keymap:vi/operator:filter/.hook'
_ble_edit_PS1='!'
ble/history/set-prefix _ble_keymap_vi_filter
_ble_keymap_vi_cmap_before_widget=ble/keymap:vi/commandline/empty-cancel.hook
_ble_keymap_vi_cmap_cancel_hook=ble/keymap:vi/operator:filter/cancel.hook
_ble_syntax_lang=bash
_ble_highlight_layer_list=(plain syntax region overwrite_mode)
return 147
fi
}
function ble/keymap:vi/operator:filter/cancel.hook {
_ble_edit_mark_active= # clear mark:vi_filter
}
function ble/keymap:vi/operator:filter/.hook {
local command=$1 # 入力されたコマンド
if [[ ! $command ]]; then
ble/widget/vi-command/bell
return 1
fi
local beg=${_ble_keymap_vi_filter_args[0]}
local end=${_ble_keymap_vi_filter_args[1]}
local context=${_ble_keymap_vi_filter_args[2]}
_ble_edit_mark_active= # clear mark:vi_filter
local old=${_ble_edit_str:beg:end-beg} new
old=${old%$'\n'}
if ! ble/util/assign new 'builtin eval -- "$command" <<< "$old" 2>/dev/null'; then
ble/widget/vi-command/bell
return 1
fi
new=${new%$'\n'}
((end<${#_ble_edit_str})) && new=$new$'\n'
ble/widget/.replace-range "$beg" "$end" "$new"
_ble_edit_ind=$beg
if [[ $context == line ]]; then
ble/widget/vi-command/first-non-space
else
ble/keymap:vi/adjust-command-mode
fi
ble/keymap:vi/mark/set-previous-edit-area "$beg" "$((beg+${#new}))"
ble/keymap:vi/operator:filter/.record-repeat "$command"
return 0
}
#--------------------------------------
# User operator: g@
bleopt/declare -v keymap_vi_operatorfunc ''
function ble/keymap:vi/operator:map {
local context=$3
if [[ $bleopt_keymap_vi_operatorfunc ]]; then
local opfunc=ble/keymap:vi/operator:$bleopt_keymap_vi_operatorfunc
if ble/is-function "$opfunc"; then
"$opfunc" "$@"
return "$?"
fi
fi
return 1
}
#------------------------------------------------------------------------------
# Motions
#--------------------------------------
# Primitive motion
## @fn ble/widget/vi-command/exclusive-range.impl src dst flag reg nobell
## @fn ble/widget/vi-command/exclusive-goto.impl index flag reg nobell
## @fn ble/widget/vi-command/inclusive-goto.impl index flag reg nobell
##
## @param[in] src, dst
## 移動前の位置と移動先の位置を指定します。
##
## @param[in] flag
## オペレータ名を指定します。
##
## @param[in] reg
## レジスタ番号を指定します。
##
## @param[in] opts
## コロン区切りのオプションです。
## nobell 移動前と移動後の位置が同じときにベルを鳴らしません。
## inclusive 移動の既定の動作が inclusive である事を示します。
##
function ble/widget/vi-command/exclusive-range.impl {
local src=$1 dst=$2 flag=$3 reg=$4 opts=$5
if [[ $flag ]]; then
local opname=${flag%%:*} opflags=${flag#*:}
if [[ :$opflags: == *:vi_line:* ]]; then
local ble_keymap_vi_opmode=vi_line
ble/keymap:vi/call-operator-linewise "$opname" "$src" "$dst" '' "$reg"; local ext=$?
elif [[ :$opflags: == *:vi_block:* ]]; then
local ble_keymap_vi_opmode=vi_line
ble/keymap:vi/call-operator-blockwise "$opname" "$src" "$dst" '' "$reg"; local ext=$?
elif [[ :$opflags: == *:vi_char:* ]]; then
local ble_keymap_vi_opmode=vi_char
# 規則 o_v (omap v) の toggle inclusive/exclusive
if [[ :$opts: == *:inclusive:* ]]; then
((src<dst?dst--:(dst<src&&src--)))
else
if ((src<=dst)); then
((dst<${#_ble_edit_str})) &&
[[ ${_ble_edit_str:dst:1} != $'\n' ]] &&
((dst++))
else
((src<${#_ble_edit_str})) &&
[[ ${_ble_edit_str:src:1} != $'\n' ]] &&
((src++))
fi
fi
ble/keymap:vi/call-operator-charwise "$opname" "$src" "$dst" '' "$reg"; local ext=$?
else
local ble_keymap_vi_opmode=
ble/keymap:vi/call-operator-charwise "$opname" "$src" "$dst" '' "$reg"; local ext=$?
fi
((ext==147)) && return 147
((ext)) && ble/widget/.bell
ble/keymap:vi/adjust-command-mode
return "$ext"
else
ble/keymap:vi/needs-eol-fix "$dst" && ((dst--))
if ((dst!=_ble_edit_ind)); then
_ble_edit_ind=$dst
elif [[ :$opts: != *:nobell:* ]]; then
ble/widget/vi-command/bell
return 1
fi
ble/keymap:vi/adjust-command-mode
return 0
fi
}
function ble/widget/vi-command/exclusive-goto.impl {
local index=$1 flag=$2 reg=$3 opts=$4
if [[ $flag ]]; then
if ble-edit/content/bolp "$index"; then
local is_linewise=
if ((_ble_edit_ind<index)); then
# :help exclusive-linewise の規則1 (src<ind の時のみ)
((index--))
# :help exclusive-linewise の規則2
rex=$'(^|\n)[ \t]*$'
[[ ${_ble_edit_str::_ble_edit_ind} =~ $rex ]] &&
is_linewise=1
elif ((index<_ble_edit_ind)); then
# :help exclusive-linewise の規則2 (条件が異なる)
ble-edit/content/bolp &&
is_linewise=1
fi
if [[ $is_linewise ]]; then
ble/widget/vi-command/linewise-goto.impl "$index" "$flag" "$reg"
return "$?"
fi
fi
fi
ble/widget/vi-command/exclusive-range.impl "$_ble_edit_ind" "$index" "$flag" "$reg" "$opts"
}
function ble/widget/vi-command/inclusive-goto.impl {
local index=$1 flag=$2 reg=$3 opts=$4
if [[ $flag ]]; then
if ((_ble_edit_ind<=index)); then
ble-edit/content/eolp "$index" || ((index++))
else
ble-edit/content/eolp || ((_ble_edit_ind++))
fi
fi
ble/widget/vi-command/exclusive-range.impl "$_ble_edit_ind" "$index" "$flag" "$reg" "$opts:inclusive"
}
## @fn ble/widget/vi-command/linewise-range.impl p q flag reg opts
## @fn ble/widget/vi-command/linewise-goto.impl index flag reg opts
##
## @param[in] p, q
## 開始位置と終了位置を指定します。
## flag が設定されていない場合は q に移動します。
##
## @param[in] index
## 開始位置を _ble_edit_ind とし、
## 移動先または終了位置を指定します。
##
## index=indx:linex の形をしているとき、
## 基準の位置 indx から linex 行目を移動先とします。
## index=整数 の場合 index を含む行を移動先とします。
##
## @param[in] flag
## @param[in] reg
##
## @param[in] opts
## 以下のフィールドを似にに含むコロン区切りのリスト
##
## preserve_column
## require_multiline
## goto_bol
##
## bolx=NUMBER
## 既に計算済みの移動先 (index, q) の行の行頭がある場合はここに指定します。
##
## nolx=NUMBER
## 既に計算済みの移動先 (index, q) の行の非空白行頭位置がある場合はここに指定します。
##
function ble/widget/vi-command/linewise-range.impl {
local p=$1 q=$2 flag=$3 reg=$4 opts=$5
local ret
if [[ $q == *:* ]]; then
local qbase=${q%%:*} qline=${q#*:}
else
local qbase=$q qline=0
fi
local bolx=; ble/string#match ":$opts:" ':bolx=([0-9]+):' && bolx=${BASH_REMATCH[1]}
local nolx=; ble/string#match ":$opts:" ':nolx=([0-9]+):' && nolx=${BASH_REMATCH[1]}
# 移動時 (オペレータが設定されていない時)
if [[ ! $flag ]]; then
if [[ ! $nolx ]]; then
if [[ ! $bolx ]]; then
ble-edit/content/find-logical-bol "$qbase" "$qline"; bolx=$ret
fi
ble-edit/content/find-non-space "$bolx"; nolx=$ret
fi
ble-edit/content/nonbol-eolp "$nolx" && ((nolx--))
_ble_edit_ind=$nolx
ble/keymap:vi/adjust-command-mode
return 0
fi
local opname=${flag%%:*} opflags=${flag#*:}
if ! ble/is-function ble/keymap:vi/operator:"$opname"; then
ble/widget/vi-command/bell
return 1
fi
local bolp bolq=$bolx nolq=$nolx
ble-edit/content/find-logical-bol "$p"; bolp=$ret
[[ $bolq ]] || { ble-edit/content/find-logical-bol "$qbase" "$qline"; bolq=$ret; }
# jk+- で1行も移動できない場合は操作をキャンセルする。
# Note: qline を用いる場合は必ずしも望みどおり
# qline 行目が存在するとは限らないことに注意する。
if [[ :$opts: == *:require_multiline:* ]]; then
if ((bolq==bolp)); then
ble/widget/vi-command/bell
return 1
fi
fi
# オペレータ呼び出し
if [[ :$opflags: == *:vi_char:* || :$opflags: == *:vi_block:* ]]; then
# 行き先の決定
local beg=$p end
if [[ :$opts: == *:preserve_column:* ]]; then
local index
ble/keymap:vi/get-index-of-relative-line "$qbase" "$qline"; end=$index
elif [[ :$opts: == *:goto_bol:* ]]; then
end=$bolq
else
[[ $nolq ]] || { ble-edit/content/find-non-space "$bolq"; nolq=$ret; }
end=$nolq
fi
((beg<=end)) || local beg=$end end=$beg
if [[ :$opflags: == *:vi_block:* ]]; then
local ble_keymap_vi_opmode=vi_block
ble/keymap:vi/call-operator "$opname" "$beg" "$end" block '' "$reg"; local ext=$?
else
local ble_keymap_vi_opmode=vi_char
ble/keymap:vi/call-operator "$opname" "$beg" "$end" char '' "$reg"; local ext=$?
fi
if ((ext)); then
((ext==147)) && return 147
ble/widget/vi-command/bell
return "$ext"
fi
else
# 行指向の処理 (既定)
# 最初の行の行頭 beg と最後の行の行末 end
local beg end
if ((bolp<=bolq)); then
ble-edit/content/find-logical-eol "$bolq"; beg=$bolp end=$ret
else
ble-edit/content/find-logical-eol "$bolp"; beg=$bolq end=$ret
fi
((end<${#_ble_edit_str}&&end++))
local ble_keymap_vi_opmode=
[[ :$opflags: == *:vi_line:* ]] && ble_keymap_vi_opmode=vi_line
ble/keymap:vi/call-operator "$opname" "$beg" "$end" line '' "$reg"; local ext=$?
if ((ext)); then
((ext==147)) && return 147
ble/widget/vi-command/bell
return "$ext"
fi
# 範囲の先頭に移動
local ind=$_ble_edit_ind
if [[ $opname == [cd] ]]; then
# これらは常に first-non-space になる。
_ble_edit_ind=$beg
ble/widget/vi-command/first-non-space
elif [[ :$opts: == *:preserve_column:* ]]; then # j k
if ((beg<ind)); then
ble/string#count-char "${_ble_edit_str:beg:ind-beg}" $'\n'
((ret=-ret))
elif ((ind<beg)); then
ble/string#count-char "${_ble_edit_str:ind:beg-ind}" $'\n'
else
ret=0
fi
if ((ret)); then
local index; ble/keymap:vi/get-index-of-relative-line "$_ble_edit_ind" "$ret"
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
_ble_edit_ind=$index
fi
elif [[ :$opts: == *:goto_bol:* ]]; then # 行指向 yis
_ble_edit_ind=$beg
else # + - gg G L H
if ((beg==bolq||ind<beg)) || [[ ${_ble_edit_str:beg:ind-beg} == *$'\n'* ]] ; then
# 先頭行の非空白行頭に移動する
if ((bolq<=bolp)) && [[ $nolq ]]; then
local nolb=$nolq
else
ble-edit/content/find-non-space "$beg"; local nolb=$ret
fi
ble-edit/content/nonbol-eolp "$nolb" && ((nolb--))
((ind<beg||nolb<ind)) && _ble_edit_ind=$nolb
fi
fi
fi
ble/keymap:vi/adjust-command-mode
return 0
}
function ble/widget/vi-command/linewise-goto.impl {
ble/widget/vi-command/linewise-range.impl "$_ble_edit_ind" "$@"
}
#------------------------------------------------------------------------------
# single char arguments
function ble/keymap:vi/async-read-char.hook {
local IFS=$_ble_term_IFS
local command="${*:1:$#-1}" key="${*:$#}"
if ((key==(_ble_decode_Ctrl|0x6B))); then # C-k
ble/decode/keymap/push vi_digraph
_ble_keymap_vi_digraph__hook="$command"
else
builtin eval -- "$command $key"
fi
}
function ble/keymap:vi/async-read-char {
local IFS=$_ble_term_IFS
_ble_decode_key__hook="ble/keymap:vi/async-read-char.hook $*"
return 147
}
#------------------------------------------------------------------------------
# marks
## @arr _ble_keymap_vi_mark_local
## 添字は mark の文字コードで指定する。
## 各要素は point:bytes の形をしている。
## @arr _ble_keymap_vi_mark_global
## 添字は mark の文字コードで指定する。
## 各要素は hindex:point:bytes の形をしている。
_ble_keymap_vi_mark_Offset=32
_ble_keymap_vi_mark_hindex=
_ble_keymap_vi_mark_local=()
_ble_keymap_vi_mark_global=()
_ble_keymap_vi_mark_history=()
_ble_keymap_vi_mark_edit_dbeg=-1
_ble_keymap_vi_mark_edit_dend=-1
_ble_keymap_vi_mark_edit_dend0=-1
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_mark_hindex \
_ble_keymap_vi_mark_local \
_ble_keymap_vi_mark_global \
_ble_keymap_vi_mark_history \
_ble_keymap_vi_mark_edit_dbeg \
_ble_keymap_vi_mark_edit_dend \
_ble_keymap_vi_mark_edit_dend0
# mark 番号と用途の対応
#
#
# 1 内部使用。矩形挿入モードの開始点を記録するためのもの
# 91 93 `[ と `]。編集・ヤンク範囲を保持する。
# 96 39 `` と `'。最後のジャンプ位置を保持する。39 は実際には使用されない。
# 60 62 `< と `>。最後のビジュアル範囲。
#
ble/array#push _ble_edit_dirty_observer ble/keymap:vi/mark/shift-by-dirty-range
blehook history_leave!=ble/keymap:vi/mark/history-onleave.hook
## @fn ble/keymap:vi/mark/history-onleave.hook
function ble/keymap:vi/mark/history-onleave.hook {
if [[ $_ble_decode_keymap == vi_[inoxs]map ]]; then
ble/keymap:vi/mark/set-local-mark 34 "$_ble_edit_ind" # `"
fi
}
# 履歴がロードされていない時は取り敢えず _ble_history_index=0 で登録をしておく。
# 履歴がロードされた後の初めての利用のときに正しい履歴番号に修正する。
function ble/keymap:vi/mark/update-mark-history {
local h; ble/history/get-index -v h
if [[ ! $_ble_keymap_vi_mark_hindex ]]; then
_ble_keymap_vi_mark_hindex=$h
elif ((_ble_keymap_vi_mark_hindex!=h)); then
local imark value
# save
local -a save=()
for imark in "${!_ble_keymap_vi_mark_local[@]}"; do
local value=${_ble_keymap_vi_mark_local[imark]}
ble/array#push save "$imark:$value"
done
local IFS=$_ble_term_IFS
_ble_keymap_vi_mark_history[_ble_keymap_vi_mark_hindex]="${save[*]-}"
# load
_ble_keymap_vi_mark_local=()
local entry
for entry in ${_ble_keymap_vi_mark_history[h]-}; do
imark=${entry%%:*} value=${entry#*:}
_ble_keymap_vi_mark_local[imark]=$value
done
_ble_keymap_vi_mark_hindex=$h
fi
}
blehook history_change!=ble/keymap:vi/mark/history-change.hook
## @fn ble/keymap:vi/mark/history-change.hook 'delete' index...
## @fn ble/keymap:vi/mark/history-change.hook 'clear'
## @fn ble/keymap:vi/mark/history-change.hook 'insert' beg len
## @param[in] index...
## 削除する項目の番号を指定します。昇順に並んでいる事と重複がない事を仮定します。
## @param[in] beg len
## 挿入位置と挿入項目の個数を指定します。
function ble/keymap:vi/mark/history-change.hook {
local kind=$1; shift
case $kind in
(delete)
# update _ble_keymap_vi_mark_global
local imark
for imark in "${!_ble_keymap_vi_mark_global[@]}"; do
local value=${_ble_keymap_vi_mark_global[imark]}
local h=${value%%:*} v=${value#*:}
local idel shift=0
for idel; do
if [[ $idel == *-* ]]; then
local b=${idel%-*} e=${idel#*-}
((b<=h&&h<e)) && shift= # delete
((h<e)) && break
((shift+=e-b))
else
((idel==h)) && shift= # delete
((idel>=h)) && break
((shift++))
fi
done
[[ $shift ]] &&
_ble_keymap_vi_mark_global[imark]=$((h-shift)):$v
done
# update _ble_keymap_vi_mark_history
ble/builtin/history/array#delete-hindex _ble_keymap_vi_mark_history "$@"
# reset _ble_keymap_vi_mark_hindex
_ble_keymap_vi_mark_hindex= ;;
(clear)
_ble_keymap_vi_mark_global=()
_ble_keymap_vi_mark_history=()
_ble_keymap_vi_mark_hindex= ;;
(insert)
local beg=$1 len=$2
# update _ble_keymap_vi_mark_global
local imark
for imark in "${!_ble_keymap_vi_mark_global[@]}"; do
local value=${_ble_keymap_vi_mark_global[imark]}
local h=${value%%:*} v=${value#*:}
((h>=beg)) && _ble_keymap_vi_mark_global[imark]=$((h+len)):$v
done
# update _ble_keymap_vi_mark_history
ble/builtin/history/array#insert-range _ble_keymap_vi_mark_history "$@"
# reset _ble_keymap_vi_mark_hindex
_ble_keymap_vi_mark_hindex= ;;
esac
}
function ble/keymap:vi/mark/shift-by-dirty-range {
local beg=$1 end=$2 end0=$3 reason=$4
if [[ $4 == edit ]]; then
ble/dirty-range#update --prefix=_ble_keymap_vi_mark_edit_d "${@:1:3}"
ble/keymap:vi/xmap/update-dirty-range "$@"
ble/keymap:vi/mark/update-mark-history
local shift=$((end-end0))
local imark
for imark in "${!_ble_keymap_vi_mark_local[@]}"; do
local value=${_ble_keymap_vi_mark_local[imark]}
local index=${value%%:*} rest=${value#*:}
((index<beg)) || _ble_keymap_vi_mark_local[imark]=$((index<end0?beg:index+shift)):$rest
done
local h; ble/history/get-index -v h
for imark in "${!_ble_keymap_vi_mark_global[@]}"; do
local value=${_ble_keymap_vi_mark_global[imark]}
[[ $value == "$h":* ]] || continue
local h=${value%%:*}; value=${value:${#h}+1}
local index=${value%%:*}; value=${value:${#index}+1}
((index<beg)) || _ble_keymap_vi_mark_global[imark]=$h:$((index<end0?beg:index+shift)):$value
done
ble/keymap:vi/mark/set-local-mark 46 "$beg" # `.
else
ble/dirty-range#clear --prefix=_ble_keymap_vi_mark_edit_d
if [[ $4 == newline && $_ble_decode_keymap != vi_cmap ]]; then
ble/keymap:vi/mark/set-local-mark 96 0 # ``
fi
fi
}
function ble/keymap:vi/mark/set-global-mark {
local c=$1 index=$2 ret
ble/keymap:vi/mark/update-mark-history
ble-edit/content/find-logical-bol "$index"; local bol=$ret
local h; ble/history/get-index -v h
_ble_keymap_vi_mark_global[c]=$h:$bol:$((index-bol))
}
function ble/keymap:vi/mark/set-local-mark {
local c=$1 index=$2 ret
ble/keymap:vi/mark/update-mark-history
ble-edit/content/find-logical-bol "$index"; local bol=$ret
_ble_keymap_vi_mark_local[c]=$bol:$((index-bol))
}
## @fn ble/keymap:vi/mark/get-mark.impl index bytes
## @param[in] index bytes
## 記録された行頭の位置と列を指定します。
## @var[out] ret
## マークが見つかったとき対応する位置を返します。
function ble/keymap:vi/mark/get-mark.impl {
local index=$1 bytes=$2
local len=${#_ble_edit_str}
((index>len&&(index=len)))
ble-edit/content/find-logical-bol "$index"; index=$ret
ble-edit/content/find-logical-eol "$index"; local eol=$ret
((index+=bytes,index>eol&&(index=eol))) # ToDo: calculate by byte offset
ret=$index
return 0
}
## @fn ble/keymap:vi/mark/get-local-mark
## @param[in] c
## mark の番号 (文字コード) を指定します。
## @var[out] ret
## マークが見つかったとき対応する位置を返します。
function ble/keymap:vi/mark/get-local-mark {
local c=$1
ble/keymap:vi/mark/update-mark-history
local value=${_ble_keymap_vi_mark_local[c]}
[[ $value ]] || return 1
local data
ble/string#split data : "$value"
ble/keymap:vi/mark/get-mark.impl "${data[0]}" "${data[1]}" # -> ret
}
# `[ `]
_ble_keymap_vi_mark_suppress_edit=
## @fn ble/keymap:vi/mark/set-previous-edit-area beg end
## @param[in] beg
## @param[in] end
function ble/keymap:vi/mark/set-previous-edit-area {
[[ $_ble_keymap_vi_mark_suppress_edit ]] && return 0
local beg=$1 end=$2
((beg<end)) && ! ble-edit/content/bolp "$end" && ((end--))
ble/keymap:vi/mark/set-local-mark 91 "$beg" # `[
ble/keymap:vi/mark/set-local-mark 93 "$end" # `]
ble/keymap:vi/undo/add
}
function ble/keymap:vi/mark/start-edit-area {
[[ $_ble_keymap_vi_mark_suppress_edit ]] && return 0
ble/dirty-range#clear --prefix=_ble_keymap_vi_mark_edit_d
}
function ble/keymap:vi/mark/commit-edit-area {
local beg=$1 end=$2
ble/dirty-range#update --prefix=_ble_keymap_vi_mark_edit_d "$beg" "$end" "$end"
}
function ble/keymap:vi/mark/end-edit-area {
[[ $_ble_keymap_vi_mark_suppress_edit ]] && return 0
local beg=$_ble_keymap_vi_mark_edit_dbeg
local end=$_ble_keymap_vi_mark_edit_dend
((beg>=0)) && ble/keymap:vi/mark/set-previous-edit-area "$beg" "$end"
}
# ``
function ble/keymap:vi/mark/set-jump {
# ToDo: jumplist?
ble/keymap:vi/mark/set-local-mark 96 "$_ble_edit_ind"
}
function ble/widget/vi-command/set-mark {
_ble_decode_key__hook="ble/widget/vi-command/set-mark.hook"
return 147
}
function ble/widget/vi-command/set-mark.hook {
local key=$1
ble/keymap:vi/clear-arg
local ret
if ble/keymap:vi/k2c "$key" && local c=$ret; then
if ((65<=c&&c<91)); then # A-Z
ble/keymap:vi/mark/set-global-mark "$c" "$_ble_edit_ind"
ble/keymap:vi/adjust-command-mode
return 0
elif ((97<=c&&c<123||c==91||c==93||c==60||c==62||c==96||c==39)); then # a-z [ ] < > ` '
((c==39)) && c=96 # m' は m` に読み替える
ble/keymap:vi/mark/set-local-mark "$c" "$_ble_edit_ind"
ble/keymap:vi/adjust-command-mode
return 0
fi
fi
ble/widget/vi-command/bell
return 1
}
function ble/widget/vi-command/goto-mark.impl {
local index=$1 flag=$2 reg=$3 opts=$4
[[ $flag ]] || ble/keymap:vi/mark/set-jump # ``
if [[ :$opts: == *:line:* ]]; then
ble/widget/vi-command/linewise-goto.impl "$index" "$flag" "$reg"
else
ble/widget/vi-command/exclusive-goto.impl "$index" "$flag" "$reg" nobell
fi
}
function ble/widget/vi-command/goto-local-mark.impl {
local c=$1 opts=$2 ret
local ARG FLAG REG; ble/keymap:vi/get-arg 1
if ble/keymap:vi/mark/get-local-mark "$c" && local index=$ret; then
ble/widget/vi-command/goto-mark.impl "$index" "$FLAG" "$REG" "$opts"
else
ble/widget/vi-command/bell
return 1
fi
}
function ble/widget/vi-command/goto-global-mark.impl {
local c=$1 opts=$2
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/keymap:vi/mark/update-mark-history
local value=${_ble_keymap_vi_mark_global[c]}
if [[ ! $value ]]; then
ble/widget/vi-command/bell
return 1
fi
local data
ble/string#split data : "$value"
# find a history entry by data[0]
local index; ble/history/get-index
if ((index!=data[0])); then
if [[ $FLAG ]]; then
ble/widget/vi-command/bell
return 1
fi
ble-edit/history/goto "${data[0]}" point=near
fi
# find position by data[1]:data[2]
local ret
ble/keymap:vi/mark/get-mark.impl "${data[1]}" "${data[2]}"
ble/widget/vi-command/goto-mark.impl "$ret" "$FLAG" "$REG" "$opts"
}
function ble/widget/vi-command/goto-mark {
_ble_decode_key__hook="ble/widget/vi-command/goto-mark.hook ${1:-char}"
return 147
}
function ble/widget/vi-command/goto-mark.hook {
local opts=$1 key=$2
local ret
if ble/keymap:vi/k2c "$key" && local c=$ret; then
if ((65<=c&&c<91)); then # A-Z
ble/widget/vi-command/goto-global-mark.impl "$c" "$opts"
return "$?"
elif ((_ble_keymap_vi_mark_Offset<=c)); then
((c==39)) && c=96 # `' は `` に読み替える
ble/widget/vi-command/goto-local-mark.impl "$c" "$opts"
return "$?"
fi
fi
ble/keymap:vi/clear-arg
ble/widget/vi-command/bell
return 1
}
function ble/widget/vi-command:marks {
ble/keymap:vi/mark/update-mark-history
local -a entries=() # <char>:<hindex>:<bol>:<col>
local c
for c in "${!_ble_keymap_vi_mark_local[@]}"; do
local value=${_ble_keymap_vi_mark_local[c]}
[[ $value ]] && ble/array#push entries "$c::$value"
done
for c in "${!_ble_keymap_vi_mark_global[@]}"; do
local value=${_ble_keymap_vi_mark_global[c]}
[[ $value ]] && ble/array#push entries "$c:$value"
done
local -a fields=()
local data ret
for data in "${entries[@]}"; do
ble/string#split data : "$data"
ble/util/c2s-edit "${data[0]}" sgr1=$'\e[9807m':sgr0=$'\e[9807m'; local s=$ret
# determine the line number and history position
local line hlabel=
if [[ ${data[1]} ]]; then
local entry
ble/history/get-edited-entry "$index"
line=$entry
hlabel=' !'${data[1]}
else
line=$_ble_edit_str
fi
ble/string#count-char "${line::data[2]}" $'\n'
((line=1+ret))
ble/keymap:vi/mark/get-mark.impl "$line" "${data[3]}"; local ind=$ret
ble/array#push fields "${data[0]}" "$s" "$line" "${data[3]}" "$ind" "$hlabel"
done
if ((${#fields[@]})); then
local content
ble/util/sprintf content '%7d %s\t(%3d, %3d) %5d%s\n' "${fields[@]}"
content=${content%$'\n'}
local mydbg=1
ble/edit/info/show ansi "$content"
fi
return 0
}
#------------------------------------------------------------------------------
# repeat (nmap .)
## @arr _ble_keymap_vi_repeat
## @arr _ble_keymap_vi_repeat_insert
##
## _ble_keymap_vi_repeat が前回の操作を記録する
## _ble_keymap_vi_repeat_insert は挿入モードにいる時に、
## その挿入モードに突入するきっかけとなった操作を保持する。
## これは <C-[> または <C-c> で挿入モードを完了する際に、
## _ble_keymap_vi_repeat に書き込まれるものである。
##
## ${_ble_keymap_vi_repeat[0]} = KEYMAP
## 呼び出し時の kmap を保持します。
## ${_ble_keymap_vi_repeat[1]} = KEYS
## 呼び出しに用いられたキーの列を保持します。
## ${_ble_keymap_vi_repeat[2]} = WIDGET
## 呼び出された編集コマンドを保持します。
## ${_ble_keymap_vi_repeat[@]:3:3} = ARG FLAG REG
## 呼び出し時の修飾状態を保持します。
## ${_ble_keymap_vi_repeat[6]} = _ble_keymap_vi_xmap_prev_edit
## vi_xmap のとき範囲の大きさと種類を記録します。
## ${_ble_keymap_vi_repeat[@]:10}
## 各 WIDGET が自由に使える領域
##
## @arr _ble_keymap_vi_repeat_irepeat
##
## _ble_keymap_vi_repeat の操作によって挿入モードに入るとき、
## そこで行われる挿入操作の列を記録する配列である。
## 形式は _ble_keymap_vi_irepeat と同じ。
##
## @var _ble_keymap_vi_repeat_invoke
## ble/keymap:vi/repeat/invoke を通して呼び出された widget かどうかを保持するローカル変数です。
## この変数が非空白のとき ble/keymap:vi/repeat/invoke 内部での呼び出しであることを表します。
##
_ble_keymap_vi_repeat=()
_ble_keymap_vi_repeat_insert=()
_ble_keymap_vi_repeat_irepeat=()
_ble_keymap_vi_repeat_invoke=
function ble/keymap:vi/repeat/record-special {
[[ $_ble_keymap_vi_mark_suppress_edit ]] && return 0
if [[ $_ble_keymap_vi_repeat_invoke ]]; then
# repeat に引数が指定されたときは以降それを使う
[[ $repeat_arg ]] && _ble_keymap_vi_repeat[3]=$repeat_arg
# レジスタが記録されていないときは、以降新しく指定されたレジスタを使う
[[ ! ${_ble_keymap_vi_repeat[5]} ]] && _ble_keymap_vi_repeat[5]=$repeat_reg
return 0
fi
return 1
}
function ble/keymap:vi/repeat/record-normal {
local IFS=$_ble_term_IFS
local -a repeat; repeat=("$KEYMAP" "${KEYS[*]-}" "$WIDGET" "$ARG" "$FLAG" "$REG" '')
if [[ $KEYMAP == vi_[xs]map ]]; then
repeat[6]=$_ble_keymap_vi_xmap_prev_edit
fi
if [[ $_ble_decode_keymap == vi_imap ]]; then
_ble_keymap_vi_repeat_insert=("${repeat[@]}")
else
_ble_keymap_vi_repeat=("${repeat[@]}")
_ble_keymap_vi_repeat_irepeat=()
fi
}
function ble/keymap:vi/repeat/record {
ble/keymap:vi/repeat/record-special && return 0
ble/keymap:vi/repeat/record-normal
}
## @fn ble/keymap:vi/repeat/record-insert
## 挿入モードを抜ける時に、挿入モードに入るきっかけになった操作と、
## 挿入モードで行われた挿入操作の列を記録します。
function ble/keymap:vi/repeat/record-insert {
ble/keymap:vi/repeat/record-special && return 0
if [[ ${_ble_keymap_vi_repeat_insert-} ]]; then
# 挿入モード突入操作が未だ有効ならば、挿入操作の有無に拘らず記録
_ble_keymap_vi_repeat=("${_ble_keymap_vi_repeat_insert[@]}")
_ble_keymap_vi_repeat_irepeat=("${_ble_keymap_vi_irepeat[@]}")
elif ((${#_ble_keymap_vi_irepeat[@]})); then
# 挿入モード突入操作が初期化されていたら、挿入操作がある時のみに記録
local IFS=$_ble_term_IFS
_ble_keymap_vi_repeat=(vi_nmap "${KEYS[*]-}" ble/widget/vi_nmap/insert-mode 1 '' '')
_ble_keymap_vi_repeat_irepeat=("${_ble_keymap_vi_irepeat[@]}")
fi
ble/keymap:vi/repeat/clear-insert
}
## @fn ble/keymap:vi/repeat/clear-insert
## 挿入モードにおいて white list にないコマンドが実行された時に、
## 挿入モードに入るきっかけになった操作を初期化します。
function ble/keymap:vi/repeat/clear-insert {
_ble_keymap_vi_repeat_insert=()
}
function ble/keymap:vi/repeat/invoke {
local repeat_arg=$_ble_edit_arg
local repeat_reg=$_ble_keymap_vi_reg
local KEYMAP=${_ble_keymap_vi_repeat[0]}
local -a KEYS; ble/string#split-words KEYS "${_ble_keymap_vi_repeat[1]}"
local WIDGET=${_ble_keymap_vi_repeat[2]}
# keymap の状態復元
if [[ $KEYMAP != vi_[onxs]map ]]; then
ble/widget/vi-command/bell
return 1
elif [[ $KEYMAP == vi_omap ]]; then
ble/decode/keymap/push vi_omap
elif [[ $KEYMAP == vi_[xs]map ]]; then
local _ble_keymap_vi_xmap_prev_edit=${_ble_keymap_vi_repeat[6]}
ble/widget/vi_xmap/.restore-visual-state
ble/decode/keymap/push "$KEYMAP"
# Note: vim では . によって領域の大きさは更新されない。
# 従ってここでは敢えて _ble_keymap_vi_xmap_prev_edit を unset しない
fi
# ※本体の _ble_keymap_vi_repeat は成功した時にのみ repeat/record で書き換える
_ble_edit_arg=
_ble_keymap_vi_oparg=${_ble_keymap_vi_repeat[3]}
_ble_keymap_vi_opfunc=${_ble_keymap_vi_repeat[4]}
[[ $repeat_arg ]] && _ble_keymap_vi_oparg=$repeat_arg
# vim ではレジスタは記録されたものが優先されるようだ
local REG=${_ble_keymap_vi_repeat[5]}
[[ $REG ]] && _ble_keymap_vi_reg=$REG
local _ble_keymap_vi_single_command{,_overwrite}= # single-command-mode は持続させる。
local _ble_keymap_vi_repeat_invoke=1
local LASTWIDGET=$_ble_decode_widget_last
_ble_decode_widget_last=$WIDGET
builtin eval -- "$WIDGET"
if [[ $_ble_decode_keymap == vi_imap ]]; then
((_ble_keymap_vi_irepeat_count<=1?(_ble_keymap_vi_irepeat_count=2):_ble_keymap_vi_irepeat_count++))
local -a _ble_keymap_vi_irepeat
_ble_keymap_vi_irepeat=("${_ble_keymap_vi_repeat_irepeat[@]}")
ble/array#push _ble_keymap_vi_irepeat '0:ble/widget/dummy' # Note: normal-mode が自分自身を pop しようとするので。
ble/widget/vi_imap/normal-mode
fi
ble/util/unlocal _ble_keymap_vi_single_command{,_overwrite}
}
# nmap .
function ble/widget/vi_nmap/repeat {
ble/keymap:vi/repeat/invoke
ble/keymap:vi/adjust-command-mode
}
#------------------------------------------------------------------------------
# command: [cdy]?[hl]
## @widget vi-command/forward-char [type]
## @widget vi-command/backward-char [type]
##
## @param[in] type
## type=wrap のとき複数行に亘る移動を許します。
##
function ble/widget/vi-command/forward-char {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local index
if [[ $1 == wrap ]]; then
# SP
if [[ $FLAG || $_ble_decode_keymap == vi_[xs]map ]]; then
((index=_ble_edit_ind+ARG,
index>${#_ble_edit_str}&&(index=${#_ble_edit_str})))
else
local nl=$'\n'
local rex="^([^$nl]$nl?|$nl){0,$ARG}"
[[ ${_ble_edit_str:_ble_edit_ind} =~ $rex ]]
((index=_ble_edit_ind+${#BASH_REMATCH}))
fi
else
local line=${_ble_edit_str:_ble_edit_ind:ARG}
line=${line%%$'\n'*}
((index=_ble_edit_ind+${#line}))
fi
ble/widget/vi-command/exclusive-goto.impl "$index" "$FLAG" "$REG"
}
function ble/widget/vi-command/backward-char {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local index
((ARG>_ble_edit_ind&&(ARG=_ble_edit_ind)))
if [[ $1 == wrap ]]; then
# DEL
if [[ $FLAG || $_ble_decode_keymap == vi_[xs]map ]]; then
((index=_ble_edit_ind-ARG,index<0&&(index=0)))
else
local width=$ARG line
while ((width<=_ble_edit_ind)); do
line=${_ble_edit_str:_ble_edit_ind-width:width}
line=${line//[!$'\n']$'\n'/x}
((${#line}>=ARG)) && break
((width+=ARG-${#line}))
done
((index=_ble_edit_ind-width,index<0&&(index=0)))
fi
else
local line=${_ble_edit_str:_ble_edit_ind-ARG:ARG}
line=${line##*$'\n'}
((index=_ble_edit_ind-${#line}))
fi
ble/widget/vi-command/exclusive-goto.impl "$index" "$FLAG" "$REG"
}
# nmap ~
function ble/widget/vi_nmap/forward-char-toggle-case {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local line=${_ble_edit_str:_ble_edit_ind:ARG}
line=${line%%$'\n'*}
local len=${#line}
if ((len==0)); then
ble/widget/vi-command/bell
return 1
fi
local index=$((_ble_edit_ind+len))
local ret; ble/string#toggle-case "${_ble_edit_str:_ble_edit_ind:len}"
ble/widget/.replace-range "$_ble_edit_ind" "$index" "$ret"
ble/keymap:vi/mark/set-previous-edit-area "$_ble_edit_ind" "$index"
ble/keymap:vi/repeat/record
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
_ble_edit_ind=$index
ble/keymap:vi/adjust-command-mode
return 0
}
#------------------------------------------------------------------------------
# command: [cdy]?[jk]
## @fn ble/widget/vi-command/.history-relative-line offset
##
## @param[in] offset
## 移動する相対行数を指定する。負の値は前に移動することを表し、
## 正の値は後に移動することを表す。
##
## @exit
## 全く移動しなかった場合は 1 を返します。
## それ以外の場合は 0 を返します。
##
function ble/widget/vi-command/.history-relative-line {
local offset=$1
((offset)) || return 0
# 履歴が初期化されていないとき最終行にいる。
if [[ ! $_ble_history_prefix && ! $_ble_history_load_done ]]; then
((offset<0)) || return 1
ble/history/initialize # to use ble/history/get-index
fi
local index histsize
ble/history/get-index
ble/history/get-count -v histsize
local index0=$index
local ret count=$((offset<0?-offset:offset))
((count--))
while ((count>=0)); do
if ((offset<0)); then
((--index))
((index>=0)) || break
else
((++index))
((index<=histsize)) || break
fi
local entry; ble/history/get-edited-entry "$index"
ble/string#count-char "$entry" $'\n'; local nline=$((ret+1))
((count<nline)) && break
((count-=nline))
done
((index!=index0)) || return 1
if ((index<0)); then
# reached the beginning of history
ble-edit/history/goto 0 point=none
_ble_edit_ind=0
elif ((index>histsize)); then
# reached the end of history
ble-edit/history/goto "$histsize" point=none
_ble_edit_ind=${#_ble_edit_str}
else
ble-edit/history/goto "$index" linewise:default-point=near
fi
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
return 0
}
## @fn ble/keymap:vi/get-index-of-relative-line p offset
## 列を保持した行移動の先の位置を計算します。
## @param[in,opt] p
## 基準となる位置を指定します。空文字列を指定した時は現在位置が使われます。
## @param[in] offset
## 移動する行数を指定します。
## @param[out] index
function ble/keymap:vi/get-index-of-relative-line {
local ind=${1:-$_ble_edit_ind} offset=$2
if ((offset==0)); then
index=$ind
return 0
fi
local count=$((offset<0?-offset:offset))
local ret
ble-edit/content/find-logical-bol "$ind" 0; local bol1=$ret
ble-edit/content/find-logical-bol "$ind" "$offset"; local bol2=$ret
if ble/edit/use-textmap; then
# 列の表示相対位置 (x,y) を保持
local b1x b1y; ble/textmap#getxy.cur --prefix=b1 "$bol1"
local b2x b2y; ble/textmap#getxy.cur --prefix=b2 "$bol2"
ble-edit/content/find-logical-eol "$bol2"; local eol2=$ret
local c1x c1y; ble/textmap#getxy.cur --prefix=c1 "$ind"
local e2x e2y; ble/textmap#getxy.cur --prefix=e2 "$eol2"
local x=$c1x y=$((b2y+c1y-b1y))
((y>e2y&&(x=e2x,y=e2y)))
ble/textmap#get-index-at "$x" "$y" # local variable "index" is set here
else
# 論理列を保持
ble-edit/content/find-logical-eol "$bol2"; local eol2=$ret
((index=bol2+ind-bol1,index>eol2&&(index=eol2)))
fi
}
## @fn ble/widget/vi-command/relative-line.impl offset flag reg opts
## @widget vi-command/forward-line # nmap j
## @widget vi-command/backward-line # nmap k
##
## j, k による移動の動作について。論理行を移動するとする。
## 配置情報があるとき、列は行頭からの相対表示位置 (dx,dy) を保持する。
## 配置情報がないとき、論理列を保持する。
##
## より前の履歴項目に移った時は列は行末に移る。
## より後の履歴項目に移った時は列は先頭に移る。
##
## ToDo: 移動開始時の相対表示位置の記録は現在行っていない。
##
## @param[in] offset flag
##
## @param[in] opts
## 以下の値をコロンで区切って繋げた物を指定する。
##
## history
## 現在の履歴項目内で要求された行数だけ移動できないとき、
## 履歴項目内の論理行を移動する。
## 但し flag がある場合は履歴項目の移動は行わない。
##
function ble/widget/vi-command/relative-line.impl {
local offset=$1 flag=$2 reg=$3 opts=$4
((offset==0)) && return 0
if [[ $flag ]]; then
ble/widget/vi-command/linewise-goto.impl "$_ble_edit_ind:$offset" "$flag" "$reg" preserve_column:require_multiline
return "$?"
fi
# 現在履歴項目内で移動できる行数の判定
local count=$((offset<0?-offset:offset)) ret
if ((offset<0)); then
ble/string#count-char "${_ble_edit_str::_ble_edit_ind}" $'\n'
else
ble/string#count-char "${_ble_edit_str:_ble_edit_ind}" $'\n'
fi
local nmove=$((count<ret?count:ret))
((count-=nmove))
# 現在の履歴項目内での探索
if ((count==0)); then
local index; ble/keymap:vi/get-index-of-relative-line "$_ble_edit_ind" "$offset"
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
_ble_edit_ind=$index
ble/keymap:vi/adjust-command-mode
return 0
fi
# 履歴項目を行数を数えつつ移動
if [[ $_ble_decode_keymap == vi_nmap && :$opts: == *:history:* ]]; then
if ble/widget/vi-command/.history-relative-line "$((offset>=0?count:-count))" || ((nmove)); then
ble/keymap:vi/adjust-command-mode
return 0
fi
fi
ble/widget/vi-command/bell
return 1
}
function ble/widget/vi-command/forward-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/relative-line.impl "$ARG" "$FLAG" "$REG" history
}
function ble/widget/vi-command/backward-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/relative-line.impl "$((-ARG))" "$FLAG" "$REG" history
}
## @fn ble/widget/vi-command/graphical-relative-line.impl offset flag reg opts
## @widget vi-command/graphical-forward-line # nmap gj
## @widget vi-command/graphical-backward-line # nmap gk
##
## @param[in] offset
## 移動する相対行数。負の値は上の行へ行くことを表す。正の値は下の行へ行くことを表す。
## @param[in] flag
## オペレータを指定する。
## @param[in] opts
## 以下のオプションをコロンで繋げたものを指定する。
##
## history
##
function ble/widget/vi-command/graphical-relative-line.impl {
local offset=$1 flag=$2 reg=$3 opts=$4
local index move
if ble/edit/use-textmap; then
local x y ax ay
ble/textmap#getxy.cur "$_ble_edit_ind"
((ax=x,ay=y+offset,
ay<_ble_textmap_begy?(ay=_ble_textmap_begy):
(ay>_ble_textmap_endy?(ay=_ble_textmap_endy):0)))
ble/textmap#get-index-at "$ax" "$ay"
ble/textmap#getxy.cur --prefix=a "$index"
((offset-=move=ay-y))
else
local ret ind=$_ble_edit_ind
ble-edit/content/find-logical-bol "$ind" 0; local bol1=$ret
ble-edit/content/find-logical-bol "$ind" "$offset"; local bol2=$ret
ble-edit/content/find-logical-eol "$bol2"; local eol2=$ret
((index=bol2+ind-bol1,index>eol2&&(index=eol2)))
if ((index>ind)); then
ble/string#count-char "${_ble_edit_str:ind:index-ind}" $'\n'
((offset+=move=-ret))
elif ((index<ind)); then
ble/string#count-char "${_ble_edit_str:index:ind-index}" $'\n'
((offset+=move=ret))
fi
fi
if ((offset==0)); then
ble/widget/vi-command/exclusive-goto.impl "$index" "$flag" "$reg"
return "$?"
fi
if [[ ! $flag && $_ble_decode_keymap == vi_nmap && :$opts: == *:history:* ]]; then
if ble/widget/vi-command/.history-relative-line "$offset"; then
ble/keymap:vi/adjust-command-mode
return 0
fi
fi
# 失敗: オペレータは実行されないが移動はする。
((move)) && ble/widget/vi-command/exclusive-goto.impl "$index"
ble/widget/vi-command/bell
return 1
}
function ble/widget/vi-command/graphical-forward-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/graphical-relative-line.impl "$ARG" "$FLAG" "$REG"
}
function ble/widget/vi-command/graphical-backward-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/graphical-relative-line.impl "$((-ARG))" "$FLAG" "$REG"
}
#------------------------------------------------------------------------------
# command: ^ + - _ $
function ble/widget/vi-command/relative-first-non-space.impl {
local arg=$1 flag=$2 reg=$3 opts=$4
local ret ind=$_ble_edit_ind
ble-edit/content/find-logical-bol "$ind" "$arg"; local bolx=$ret
ble-edit/content/find-non-space "$bolx"; local nolx=$ret
# 2017-09-12 何故か分からないが vim はこういう振る舞いに見える。
((_ble_keymap_vi_single_command==2&&_ble_keymap_vi_single_command--))
if [[ $flag ]]; then
if [[ :$opts: == *:charwise:* ]]; then
# command: ^
ble-edit/content/nonbol-eolp "$nolx" && ((nolx--))
ble/widget/vi-command/exclusive-goto.impl "$nolx" "$flag" "$reg" nobell
elif [[ :$opts: == *:multiline:* ]]; then
# command: + -
ble/widget/vi-command/linewise-goto.impl "$nolx" "$flag" "$reg" require_multiline:bolx="$bolx":nolx="$nolx"
else
# command: _
ble/widget/vi-command/linewise-goto.impl "$nolx" "$flag" "$reg" bolx="$bolx":nolx="$nolx"
fi
return "$?"
fi
local count=$((arg<0?-arg:arg)) nmove=0
if ((count)); then
local beg end; ((nolx<ind?(beg=nolx,end=ind):(beg=ind,end=nolx)))
ble/string#count-char "${_ble_edit_str:beg:end-beg}" $'\n'; nmove=$ret
((count-=nmove))
fi
if ((count==0)); then
ble/keymap:vi/needs-eol-fix "$nolx" && ((nolx--))
_ble_edit_ind=$nolx
ble/keymap:vi/adjust-command-mode
return 0
fi
# 履歴項目の移動
if [[ $_ble_decode_keymap == vi_nmap && :$opts: == *:history:* ]] && ble/widget/vi-command/.history-relative-line "$((arg>=0?count:-count))"; then
ble/widget/vi-command/first-non-space
elif ((nmove)); then
ble/keymap:vi/needs-eol-fix "$nolx" && ((nolx--))
_ble_edit_ind=$nolx
ble/keymap:vi/adjust-command-mode
else
ble/widget/vi-command/bell
return 1
fi
}
# nmap ^
function ble/widget/vi-command/first-non-space {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/relative-first-non-space.impl 0 "$FLAG" "$REG" charwise:history
}
# nmap +
function ble/widget/vi-command/forward-first-non-space {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/relative-first-non-space.impl "$ARG" "$FLAG" "$REG" multiline:history
}
# nmap -
function ble/widget/vi-command/backward-first-non-space {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/relative-first-non-space.impl "$((-ARG))" "$FLAG" "$REG" multiline:history
}
# nmap _
function ble/widget/vi-command/first-non-space-forward {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/relative-first-non-space.impl "$((ARG-1))" "$FLAG" "$REG" history
}
# nmap $
function ble/widget/vi-command/forward-eol {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
if ((ARG>1)) && [[ ${_ble_edit_str:_ble_edit_ind} != *$'\n'* ]]; then
ble/widget/vi-command/bell
return 1
fi
local ret index
ble-edit/content/find-logical-eol "$_ble_edit_ind" "$((ARG-1))"; index=$ret
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
ble/widget/vi-command/inclusive-goto.impl "$index" "$FLAG" "$REG" nobell
[[ $_ble_decode_keymap == vi_[xs]map ]] &&
ble/keymap:vi/xmap/add-eol-extension # 末尾拡張
}
# nmap g0 g<home>
function ble/widget/vi-command/beginning-of-graphical-line {
if ble/edit/use-textmap; then
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local x y index
ble/textmap#getxy.cur "$_ble_edit_ind"
ble/textmap#get-index-at 0 "$y"
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
ble/widget/vi-command/exclusive-goto.impl "$index" "$FLAG" "$REG" nobell
else
ble/widget/vi-command/beginning-of-line
fi
}
# nmap g^
function ble/widget/vi-command/graphical-first-non-space {
if ble/edit/use-textmap; then
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local x y index ret
ble/textmap#getxy.cur "$_ble_edit_ind"
ble/textmap#get-index-at 0 "$y"
ble-edit/content/find-non-space "$index"
ble/keymap:vi/needs-eol-fix "$ret" && ((ret--))
ble/widget/vi-command/exclusive-goto.impl "$ret" "$FLAG" "$REG" nobell
else
ble/widget/vi-command/first-non-space
fi
}
# nmap g$ g<end>
function ble/widget/vi-command/graphical-forward-eol {
if ble/edit/use-textmap; then
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local x y index
ble/textmap#getxy.cur "$_ble_edit_ind"
ble/textmap#get-index-at "$((_ble_textmap_cols-1))" "$((y+ARG-1))"
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
ble/widget/vi-command/inclusive-goto.impl "$index" "$FLAG" "$REG" nobell
else
ble/widget/vi-command/forward-eol
fi
}
# nmap gm
function ble/widget/vi-command/middle-of-graphical-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local index
if ble/edit/use-textmap; then
local x y
ble/textmap#getxy.cur "$_ble_edit_ind"
ble/textmap#get-index-at "$((_ble_textmap_cols/2))" "$y"
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
else
local ret
ble-edit/content/find-logical-bol; local bol=$ret
ble-edit/content/find-logical-eol; local eol=$ret
((index=(bol+${COLUMNS:-eol})/2,
index>eol&&(index=eol),
bol<eol&&index==eol&&(index--)))
fi
ble/widget/vi-command/exclusive-goto.impl "$index" "$FLAG" "$REG" nobell
}
# nmap g_
function ble/widget/vi-command/last-non-space {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret
ble-edit/content/find-logical-eol "$_ble_edit_ind" "$((ARG-1))"; local index=$ret
if ((ARG>1)) && [[ ${_ble_edit_str:_ble_edit_ind:index-_ble_edit_ind} != *$'\n'* ]]; then
# 行移動を起こすはずだったのに一行も進めなかった場合は失敗
ble/widget/vi-command/bell
return 1
fi
local rex=$'([^ \t\n]?[ \t]+|[^ \t\n])$'
[[ ${_ble_edit_str::index} =~ $rex ]] && ((index-=${#BASH_REMATCH}))
ble/widget/vi-command/inclusive-goto.impl "$index" "$FLAG" "$REG" nobell
}
#------------------------------------------------------------------------------
# vi_nmap: scroll
# C-d, C-u, C-e, C-y
# C-b, prior, C-f, next
_ble_keymap_vi_previous_scroll=
## @fn ble/widget/vi_nmap/scroll.impl opts
## @arg[in] opts
## forward
## backward
function ble/widget/vi_nmap/scroll.impl {
local opts=$1
local height=${_ble_canvas_panel_height[_ble_textarea_panel]}
# adjust arguments
local ARG FLAG REG; ble/keymap:vi/get-arg "$_ble_keymap_vi_previous_scroll"
_ble_keymap_vi_previous_scroll=$ARG
[[ $ARG ]] || ((ARG=height/2))
[[ :$opts: == *:backward:* ]] && ((ARG=-ARG))
ble/widget/.update-textmap
if [[ :$opts: == *:cursor:* ]]; then
# move
local x y index ret
ble/textmap#getxy.cur "$_ble_edit_ind"
ble/textmap#get-index-at 0 "$((y+ARG))"
ble-edit/content/find-non-space "$index"
ble/keymap:vi/needs-eol-fix "$ret" && ((ret--))
_ble_edit_ind=$ret
ble/keymap:vi/adjust-command-mode
((_ble_textmap_endy<height)) && return 0
local ax ay
ble/textmap#getxy.cur --prefix=a "$_ble_edit_ind"
local max_scroll=$((_ble_textmap_endy+1-height))
((_ble_textarea_scroll_new+=ay-y))
if ((_ble_textarea_scroll_new<0)); then
_ble_textarea_scroll_new=0
elif ((_ble_textarea_scroll_new>max_scroll)); then
_ble_textarea_scroll_new=$max_scroll
fi
else
((_ble_textmap_endy<height)) && return 0
local max_scroll=$((_ble_textmap_endy+1-height))
((_ble_textarea_scroll_new+=ARG))
if ((_ble_textarea_scroll_new<0)); then
_ble_textarea_scroll_new=0
elif ((_ble_textarea_scroll_new>max_scroll)); then
_ble_textarea_scroll_new=$max_scroll
fi
# ax ay 表示範囲
local ay=$((_ble_textarea_scroll_new+_ble_textmap_begy))
local by=$((_ble_textarea_scroll_new+height-1))
((_ble_textarea_scroll_new&&ay++))
# カーソル範囲
((_ble_textarea_scroll_new!=0&&ay<by&&ay++,
_ble_textarea_scroll_new!=max_scroll&&ay<by&&by--))
local x y
ble/textmap#getxy.cur "$_ble_edit_ind"
if ((y<ay?(y=ay,1):(y>by?(y=by,1):0))); then
local index
ble/textmap#get-index-at "$x" "$y"
_ble_edit_ind=$index
fi
ble/keymap:vi/adjust-command-mode
fi
}
# nmap C-d
function ble/widget/vi_nmap/forward-line-scroll {
ble/widget/vi_nmap/scroll.impl forward:cursor
}
# nmap C-u
function ble/widget/vi_nmap/backward-line-scroll {
ble/widget/vi_nmap/scroll.impl backward:cursor
}
# nmap C-e
function ble/widget/vi_nmap/forward-scroll {
ble/widget/vi_nmap/scroll.impl forward
}
# nmap C-y
function ble/widget/vi_nmap/backward-scroll {
ble/widget/vi_nmap/scroll.impl backward
}
# nmap C-f, next
function ble/widget/vi_nmap/pagedown {
local height=${_ble_canvas_panel_height[_ble_textarea_panel]}
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/.update-textmap
# 最終行以外にいる事を確認
local x y
ble/textmap#getxy.cur "$_ble_edit_ind"
if ((y==_ble_textmap_endy)); then
ble/widget/vi-command/bell
return 1
fi
# 行き先を決定
local vheight=$((height-_ble_textmap_begy-1))
local ybase=$((_ble_textarea_scroll_new+height-1))
local y1=$((ybase+(ARG-1)*(vheight-2)))
local index ret
ble/textmap#get-index-at 0 "$y1"
ble-edit/content/bolp "$index" &&
ble-edit/content/find-non-space "$index"; index=$ret
_ble_edit_ind=$index
# スクロール (現在位置が上から2行目になる様に)
local max_scroll=$((_ble_textmap_endy+1-height))
ble/textmap#getxy.cur "$_ble_edit_ind"
local scroll=$((y<=_ble_textmap_begy+1?0:(y-_ble_textmap_begy-1)))
((scroll>max_scroll&&(scroll=max_scroll)))
_ble_textarea_scroll_new=$scroll
ble/keymap:vi/adjust-command-mode
}
# nmap C-b, prior
function ble/widget/vi_nmap/pageup {
local height=${_ble_canvas_panel_height[_ble_textarea_panel]}
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/.update-textmap
# 少なくとも1行目が表示されていない事を確認
if ((!_ble_textarea_scroll_new)); then
ble/widget/vi-command/bell
return 1
fi
# 行き先を決定
local vheight=$((height-_ble_textmap_begy-1))
local ybase=$((_ble_textarea_scroll_new+_ble_textmap_begy+1))
local y1=$((ybase-(ARG-1)*(vheight-2)))
((y1<_ble_textmap_begy&&(y1=_ble_textmap_begy)))
local index ret
ble/textmap#get-index-at 0 "$y1"
ble-edit/content/bolp "$index" &&
ble-edit/content/find-non-space "$index"; index=$ret
_ble_edit_ind=$index
# スクロール (現在位置が下から2行目になる様に)
local x y
ble/textmap#getxy.cur "$_ble_edit_ind"
local scroll=$((y-height+2))
((scroll<0&&(scroll=0)))
_ble_textarea_scroll_new=$scroll
ble/keymap:vi/adjust-command-mode
}
function ble/widget/vi_nmap/scroll-to-center.impl {
local opts=$1
ble/widget/.update-textmap
local height=${_ble_canvas_panel_height[_ble_textarea_panel]}
local ARG FLAG REG; ble/keymap:vi/get-arg ''
if [[ ! $ARG && :$opts: == *:pagedown:* ]]; then
local y1=$((_ble_textarea_scroll_new+height))
local index
ble/textmap#get-index-at 0 "$y1"
((_ble_edit_ind=index))
fi
local ret
ble-edit/content/find-logical-bol "$_ble_edit_ind"; local bol1=$ret
if [[ $ARG || :$opts: == *:nol:* ]]; then
if [[ $ARG ]]; then
ble-edit/content/find-logical-bol 0 "$((ARG-1))"; local bol2=$ret
else
local bol2=$bol1
fi
if [[ :$opts: == *:nol:* ]]; then
# 非空白行頭に移動する
ble-edit/content/find-non-space "$bol2"
_ble_edit_ind=$ret
elif ((bol1!=bol2)); then
# 行内の同じ相対位置に移動する
# dx dy = 行頭からの相対位置
local b1x b1y p1x p1y dx dy
ble/textmap#getxy.cur --prefix=b1 "$bol1"
ble/textmap#getxy.cur --prefix=p1 "$_ble_edit_ind"
((dx=p1x,dy=p1y-b1y))
# index = 行き先の行 bol2 の同じ相対位置のインデックス
local b2x b2y p2x p2y index
ble/textmap#getxy.cur --prefix=b2 "$bol2"
((p2x=b2x,p2y=b2y+dy))
ble/textmap#get-index-at "$p2x" "$p2y"
if ble-edit/content/find-logical-bol "$index"; ((ret==bol2)); then
_ble_edit_ind=$index
else
# 別の行になっている時は行末に移動
ble-edit/content/find-logical-eol "$bol2"
_ble_edit_ind=$ret
fi
fi
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
fi
# スクロール量の計算
if ((_ble_textmap_endy+1>height)); then
local max_scroll=$((_ble_textmap_endy+1-height))
local b1x b1y
ble/textmap#getxy.cur --prefix=b1 "$bol1"
local scroll=
if [[ :$opts: == *:top:* ]]; then
((scroll=b1y-(_ble_textmap_begy+2)))
elif [[ :$opts: == *:bottom:* ]]; then
((scroll=b1y-(height-2)))
else
local vheight=$((height-_ble_textmap_begy-1))
((scroll=b1y-(_ble_textmap_begy+1+vheight/2)))
fi
if ((scroll<0)); then
scroll=0
elif ((scroll>max_scroll)); then
scroll=$max_scroll
fi
_ble_textarea_scroll_new=$scroll
fi
ble/keymap:vi/adjust-command-mode
}
# nmap zz
function ble/widget/vi_nmap/scroll-to-center-and-redraw {
ble/widget/vi_nmap/scroll-to-center.impl
ble/widget/redraw-line
}
# nmap zt
function ble/widget/vi_nmap/scroll-to-top-and-redraw {
ble/widget/vi_nmap/scroll-to-center.impl top
ble/widget/redraw-line
}
# nmap zb
function ble/widget/vi_nmap/scroll-to-bottom-and-redraw {
ble/widget/vi_nmap/scroll-to-center.impl bottom
ble/widget/redraw-line
}
# nmap z.
function ble/widget/vi_nmap/scroll-to-center-non-space-and-redraw {
ble/widget/vi_nmap/scroll-to-center.impl nol
ble/widget/redraw-line
}
# nmap z<C-m>
function ble/widget/vi_nmap/scroll-to-top-non-space-and-redraw {
ble/widget/vi_nmap/scroll-to-center.impl top:nol
ble/widget/redraw-line
}
# nmap z-
function ble/widget/vi_nmap/scroll-to-bottom-non-space-and-redraw {
ble/widget/vi_nmap/scroll-to-center.impl bottom:nol
ble/widget/redraw-line
}
# nmap z+
function ble/widget/vi_nmap/scroll-or-pagedown-and-redraw {
ble/widget/vi_nmap/scroll-to-center.impl top:nol:pagedown
ble/widget/redraw-line
}
#------------------------------------------------------------------------------
# command: p P
## @fn ble/widget/vi_nmap/paste.impl/block arg [type]
##
## @param[in] arg
## 挿入する各行の繰り返し回数を指定します。
##
## @param[in] type
## graphical を指定すると配置情報を用いて挿入します。
## 省略したときは、配置情報があるときにそれを使用します。
## それ以外を指定すると論理列に基いて挿入を行います。
##
## @var[in] _ble_edit_kill_ring
## 改行区切りの文字列リストです。
##
## @var[in] _ble_edit_kill_type == B:*
## B: に続き空白区切りの数字のリストを保持します。
## 数字は _ble_edit_kill_ring に含まれる行の数と同じだけ指定します。
## 数字は行の途中に挿入する際に後ろに追加する空白の数を表します。
##
function ble/widget/vi_nmap/paste.impl/block {
local arg=${1:-1} type=$2
local graphical=
if [[ $type ]]; then
[[ $type == graphical ]] && graphical=1
else
ble/edit/use-textmap && graphical=1
fi
local ret cols=$_ble_textmap_cols
local -a afill; ble/string#split-words afill "${_ble_edit_kill_type:2}"
local atext; ble/string#split-lines atext "$_ble_edit_kill_ring"
local ntext=${#atext[@]}
if [[ $graphical ]]; then
ble-edit/content/find-logical-bol; local bol=$ret
local bx by x y c
ble/textmap#getxy.cur --prefix=b "$bol"
ble/textmap#getxy.cur "$_ble_edit_ind"
((y-=by,c=y*cols+x))
else
ble-edit/content/find-logical-bol; local bol=$ret
local c=$((_ble_edit_ind-bol))
fi
local -a ins_beg=() ins_end=() ins_text=()
local i is_newline=
for ((i=0;i<ntext;i++)); do
if ((i>0)); then
ble-edit/content/find-logical-bol "$bol" 1
if ((bol==ret)); then
is_newline=1
else
bol=$ret
[[ $graphical ]] && ble/textmap#getxy.cur --prefix=b "$bol"
fi
fi
# 挿入文字列
local text=${atext[i]}
local fill=$((afill[i]))
if ((arg>1)); then
ret=
((fill)) && ble/string#repeat ' ' "$fill"
ble/string#repeat "$text$ret" "$arg"
text=${ret::${#ret}-fill}
fi
# 挿入位置と padding
local index iend=
if [[ $is_newline ]]; then
index=${#_ble_edit_str}
ble/string#repeat ' ' "$c"
text=$'\n'$ret$text
elif [[ $graphical ]]; then
ble-edit/content/find-logical-eol "$bol"; local eol=$ret
ble/textmap#get-index-at "$x" "$((by+y))"; ((index>eol&&(index=eol)))
# left padding (行末がより左にある、または、全角文字があるとき)
local ax ay ac; ble/textmap#getxy.out --prefix=a "$index"
((ay-=by,ac=ay*cols+ax))
if ((ac<c)); then
ble/string#repeat ' ' "$((c-ac))"
text=$ret$text
# タブを空白に変換
if ((index<eol)) && [[ ${_ble_edit_str:index:1} == $'\t' ]]; then
local rx ry rc; ble/textmap#getxy.out --prefix=r "$((index+1))"
((rc=(ry-by)*cols+rx))
ble/string#repeat ' ' "$((rc-c))"
text=$text$ret
iend=$((index+1))
fi
fi
# right padding (行末がより右にあるとき)
if ((index<eol&&fill)); then
ble/string#repeat ' ' "$fill"
text=$text$ret
fi
else
ble-edit/content/find-logical-eol "$bol"; local eol=$ret
local index=$((bol+c))
if ((index<eol)); then
if ((fill)); then
ble/string#repeat ' ' "$fill"
text=$text$ret
fi
elif ((index>eol)); then
ble/string#repeat ' ' "$((index-eol))"
text=$ret$text
index=$eol
fi
fi
ble/array#push ins_beg "$index"
ble/array#push ins_end "${iend:-$index}"
ble/array#push ins_text "$text"
done
# 逆順に挿入
ble/keymap:vi/mark/start-edit-area
local i=${#ins_beg[@]}
while ((i--)); do
local ibeg=${ins_beg[i]} iend=${ins_end[i]} text=${ins_text[i]}
ble/widget/.replace-range "$ibeg" "$iend" "$text"
done
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/repeat/record
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
ble/keymap:vi/adjust-command-mode
}
function ble/widget/vi_nmap/paste.impl {
local arg=$1 reg=$2 is_after=$3
if [[ $reg ]]; then
local _ble_edit_kill_ring _ble_edit_kill_type
ble/keymap:vi/register#load "$reg"
fi
[[ $_ble_edit_kill_ring ]] || return 0
local ret
if [[ $_ble_edit_kill_type == L ]]; then
ble/string#repeat "$_ble_edit_kill_ring" "$arg"
local content=$ret
local index dbeg dend
if ((is_after)); then
ble-edit/content/find-logical-eol; index=$ret
if ((index==${#_ble_edit_str})); then
content=$'\n'${content%$'\n'}
((dbeg=index+1,dend=index+${#content}))
else
((index++,dbeg=index,dend=index+${#content}-1))
fi
else
ble-edit/content/find-logical-bol
((index=ret,dbeg=index,dend=index+${#content}-1))
fi
ble/widget/.replace-range "$index" "$index" "$content"
_ble_edit_ind=$dbeg
ble/keymap:vi/mark/set-previous-edit-area "$dbeg" "$dend"
ble/keymap:vi/repeat/record
ble/widget/vi-command/first-non-space
elif [[ $_ble_edit_kill_type == B:* ]]; then
if ((is_after)) && ! ble-edit/content/eolp; then
((_ble_edit_ind++))
fi
ble/widget/vi_nmap/paste.impl/block "$arg"
else
if ((is_after)) && ! ble-edit/content/eolp; then
((_ble_edit_ind++))
fi
ble/string#repeat "$_ble_edit_kill_ring" "$arg"
local beg=$_ble_edit_ind
ble/widget/.insert-string "$ret"
local end=$_ble_edit_ind
ble/keymap:vi/mark/set-previous-edit-area "$beg" "$end"
ble/keymap:vi/repeat/record
[[ $_ble_keymap_vi_single_command ]] || ((_ble_edit_ind--))
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
ble/keymap:vi/adjust-command-mode
fi
return 0
}
function ble/widget/vi_nmap/paste-after {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi_nmap/paste.impl "$ARG" "$REG" 1
}
function ble/widget/vi_nmap/paste-before {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi_nmap/paste.impl "$ARG" "$REG" 0
}
function ble/widget/vi-rlfunc/paste-from-clipboard {
ble/keymap:vi/clear-arg
local clipboard
if ! ble/edit/get-clipboard; then
ble/widget/.bell
return 1
fi
local _ble_edit_kill_ring=$clipboard
local _ble_edit_kill_type=
ble/widget/vi_nmap/paste.impl 1 '' 1
return 0
}
#------------------------------------------------------------------------------
# command: x s X C D
# nmap x, <delete>
function ble/widget/vi_nmap/kill-forward-char {
_ble_keymap_vi_opfunc=d
ble/widget/vi-command/forward-char
}
# nmap s
function ble/widget/vi_nmap/kill-forward-char-and-insert {
_ble_keymap_vi_opfunc=c
ble/widget/vi-command/forward-char
}
# nmap X
function ble/widget/vi_nmap/kill-backward-char {
_ble_keymap_vi_opfunc=d
ble/widget/vi-command/backward-char
}
# nmap D
function ble/widget/vi_nmap/kill-forward-line {
_ble_keymap_vi_opfunc=d
ble/widget/vi-command/forward-eol
}
# nmap C
function ble/widget/vi_nmap/kill-forward-line-and-insert {
_ble_keymap_vi_opfunc=c
ble/widget/vi-command/forward-eol
}
#------------------------------------------------------------------------------
# command: w W b B e E
function ble/widget/vi-command/forward-word.impl {
local arg=$1 flag=$2 reg=$3 rex_word=$4
local ifs=$_ble_term_IFS
if [[ $flag == c && ${_ble_edit_str:_ble_edit_ind:1} != [$ifs] ]]; then
# Note: cw cW は特別な動作
# http://vim-jp.org/vimdoc-ja/change.html#cw
ble/widget/vi-command/forward-word-end.impl "$arg" "$flag" "$reg" "$rex_word" allow_here
return "$?"
fi
local b=$'[ \t]' n=$'\n'
local rex="^((($rex_word)$n?|$b+$n?|$n)($b+$n)*$b*){0,$arg}" # 単語先頭または空行に止まる
[[ ${_ble_edit_str:_ble_edit_ind} =~ $rex ]]
local index=$((_ble_edit_ind+${#BASH_REMATCH}))
if [[ $flag ]]; then
# :help word-motions の特別規則 (通過した最後の単語が行末にあるとき)
local rematch1=${BASH_REMATCH[1]}
if local rex="$n$b*\$"; [[ $rematch1 =~ $rex ]]; then
local suffix_len=${#BASH_REMATCH}
((suffix_len<${#rematch1})) &&
((index-=suffix_len))
fi
fi
ble/widget/vi-command/exclusive-goto.impl "$index" "$flag" "$reg"
}
function ble/widget/vi-command/forward-word-end.impl {
local arg=$1 flag=$2 reg=$3 rex_word=$4 opts=$5
local IFS=$_ble_term_IFS
local rex="^([$IFS]*($rex_word)?){0,$arg}" # 単語末端に止まる。空行には止まらない
local offset=1; [[ :$opts: == *:allow_here:* ]] && offset=0
[[ ${_ble_edit_str:_ble_edit_ind+offset} =~ $rex ]]
local index=$((_ble_edit_ind+offset+${#BASH_REMATCH}-1))
((index<_ble_edit_ind&&(index=_ble_edit_ind)))
[[ ! $flag && $BASH_REMATCH && ${_ble_edit_str:index:1} == [$IFS] ]] && ble/widget/.bell
ble/widget/vi-command/inclusive-goto.impl "$index" "$flag" "$reg"
}
function ble/widget/vi-command/backward-word.impl {
local arg=$1 flag=$2 reg=$3 rex_word=$4
local b=$'[ \t]' n=$'\n'
local rex="((($rex_word)$n?|$b+$n?|$n)($b+$n)*$b*){0,$arg}\$" # 単語先頭または空行に止まる
[[ ${_ble_edit_str::_ble_edit_ind} =~ $rex ]]
local index=$((_ble_edit_ind-${#BASH_REMATCH}))
ble/widget/vi-command/exclusive-goto.impl "$index" "$flag" "$reg"
}
function ble/widget/vi-command/backward-word-end.impl {
local arg=$1 flag=$2 reg=$3 rex_word=$4
local i=$'[ \t\n]' b=$'[ \t]' n=$'\n' w="($rex_word)"
local rex1="(^|$w$n?|$n)($b+$n)*$b*"
local rex="($rex1)($rex1){$((arg-1))}($rex_word|$i)\$" # 単語末端または空行に止まる
[[ ${_ble_edit_str::_ble_edit_ind+1} =~ $rex ]]
local index=$((_ble_edit_ind+1-${#BASH_REMATCH}))
local rematch3=${BASH_REMATCH[3]} # 最初の ($rex_word)
[[ $rematch3 ]] && ((index+=${#rematch3}-1))
ble/widget/vi-command/inclusive-goto.impl "$index" "$flag" "$reg"
}
# motion w
function ble/widget/vi-command/forward-vword {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/forward-word.impl "$ARG" "$FLAG" "$REG" "$_ble_keymap_vi_REX_WORD"
}
# motion e
function ble/widget/vi-command/forward-vword-end {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/forward-word-end.impl "$ARG" "$FLAG" "$REG" "$_ble_keymap_vi_REX_WORD"
}
# motion b
function ble/widget/vi-command/backward-vword {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/backward-word.impl "$ARG" "$FLAG" "$REG" "$_ble_keymap_vi_REX_WORD"
}
# motion ge
function ble/widget/vi-command/backward-vword-end {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/backward-word-end.impl "$ARG" "$FLAG" "$REG" "$_ble_keymap_vi_REX_WORD"
}
# motion W
function ble/widget/vi-command/forward-uword {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/forward-word.impl "$ARG" "$FLAG" "$REG" $'[^ \t\n]+'
}
# motion E
function ble/widget/vi-command/forward-uword-end {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/forward-word-end.impl "$ARG" "$FLAG" "$REG" $'[^ \t\n]+'
}
# motion B
function ble/widget/vi-command/backward-uword {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/backward-word.impl "$ARG" "$FLAG" "$REG" $'[^ \t\n]+'
}
# motion gE
function ble/widget/vi-command/backward-uword-end {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/backward-word-end.impl "$ARG" "$FLAG" "$REG" $'[^ \t\n]+'
}
#------------------------------------------------------------------------------
# command: [cdy]?[|HL] G gg
# nmap |
function ble/widget/vi-command/nth-column {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret index
ble-edit/content/find-logical-bol; local bol=$ret
ble-edit/content/find-logical-eol; local eol=$ret
if ble/edit/use-textmap; then
local bx by; ble/textmap#getxy.cur --prefix=b "$bol" # Note: 先頭行はプロンプトにより bx!=0
local ex ey; ble/textmap#getxy.cur --prefix=e "$eol"
local dstx=$((bx+ARG-1)) dsty=$by cols=${COLUMNS:-80}
((dsty+=dstx/cols,dstx%=cols))
((dsty>ey&&(dsty=ey,dstx=ex)))
ble/textmap#get-index-at "$dstx" "$dsty" # local variable "index" is set here
# Note: 何故かノーマルモードで d や c を実行するときには行末に行かないのに、
# ビジュアルモードでは行末に行くことができるようだ。
[[ $_ble_decode_keymap != vi_[xs]map ]] &&
ble-edit/content/nonbol-eolp "$index" && ((index--))
else
[[ $_ble_decode_keymap != vi_[xs]map ]] &&
ble-edit/content/nonbol-eolp "$eol" && ((eol--))
((index=bol+ARG-1,index>eol&&(index=eol)))
fi
ble/widget/vi-command/exclusive-goto.impl "$index" "$FLAG" "$REG" nobell
}
# nmap H
function ble/widget/vi-command/nth-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
[[ $FLAG ]] || ble/keymap:vi/mark/set-jump # ``
ble/widget/vi-command/linewise-goto.impl "0:$((ARG-1))" "$FLAG" "$REG"
}
# nmap L
function ble/widget/vi-command/nth-last-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
[[ $FLAG ]] || ble/keymap:vi/mark/set-jump # ``
ble/widget/vi-command/linewise-goto.impl "${#_ble_edit_str}:$((-(ARG-1)))" "$FLAG" "$REG"
}
# nmap gg in history
function ble/widget/vi-command/history-beginning {
local ARG FLAG REG; ble/keymap:vi/get-arg 0
if [[ $FLAG ]]; then
if ((ARG)); then
_ble_keymap_vi_oparg=$ARG
else
_ble_keymap_vi_oparg=
fi
_ble_keymap_vi_opfunc=$FLAG
_ble_keymap_vi_reg=$REG
ble/widget/vi-command/nth-line
return "$?"
fi
if ((ARG)); then
ble-edit/history/goto "$((ARG-1))"
else
ble/widget/history-beginning
fi
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
ble/keymap:vi/adjust-command-mode
return 0
}
# nmap G in history
function ble/widget/vi-command/history-end {
local ARG FLAG REG; ble/keymap:vi/get-arg 0
if [[ $FLAG ]]; then
_ble_keymap_vi_opfunc=$FLAG
_ble_keymap_vi_reg=$REG
if ((ARG)); then
_ble_keymap_vi_oparg=$ARG
ble/widget/vi-command/nth-line
else
_ble_keymap_vi_oparg=
ble/widget/vi-command/nth-last-line
fi
return "$?"
fi
if ((ARG)); then
ble-edit/history/goto "$((ARG-1))"
else
ble/widget/history-end
fi
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
ble/keymap:vi/adjust-command-mode
return 0
}
function ble/widget/vi-command/.history-goto {
local new_index=$1 old_index
ble/history/get-index -v old_index
if ((new_index==old_index)); then
ble/widget/vi-command/bell 'already on history !'"$new_index"
return 1
fi
local point_opts point point_x
if ((new_index>old_index)); then
point_opts=forward
else
point_opts=backward
fi
ble-edit/history/goto "$new_index"
# adjust the cursor position
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
ble/keymap:vi/adjust-command-mode
}
function ble/widget/vi-command/history-next {
if [[ $_ble_history_prefix || $_ble_history_load_done ]]; then
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/history/initialize
ble/widget/vi-command/.history-goto "$((_ble_history_INDEX+ARG))"
else
ble/keymap:vi/clear-arg
ble/widget/vi-command/bell
fi
}
function ble/widget/vi-command/history-prev {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/history/initialize
ble/widget/vi-command/.history-goto "$((_ble_history_INDEX-ARG))"
}
function ble/widget/vi-command/history-goto {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
if ((--ARG<0)); then
ble/history/initialize
((ARG+=_ble_history_COUNT))
fi
ble/widget/vi-command/.history-goto "$ARG"
}
# nmap G
# Note: vim では G はこの振る舞いだが、blesh では実際には
# vi-command/history-end が束縛されるのでこれは既定では使われない。
function ble/widget/vi-command/last-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 0
[[ $FLAG ]] || ble/keymap:vi/mark/set-jump # ``
if ((ARG)); then
ble/widget/vi-command/linewise-goto.impl "0:$((ARG-1))" "$FLAG" "$REG"
else
ble/widget/vi-command/linewise-goto.impl "${#_ble_edit_str}:0" "$FLAG" "$REG"
fi
}
# nmap C-home / gg
# Note: nth-line (H) との違いは jump でない事のみである。
# Note: vim では gg もこの振る舞いだが、blesh では gg は
# 既定では vi-command/history-beginning に束縛される。
function ble/widget/vi-command/first-nol {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/linewise-goto.impl "0:$((ARG-1))" "$FLAG" "$REG"
}
# nmap C-end
function ble/widget/vi-command/last-eol {
local ARG FLAG REG; ble/keymap:vi/get-arg ''
local ret index
if [[ $ARG ]]; then
ble-edit/content/find-logical-eol 0 "$((ARG-1))"; index=$ret
else
ble-edit/content/find-logical-eol "${#_ble_edit_str}"; index=$ret
fi
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
ble/widget/vi-command/inclusive-goto.impl "$index" "$FLAG" "$REG" nobell
}
#------------------------------------------------------------------------------
# command: r gr
## @fn ble/widget/vi_nmap/replace-char.impl code [overwrite_mode]
## @param[in] overwrite_mode
## 置換する文字の挿入方法を指定します。
function ble/widget/vi_nmap/replace-char.impl {
local key=$1 overwrite_mode=${2:-R}
_ble_edit_overwrite_mode=
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret
if ((key==(_ble_decode_Ctrl|91))); then # C-[
ble/keymap:vi/adjust-command-mode
return 27
elif ! ble/keymap:vi/k2c "$key"; then
ble/widget/vi-command/bell
return 1
fi
local pos=$_ble_edit_ind
ble/keymap:vi/mark/start-edit-area
{
local -a KEYS; KEYS=("$ret")
local _ble_edit_arg=$ARG
local _ble_edit_overwrite_mode=$overwrite_mode
local ble_widget_self_insert_opts=nolineext
ble/widget/self-insert
ble/util/unlocal KEYS
}
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/repeat/record
((pos<_ble_edit_ind&&_ble_edit_ind--))
ble/keymap:vi/adjust-command-mode
return 0
}
function ble/widget/vi_nmap/replace-char.hook {
ble/widget/vi_nmap/replace-char.impl "$1" R
}
function ble/widget/vi_nmap/replace-char {
_ble_edit_overwrite_mode=R
ble/keymap:vi/async-read-char ble/widget/vi_nmap/replace-char.hook
}
function ble/widget/vi_nmap/virtual-replace-char.hook {
ble/widget/vi_nmap/replace-char.impl "$1" 1
}
function ble/widget/vi_nmap/virtual-replace-char {
_ble_edit_overwrite_mode=1
ble/keymap:vi/async-read-char ble/widget/vi_nmap/virtual-replace-char.hook
}
#------------------------------------------------------------------------------
# command: J gJ o O
# nmap J
function ble/widget/vi_nmap/connect-line-with-space {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret
ble-edit/content/find-logical-eol; local eol1=$ret
ble-edit/content/find-logical-eol "$_ble_edit_ind" "$((ARG<=1?1:ARG-1))"; local eol2=$ret
ble-edit/content/find-logical-bol "$eol2"; local bol2=$ret
if ((eol1<eol2)); then
local text=${_ble_edit_str:eol1:eol2-eol1}
text=${text//$'\n'/' '}
ble/widget/.replace-range "$eol1" "$eol2" "$text"
ble/keymap:vi/mark/set-previous-edit-area "$eol1" "$eol2"
ble/keymap:vi/repeat/record
_ble_edit_ind=$((bol2-1))
ble/keymap:vi/adjust-command-mode
return 0
else
ble/widget/vi-command/bell
return 1
fi
}
# nmap gJ
function ble/widget/vi_nmap/connect-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret
ble-edit/content/find-logical-eol; local eol1=$ret
ble-edit/content/find-logical-eol "$_ble_edit_ind" "$((ARG<=1?1:ARG-1))"; local eol2=$ret
ble-edit/content/find-logical-bol "$eol2"; local bol2=$ret
if ((eol1<eol2)); then
local text=${_ble_edit_str:eol1:bol2-eol1}
text=${text//$'\n'}
ble/widget/.replace-range "$eol1" "$bol2" "$text"
local delta=$((${#text}-(bol2-eol1)))
ble/keymap:vi/mark/set-previous-edit-area "$eol1" "$((eol2+delta))"
ble/keymap:vi/repeat/record
_ble_edit_ind=$((bol2+delta))
ble/keymap:vi/adjust-command-mode
return 0
else
ble/widget/vi-command/bell
return 1
fi
}
function ble/widget/vi_nmap/insert-mode-at-forward-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret
ble-edit/content/find-logical-bol; local bol=$ret
ble-edit/content/find-logical-eol; local eol=$ret
ble-edit/content/find-non-space "$bol"; local indent=${_ble_edit_str:bol:ret-bol}
_ble_edit_ind=$eol
ble/widget/.insert-string $'\n'"$indent"
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/insert-mode-at-backward-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret
ble-edit/content/find-logical-bol; local bol=$ret
ble-edit/content/find-non-space "$bol"; local indent=${_ble_edit_str:bol:ret-bol}
_ble_edit_ind=$bol
ble/widget/.insert-string "$indent"$'\n'
_ble_edit_ind=$((bol+${#indent}))
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
#------------------------------------------------------------------------------
# command: f F t F
## @var _ble_keymap_vi_char_search
## 前回の ble/widget/vi-command/search-char.impl/core の検索を記録します。
_ble_keymap_vi_char_search=
## @fn ble/widget/vi-command/search-char.impl/core opts key|char
##
## @param[in] opts
## 以下のフラグ文字から構成される文字列です。
##
## b 後方検索であることを表します。
##
## p 見つかった文字の1つ手前に移動することを表します。
##
## r 繰り返し検索であることを表します。
## このとき第1引数は文字 char と解釈されます。
## これ以外のとき第1引数はキーコード key と解釈されます。
##
## @param[in] key
## @param[in] char
## key は検索対象のキーコードを指定します。
## char は検索対象の文字を指定します。
## どちらで解釈されるかは後述する opts のフラグ r に依存します。
##
##
function ble/widget/vi-command/search-char.impl/core {
local opts=$1 key=$2
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret c
[[ $opts != *p* ]]; local isprev=$?
[[ $opts != *r* ]]; local isrepeat=$?
if ((isrepeat)); then
c=$key
elif ((key==(_ble_decode_Ctrl|91))); then # C-[ -> cancel
return 27
else
ble/keymap:vi/k2c "$key" || return 1
ble/util/c2s "$ret"; local c=$ret
fi
[[ $c ]] || return 1
((isrepeat)) || _ble_keymap_vi_char_search=$c$opts
local index
if [[ $opts == *b* ]]; then
# backward search
ble-edit/content/find-logical-bol; local bol=$ret
local base=$_ble_edit_ind
((isrepeat&&isprev&&base--,base>bol)) || return 1
local line=${_ble_edit_str:bol:base-bol}
ble/string#last-index-of "$line" "$c" "$ARG"
((ret>=0)) || return 1
((index=bol+ret,isprev&&index++))
ble/widget/vi-command/exclusive-goto.impl "$index" "$FLAG" "$REG" nobell
return "$?"
else
# forward search
ble-edit/content/find-logical-eol; local eol=$ret
local base=$((_ble_edit_ind+1))
((isrepeat&&isprev&&base++,base<eol)) || return 1
local line=${_ble_edit_str:base:eol-base}
ble/string#index-of "$line" "$c" "$ARG"
((ret>=0)) || return 1
((index=base+ret,isprev&&index--))
ble/widget/vi-command/inclusive-goto.impl "$index" "$FLAG" "$REG" nobell
return "$?"
fi
}
function ble/widget/vi-command/search-char.impl {
if ble/widget/vi-command/search-char.impl/core "$1" "$2"; then
ble/keymap:vi/adjust-command-mode
return 0
else
ble/widget/vi-command/bell
return 1
fi
}
function ble/widget/vi-command/search-forward-char {
ble/keymap:vi/async-read-char ble/widget/vi-command/search-char.impl f
}
function ble/widget/vi-command/search-forward-char-prev {
ble/keymap:vi/async-read-char ble/widget/vi-command/search-char.impl fp
}
function ble/widget/vi-command/search-backward-char {
ble/keymap:vi/async-read-char ble/widget/vi-command/search-char.impl b
}
function ble/widget/vi-command/search-backward-char-prev {
ble/keymap:vi/async-read-char ble/widget/vi-command/search-char.impl bp
}
function ble/widget/vi-command/search-char-repeat {
[[ $_ble_keymap_vi_char_search ]] || ble/widget/.bell
local c=${_ble_keymap_vi_char_search::1} opts=${_ble_keymap_vi_char_search:1}
ble/widget/vi-command/search-char.impl "r$opts" "$c"
}
function ble/widget/vi-command/search-char-reverse-repeat {
[[ $_ble_keymap_vi_char_search ]] || ble/widget/.bell
local c=${_ble_keymap_vi_char_search::1} opts=${_ble_keymap_vi_char_search:1}
if [[ $opts == *b* ]]; then
opts=f${opts//b}
else
opts=b${opts//f}
fi
ble/widget/vi-command/search-char.impl "r$opts" "$c"
}
#------------------------------------------------------------------------------
# command: %
## @fn ble/widget/vi-command/search-matchpair/.search-forward
## @var[in] _ble_edit_str, ch1, ch2, index
## @var[out] ret
function ble/widget/vi-command/search-matchpair/.search-forward {
ble/string#index-of-chars "$_ble_edit_str" "$ch1$ch2" "$((index+1))"
}
function ble/widget/vi-command/search-matchpair/.search-backward {
ble/string#last-index-of-chars "$_ble_edit_str" "$ch1$ch2" "$index"
}
function ble/widget/vi-command/search-matchpair-or {
local ARG FLAG REG; ble/keymap:vi/get-arg -1
if ((ARG>=0)); then
_ble_keymap_vi_oparg=$ARG
_ble_keymap_vi_opfunc=$FLAG
_ble_keymap_vi_reg=$REG
ble/widget/"$@"
return "$?"
fi
local open='({[' close=')}]'
local ret
ble-edit/content/find-logical-eol; local eol=$ret
if ! ble/string#index-of-chars "${_ble_edit_str::eol}" '(){}[]' "$_ble_edit_ind"; then
ble/keymap:vi/adjust-command-mode
return 1
fi
local index1=$ret ch1=${_ble_edit_str:ret:1}
if [[ $ch1 == ["$open"] ]]; then
local i=${open%%"$ch"*}; i=${#i}
local ch2=${close:i:1}
local searcher=ble/widget/vi-command/search-matchpair/.search-forward
else
local i=${close%%"$ch"*}; i=${#i}
local ch2=${open:i:1}
local searcher=ble/widget/vi-command/search-matchpair/.search-backward
fi
local index=$index1 count=1
while "$searcher"; do
index=$ret
if [[ ${_ble_edit_str:ret:1} == "$ch1" ]]; then
((++count))
else
((--count==0)) && break
fi
done
if ((count)); then
ble/keymap:vi/adjust-command-mode
return 1
fi
[[ $FLAG ]] || ble/keymap:vi/mark/set-jump # ``
ble/widget/vi-command/inclusive-goto.impl "$index" "$FLAG" "$REG" nobell
}
function ble/widget/vi-command/percentage-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 0
local ret; ble/string#count-char "$_ble_edit_str" $'\n'; local nline=$((ret+1))
local iline=$(((ARG*nline+99)/100))
ble/widget/vi-command/linewise-goto.impl "0:$((iline-1))" "$FLAG" "$REG"
}
#------------------------------------------------------------------------------
# command: go
function ble/widget/vi-command/nth-byte {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
((ARG--))
local offset=0 text=$_ble_edit_str len=${#_ble_edit_str}
local left nleft ret
while ((ARG>0&&len>1)); do
left=${text::len/2}
ble/util/strlen "$left"; nleft=$ret
if ((ARG<nleft)); then
text=$left
((len/=2))
else
text=${text:len/2}
((offset+=len/2,
ARG-=nleft,
len-=len/2))
fi
done
ble/keymap:vi/needs-eol-fix "$offset" && ((offset--))
ble/widget/vi-command/exclusive-goto.impl "$offset" "$FLAG" "$REG" nobell
}
#------------------------------------------------------------------------------
# text objects
_ble_keymap_vi_text_object=
## @fn ble/keymap:vi/text-object/word.impl arg flag reg type
## @fn ble/keymap:vi/text-object/quote.impl arg flag reg type
## @fn ble/keymap:vi/text-object/block.impl arg flag reg type
## @fn ble/keymap:vi/text-object/tag.impl arg flag reg type
## @fn ble/keymap:vi/text-object/sentence.impl arg flag reg type
## @fn ble/keymap:vi/text-object/paragraph.impl arg flag reg type
##
## @exit テキストオブジェクトの処理が完了したときに 0 を返します。
##
## @fn ble/keymap:vi/text-object/word.extend-forward
## Note #D0855
## @var[in] type arg
## @var[in] rex_word nl space ifs
## @var[in,out] beg end
## @var[out] flags
## A 先頭に空白が含まれる事を表す。
## Z 末尾に空白が含まれる事を表す。
## I 単語前半の取り込みが試みられた事を表す。
function ble/keymap:vi/text-object/word.extend-forward {
local rex
flags=
[[ ${_ble_edit_str:beg:1} == ["$ifs"] ]] && flags=${flags}A
if [[ $_ble_decode_keymap != vi_[xs]map ]]; then
flags=${flags}I
elif ((_ble_edit_mark==_ble_edit_ind)); then
flags=${flags}I
fi
local rex_unit
local W='('$rex_word')' b='['$space']' n=$nl
if [[ $type == i* ]]; then
rex_unit='^'$W'|^'$b'+|^'$n
elif [[ $type == a* ]]; then
rex_unit='^'$W$b'*|^'$b'+'$W'|^'$b'*'$n'('$b'+'$n')*('$n'|'$b'*'$W')'
else
return 1
fi
local i rematch=
for ((i=0;i<arg;i++)); do
if ((i==0)) && [[ $flags == *I* ]]; then
# 単語前方を取り込む
rex='('$rex_word')$|['$space']*['$ifs']$'
[[ ${_ble_edit_str::beg+1} =~ $rex ]] &&
((beg-=${#BASH_REMATCH}-1,end=beg))
else
[[ ${_ble_edit_str:end:1} == $'\n' ]] && ((end++))
fi
[[ ${_ble_edit_str:end} =~ $rex_unit ]] || return 1
rematch=$BASH_REMATCH
((end+=${#rematch}))
# Note: aw に対する正規表現では二重改行を読むが後退する。
[[ $type == a* && $rematch == *$'\n\n' ]] && ((end--))
# Note: Vim では何故か最初の一致だけ改行を除去。
# 最後の一致の改行は exclusive にする事で、
# 呼び出し元に除去させている様な気がする。
# Note: aw の時は "非空白から改行" に一致する事はない。
if ((i==0)) && [[ $flags == *I* ]] || ((i==arg-1)); then
[[ $type == i* && $rematch == *"$nl" ]] && ((end--))
fi
done
[[ ${_ble_edit_str:end-1:1} == *["$ifs"] ]] && flags=${flags}Z
if [[ $type == a* && $flags != *[AZ]* ]]; then
# aw で前後に空白が含まれない時、前方の空白を取り込む
# Note: vim の実装 (search.c (current_word)) では
# 行頭 exclusive でも前方空白を取り込むが、
# aw において行頭 exclusive になる事は普通はないので謎。
# virtual_active() の時行の途中で oneleft() が失敗する事はあるが、
# この様な状況を意図してこの条件が加えられたとは思えない。
if rex='['$space']+$'; [[ ${_ble_edit_str::beg} =~ $rex ]]; then
local p=$((beg-${#BASH_REMATCH}))
ble-edit/content/bolp "$p" || beg=$p
fi
fi
return 0
}
## @fn ble/keymap:vi/text-object/word.extend-backward
## Note #D0855
## @var[in,out] beg
## @var[in] type arg
## @var[in] rex_word nl space ifs
function ble/keymap:vi/text-object/word.extend-backward {
local rex_unit=
local W='('$rex_word')' b='['$space']' n=$nl
if [[ $type == i* ]]; then
rex_unit='('$W'|'$b'+)'$n'?$|'$n'$'
elif [[ $type == a* ]]; then
rex_unit=$b'*'$W$n'?$|'$W'?'$b'*('$n'('$b'+'$n')*'$b'*)?('$b$n'?|'$n')$'
else
return 1
fi
local count=$arg
while ((count--)); do
[[ ${_ble_edit_str::beg} =~ $rex_unit ]] || return 1
((beg-=${#BASH_REMATCH}))
# Note: vim の振る舞いに倣って
local match=${BASH_REMATCH%"$nl"}
if ((beg==0&&${#match}>=2)); then
if [[ $type == i* ]]; then
[[ $match == ["$space"]* ]] && beg=1
elif [[ $type == a* ]]; then
[[ $match == *[!"$ifs"] ]] && beg=1
fi
fi
done
return 0
}
function ble/keymap:vi/text-object/word.impl {
local arg=$1 flag=$2 reg=$3 type=$4
local space=$' \t' nl=$'\n' ifs=$_ble_term_IFS
((arg==0)) && return 0
local rex_word
if [[ $type == ?W ]]; then
rex_word="[^$ifs]+"
else
rex_word=$_ble_keymap_vi_REX_WORD
fi
local index=$_ble_edit_ind
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
if ((index<_ble_edit_mark)); then
local beg=$index
if ble/keymap:vi/text-object/word.extend-backward; then
_ble_edit_ind=$beg
else
_ble_edit_ind=0
ble/widget/.bell
fi
ble/keymap:vi/adjust-command-mode
return 0
fi
fi
local beg=$index end=$index flags=
if ! ble/keymap:vi/text-object/word.extend-forward; then
# 一致失敗
index=${#_ble_edit_str}
ble-edit/content/nonbol-eolp "$index" && ((index--))
_ble_edit_ind=$index
ble/widget/vi-command/bell
return 1
fi
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
((end--))
ble-edit/content/nonbol-eolp "$end" && ((end--))
((beg<_ble_edit_mark)) && _ble_edit_mark=$beg
[[ $_ble_edit_mark_active == vi_line ]] &&
_ble_edit_mark_active=vi_char
_ble_edit_ind=$end
ble/keymap:vi/adjust-command-mode
return 0
else
ble/widget/vi-command/exclusive-range.impl "$beg" "$end" "$flag" "$reg"
fi
}
## @fn ble/keymap:vi/text-object:quote/is-closing-quote index
## @var[in] quote
function ble/keymap:vi/text-object:quote/is-closing-quote {
local index=${1:-$_ble_edit_ind}
[[ ${_ble_edit_str:index:1} == "$quote" ]] || return 1
local ret
ble-edit/content/find-logical-bol "$index"; local bol=$ret
ble/string#count-char "${_ble_edit_str:bol:_ble_edit_ind-bol}" "$quote"
((ret%2==1))
}
## @fn ble/keymap:vi/text-object:quote/.next [index]
## @var[in] quote
## @var[out] ret
function ble/keymap:vi/text-object:quote/.next {
local index=${1:-$((_ble_edit_ind+1))} nl=$'\n'
local rex="^[^$nl$quote]*$quote"
[[ ${_ble_edit_str:index} =~ $rex ]] || return 1
((ret=index+${#BASH_REMATCH}-1))
return 0
}
## @fn ble/keymap:vi/text-object:quote/.prev [index]
## @var[in] quote
## @var[out] ret
function ble/keymap:vi/text-object:quote/.prev {
local index=${1:-_ble_edit_ind} nl=$'\n'
local rex="$quote[^$nl$quote]*\$"
[[ ${_ble_edit_str::index} =~ $rex ]] || return 1
((ret=index-${#BASH_REMATCH}))
return 0
}
function ble/keymap:vi/text-object/quote.impl {
local arg=$1 flag=$2 reg=$3 type=$4
local ret quote=${type:1}
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
if ble/keymap:vi/text-object:quote/.xmap; then
ble/keymap:vi/adjust-command-mode
return 0
else
ble/widget/vi-command/bell
return 1
fi
fi
local beg= end=
if [[ ${_ble_edit_str:_ble_edit_ind:1} == "$quote" ]]; then
ble-edit/content/find-logical-bol; local bol=$ret
ble/string#count-char "${_ble_edit_str:bol:_ble_edit_ind-bol}" "$quote"
if ((ret%2==1)); then
# 現在終了引用符
((end=_ble_edit_ind+1))
ble/keymap:vi/text-object:quote/.prev && beg=$ret
else
((beg=_ble_edit_ind))
ble/keymap:vi/text-object:quote/.next && end=$((ret+1))
fi
elif ble/keymap:vi/text-object:quote/.prev && beg=$ret; then
ble/keymap:vi/text-object:quote/.next && end=$((ret+1))
elif ble/keymap:vi/text-object:quote/.next && beg=$ret; then
ble/keymap:vi/text-object:quote/.next "$((beg+1))" && end=$((ret+1))
fi
if [[ $beg && $end ]]; then
[[ $type == i* || arg -gt 1 ]] && ((beg++,end--))
ble/widget/vi-command/exclusive-range.impl "$beg" "$end" "$flag" "$reg"
else
ble/widget/vi-command/bell
return 1
fi
}
## @fn ble/keymap:vi/text-object:quote/.expand-xmap-range mode
## @param[in] mode
## @var[in,out] beg
## @var[in,out] end
function ble/keymap:vi/text-object:quote/.expand-xmap-range {
local inclusive=$1
((end++))
if ((inclusive==2)); then
local rex
rex=$'^[ \t]+'; [[ ${_ble_edit_str:end} =~ $rex ]] && ((end+=${#BASH_REMATCH}))
elif ((inclusive==0&&end-beg>2)); then
((beg++,end--))
fi
}
## @fn ble/keymap:vi/text-object:quote/.xmap
## @var[in] quote
function ble/keymap:vi/text-object:quote/.xmap {
# 複数行に亘る場合は失敗
local min=$_ble_edit_ind max=$_ble_edit_mark
((min>max)) && local min=$max max=$min
[[ ${_ble_edit_str:min:max+1-min} == *$'\n'* ]] && return 1
local inclusive=0
if [[ $type == a* ]]; then
inclusive=2
elif ((arg>1)); then
inclusive=1
fi
local ret
if ((_ble_edit_ind==_ble_edit_mark)); then
ble/keymap:vi/text-object:quote/.prev "$((_ble_edit_ind+1))" ||
ble/keymap:vi/text-object:quote/.next "$((_ble_edit_ind+1))" || return 1
if ble/keymap:vi/text-object:quote/is-closing-quote; then
local end=$ret
ble/keymap:vi/text-object:quote/.prev "$end" || return 1
local beg=$ret
else
local beg=$ret
ble/keymap:vi/text-object:quote/.next "$((beg+1))" || return 1
local end=$ret
fi
ble/keymap:vi/text-object:quote/.expand-xmap-range "$inclusive"
_ble_edit_mark=$beg
_ble_edit_ind=$((end-1))
return 0
elif ((_ble_edit_ind>_ble_edit_mark)); then
local updates_mark=
if [[ ${_ble_edit_str:_ble_edit_ind:1} == "$quote" ]]; then
# 現在位置に " があるとき。
ble/keymap:vi/text-object:quote/.next "$((_ble_edit_ind+1))" || return 1; local beg=$ret
if ble/keymap:vi/text-object:quote/.next "$((beg+1))"; then
local end=$ret
else
local end=$beg beg=$_ble_edit_ind
fi
else
# 現在位置以降の最初の 右" (その行の偶数番目の ") と対応する 左"
ble-edit/content/find-logical-bol; local bol=$ret
ble/string#count-char "${_ble_edit_str:bol:_ble_edit_ind-bol}" "$quote"
if ((ret%2==0)); then
ble/keymap:vi/text-object:quote/.next "$((_ble_edit_ind+1))" || return 1; local beg=$ret
ble/keymap:vi/text-object:quote/.next "$((beg+1))" || return 1; local end=$ret
else
ble/keymap:vi/text-object:quote/.prev "$_ble_edit_ind" || return 1; local beg=$ret
ble/keymap:vi/text-object:quote/.next "$((_ble_edit_ind+1))" || return 1; local end=$ret
fi
local i1=$((_ble_edit_mark?_ble_edit_mark-1:0))
[[ ${_ble_edit_str:i1:_ble_edit_ind-i1} != *"$quote"* ]] && updates_mark=1
fi
ble/keymap:vi/text-object:quote/.expand-xmap-range "$inclusive"
[[ $updates_mark ]] && _ble_edit_mark=$beg
_ble_edit_ind=$((end-1))
return 0
else
ble-edit/content/find-logical-bol; local bol=$ret nl=$'\n'
local rex="^([^$nl$quote]*$quote[^$nl$quote]*$quote)*[^$nl$quote]*$quote"
[[ ${_ble_edit_str:bol:_ble_edit_ind-bol} =~ $rex ]] || return 1
local beg=$((bol+${#BASH_REMATCH}-1))
ble/keymap:vi/text-object:quote/.next "$((beg+1))" || return 1
local end=$ret
ble/keymap:vi/text-object:quote/.expand-xmap-range "$inclusive"
[[ ${_ble_edit_str:_ble_edit_ind:_ble_edit_mark+2-_ble_edit_ind} != *"$quote"* ]] && _ble_edit_mark=$((end-1))
_ble_edit_ind=$beg
return 0
fi
}
## @fn ble/keymap:vi/text-object:block/.prev-matching-lparen [index [depth]]
function ble/keymap:vi/text-object:block/.prev-matching-lparen {
local index=${1:-$_ble_edit_ind} goal_count=${2:-1}
local p=$index count=0
while ble/string#last-index-of-chars "$_ble_edit_str" "$rparen$lparen" "$p"; do
p=$ret
if [[ ${_ble_edit_str:ret:1} == "$lparen" ]]; then
((++count==goal_count)) && return 0
else
((--count))
fi
done
ret=$count
return 1
}
## @fn ble/keymap:vi/text-object:block/.next-matching-rparen [index [depth]]
## @var[out] ret
## The position of the right paren is stored when the function succeeded.
## The nesting level of the current context relative to the end of the
## string is stored when the function failed.
function ble/keymap:vi/text-object:block/.next-matching-rparen {
local index=${1:-$_ble_edit_ind} goal_count=${2:-1}
local p=$index count=0
while ble/string#index-of-chars "$_ble_edit_str" "$rparen$lparen" "$p"; do
p=$((ret+1))
if [[ ${_ble_edit_str:ret:1} == "$rparen" ]]; then
((++count==goal_count)) && return 0
else
((--count))
fi
done
ret=$count
return 1
}
## @fn ble/keymap:vi/text-object:block/.next-matching-lparen [index [depth]]
function ble/keymap:vi/text-object:block/.next-matching-lparen {
local index=${1:-$_ble_edit_ind} goal_count=${2:-1}
local p=$index count=0
while ble/string#index-of-chars "$_ble_edit_str" "$rparen$lparen" "$p"; do
p=$((ret+1))
if [[ ${_ble_edit_str:ret:1} == "$rparen" ]]; then
((++count==goal_count)) && { ret=$count; return 1; }
else
((count+1==goal_count)) && return 0
((--count))
fi
done
ret=$count
return 1
}
## @fn ble/keymap:vi/text-object:block/.expand-one-level p1
## @var[ref] beg end
function ble/keymap:vi/text-object:block/.expand-one-level {
local p1=$1 beg1= end1= ret
ble/keymap:vi/text-object:block/.prev-matching-lparen "$p1" && beg1=$ret
ble/keymap:vi/text-object:block/.next-matching-rparen "$p1" && end1=$ret
if [[ $beg1 && $end1 ]]; then
beg=$beg1 end=$end1
elif [[ $beg1 || $end1 ]]; then
return 1
fi
}
## @fn ble/keymap:vi/text-object:block/.get-outer-range beg end
## @var[out] outer_beg outer_end
function ble/keymap:vi/text-object:block/.outer-range {
outer_beg=$1 outer_end=$2
if [[ $type == i* ]]; then
case ${_ble_edit_str::outer_beg} in
(*"$lparen"$'\n') ((outer_beg-=2)) ;;
(*"$lparen") ((outer_beg--)) ;;
esac
case ${_ble_edit_str:outer_end+1} in
($'\n'"$rparen"*) ((outer_end+=2)) ;;
("$rparen"*) ((outer_end++)) ;;
esac
fi
}
## @fn ble/keymap:vi/text-object:block/.search-block min max L R [opts]
## @var[out] beg end
function ble/keymap:vi/text-object:block/.search-block {
local ret p1=$1 p2=$2 L=$3 R=$4 opts=$5
[[ ${_ble_edit_str:p1:1} == "$L" ]] && ((p1++))
if ble/keymap:vi/text-object:block/.prev-matching-lparen "$p1" "$arg"; then
# We first attempt to search for "a surrounding pair (...)" at the
# specified level of $arg.
beg=$ret
ble/keymap:vi/text-object:block/.next-matching-rparen "$p1" "$arg" || return 1
end=$ret
if [[ :$opts: == *:reject-empty-here:* ]]; then
((beg+1<end)) || return 1
fi
if [[ :$opts: == *:check-expand:* ]]; then
# If the new range is essentially identical (or a prefix) to the current
# selection, we try to capture the range upper by one level.
local outer_beg outer_end
ble/keymap:vi/text-object:block/.outer-range "$p1" "$p2"
if ((outer_beg==beg&&outer_end>=end)); then
[[ $type == i* ]] && ((p1--))
ble/keymap:vi/text-object:block/.expand-one-level "$outer_beg" || return 1
fi
fi
elif ((ret<=0&&arg==1)); then
# When we fail to find "the surrounding pair (...)" at the top level, we
# next try "the next pair (...)". This is attempted only when the
# specified level is arg=1.
p1=$1
[[ ${_ble_edit_str:p1:1} == "$R" ]] && ((p1++))
ble/keymap:vi/text-object:block/.next-matching-lparen "$p1" || return 1
beg=$ret
ble/keymap:vi/text-object:block/.next-matching-rparen "$((beg+1))" || return 1
end=$ret
# Note: In Vim, ((beg+1<end)) check does not seem to be performed here.
# See also the next Note in the code comment below.
if [[ :$opts: == *:check-expand:* ]]; then
if [[ $type == i* ]]; then
local outer_end=$p2
case ${_ble_edit_str:outer_end+1} in
($'\n'"$rparen"*) ((outer_end+=2)) ;;
("$rparen"*) ((outer_end++)) ;;
esac
((outer_end<end)) || return 1
fi
fi
else
return 1
fi
return 0
}
## @fn ble/keymap:vi/text-object:block/.xmap
## @var[in] lparen rparen
## @var[in] arg
function ble/keymap:vi/text-object:block/.xmap {
if ((_ble_edit_ind==_ble_edit_mark)); then
local beg end p=$_ble_edit_ind
ble/keymap:vi/text-object:block/.search-block "$p" "$p" "$lparen" "$rparen" reject-empty-here || return 1
# i, a に応じて適切に範囲を決定する
if [[ $type == i* ]]; then
# Note: When ((beg + 1 == end)) with "the next pair (...)", the mark and
# the index is reversed in Vim 9.0. This might be a bug of Vim because
# when ((beg + 1 == end)), the text object fails for "the surrounding
# pair (...)" but this check seems skipped for "the next pair (...)".
# Nevertheless, ble.sh follows Vim's behavior.
((beg++,end--))
[[ ${_ble_edit_str:beg:1} == $'\n' ]] && ((beg++))
fi
_ble_edit_mark=$beg
_ble_edit_ind=$end
return 0
else
local min=$_ble_edit_mark max=$_ble_edit_ind
((min<max)) || local min=$max max=$min
ble/keymap:vi/text-object:block/.search-block "$min" "$max" "$rparen" "$lparen" reject-empty-here:check-expand || return 1
if [[ $type == i* ]]; then
((beg++,end--))
[[ ${_ble_edit_str:beg:1} == $'\n' ]] && ((beg++))
fi
_ble_edit_mark=$beg
_ble_edit_ind=$end
return 0
fi
}
function ble/keymap:vi/text-object/block.impl {
local arg=$1 flag=$2 reg=$3 type=$4
local ret paren=${type:1} lparen=${type:1:1} rparen=${type:2:1}
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
if ble/keymap:vi/text-object:block/.xmap; then
ble/keymap:vi/adjust-command-mode
return 0
else
ble/widget/vi-command/bell
return 1
fi
fi
local beg end p=$_ble_edit_ind
if ! ble/keymap:vi/text-object:block/.search-block "$p" "$p" "$lparen" "$rparen"; then
ble/widget/vi-command/bell
return 1
fi
((end++))
local linewise=
if [[ $type == *i* ]]; then
((beg++,end--))
[[ ${_ble_edit_str:beg:1} == $'\n' ]] && ((beg++))
((beg<end)) && ble-edit/content/bolp "$end" && ((end--))
((beg<end)) && ble-edit/content/bolp "$beg" && ble-edit/content/eolp "$end" && linewise=1
fi
if [[ $linewise ]]; then
ble/widget/vi-command/linewise-range.impl "$beg" "$end" "$flag" "$reg" goto_bol
else
ble/widget/vi-command/exclusive-range.impl "$beg" "$end" "$flag" "$reg"
fi
}
## @fn ble/keymap:vi/text-object:tag/.find-end-tag
## @var[in] beg
## @var[out] end
function ble/keymap:vi/text-object:tag/.find-end-tag {
local ifs=$_ble_term_IFS ret rex
rex="^<([^$ifs/>!]+)"; [[ ${_ble_edit_str:beg} =~ $rex ]] || return 1
ble/string#escape-for-extended-regex "${BASH_REMATCH[1]}"; local tagname=$ret
rex="^</?$tagname([$ifs]+([^>]*[^/])?)?>"
end=$beg
local count=0
while ble/string#index-of-chars "$_ble_edit_str" '<' "$end" && end=$((ret+1)); do
[[ ${_ble_edit_str:end-1} =~ $rex ]] || continue
((end+=${#BASH_REMATCH}-1))
if [[ ${BASH_REMATCH::2} == '</' ]]; then
((--count==0)) && return 0
else
((++count))
fi
done
return 1
}
function ble/keymap:vi/text-object/tag.impl {
local arg=$1 flag=$2 reg=$3 type=$4
local ret rex
local pivot=$_ble_edit_ind ret=$_ble_edit_ind
if [[ ${_ble_edit_str:ret:1} == '<' ]] || ble/string#last-index-of-chars "${_ble_edit_str::_ble_edit_ind}" '<>'; then
if rex='^<[^/][^>]*>' && [[ ${_ble_edit_str:ret} =~ $rex ]]; then
((pivot=ret+${#BASH_REMATCH}))
else
((pivot=ret+1))
fi
fi
local ifs=$_ble_term_IFS
local beg=$pivot count=$arg
rex="<([^$ifs/>!]+([$ifs]+([^>]*[^/])?)?|/[^>]*)>\$"
while ble/string#last-index-of-chars "${_ble_edit_str::beg}" '>' && beg=$ret; do
[[ ${_ble_edit_str::beg+1} =~ $rex ]] || continue
((beg-=${#BASH_REMATCH}-1))
if [[ ${BASH_REMATCH::2} == '</' ]]; then
((++count))
else
if ((--count==0)); then
if ble/keymap:vi/text-object:tag/.find-end-tag "$beg" && ((_ble_edit_ind<end)); then
break
else
((count++))
fi
fi
fi
done
if ((count)); then
ble/widget/vi-command/bell
return 1
fi
if [[ $type == i* ]]; then
rex='^<[^>]*>'; [[ ${_ble_edit_str:beg:end-beg} =~ $rex ]] && ((beg+=${#BASH_REMATCH}))
rex='<[^>]*>$'; [[ ${_ble_edit_str:beg:end-beg} =~ $rex ]] && ((end-=${#BASH_REMATCH}))
fi
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
_ble_edit_mark=$beg
ble/widget/vi-command/exclusive-goto.impl "$end"
else
ble/widget/vi-command/exclusive-range.impl "$beg" "$end" "$flag" "$reg"
fi
}
## @fn ble/keymap:vi/text-object:sentence/.beg
## @var[out] beg
## @var[out] is_interval
## @var[in] lf, ht
function ble/keymap:vi/text-object:sentence/.beg {
beg= is_interval=
local pivot=$_ble_edit_ind rex=
if ble-edit/content/bolp && ble-edit/content/eolp; then
if rex=$'^\n+[^\n]'; [[ ${_ble_edit_str:pivot} =~ $rex ]]; then
# 前方に非空白が見つかればその手前の行を開始点とする
beg=$((pivot+${#BASH_REMATCH}-2))
else
# 前の非空行末を基点に取り直す
if rex=$'\n+$'; [[ ${_ble_edit_str::pivot} =~ $rex ]]; then
((pivot-=${#BASH_REMATCH}))
fi
fi
fi
if [[ ! $beg ]]; then
rex="^.*((^$lf?|$lf$lf)([ $ht]*)|[.!?][])'\"]*([ $ht$lf]+))"
if [[ ${_ble_edit_str::pivot+1} =~ $rex ]]; then
beg=${#BASH_REMATCH}
if ((pivot<beg)); then
# pivot < beg は beg == pivot + 1 (終端まで一致) を意味する。
# この時点で pivot は必ず非空行または先頭行にいるので /\n\n/ に一致することはない。
local rematch34=${BASH_REMATCH[3]}${BASH_REMATCH[4]}
if [[ $rematch34 ]]; then
# /(^\n\s+|\n\n\s+|[.!?]\s+)$/
beg=$((pivot+1-${#rematch34})) is_interval=1
else
# /^\n$/
beg=$pivot
fi
fi
else
beg=0
fi
fi
}
## @fn ble/keymap:vi/text-object:sentence/.next {
## @var[in,out] end
## @var[in,out] is_interval
## @var[in] lf, ht
function ble/keymap:vi/text-object:sentence/.next {
if [[ $is_interval ]]; then
is_interval=
local rex=$'[ \t]*((\n[ \t]+)*\n[ \t]*)?'
[[ ${_ble_edit_str:end} =~ $rex ]]
local index=$((end+${#BASH_REMATCH}))
((end<index)) && [[ ${_ble_edit_str:index-1:1} == $'\n' ]] && ((index--))
((end=index))
else
is_interval=1
if local rex=$'^\n+'; [[ ${_ble_edit_str:end} =~ $rex ]]; then
# 連続する LF を読み切る
((end+=${#BASH_REMATCH}))
elif rex="(([.!?][])\"']*)[ $ht$lf]|$lf$lf).*\$"; [[ ${_ble_edit_str:end} =~ $rex ]]; then
# 文を次の文末記号まで
local rematch2=${BASH_REMATCH[2]}
end=$((${#_ble_edit_str}-${#BASH_REMATCH}+${#rematch2}))
else
# 最後の文
local index=${#_ble_edit_str}
((end<index)) && [[ ${_ble_edit_str:index-1:1} == $'\n' ]] && ((index--))
((end=index))
fi
fi
}
function ble/keymap:vi/text-object/sentence.impl {
local arg=$1 flag=$2 reg=$3 type=$4
local lf=$'\n' ht=$'\t'
local rex
local beg is_interval
ble/keymap:vi/text-object:sentence/.beg
local end=$beg i n=$arg
[[ $type != i* ]] && ((n*=2))
for ((i=0;i<n;i++)); do
ble/keymap:vi/text-object:sentence/.next
done
((beg<end)) && [[ ${_ble_edit_str:end-1:1} == $'\n' ]] && ((end--))
# at は後方 (forward) に空白を確保できなければ前方 (backward) に空白を確保する。
if [[ $type != i* && ! $is_interval ]]; then
local ifs=$_ble_term_IFS
if ((end)) && [[ ${_ble_edit_str:end-1:1} != ["$ifs"] ]]; then
rex="^.*(^$lf$lf|[.!?][])'\"]*([ $ht$lf]))([ $ht$lf]*)\$"
if [[ ${_ble_edit_str::beg} =~ $rex ]]; then
local rematch2=${BASH_REMATCH[2]}
local rematch3=${BASH_REMATCH[3]}
((beg-=${#rematch2}+${#rematch3}))
[[ ${_ble_edit_str:beg:1} == $'\n' ]] && ((beg++))
fi
fi
fi
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
_ble_edit_mark=$beg
ble/widget/vi-command/exclusive-goto.impl "$end"
elif ble-edit/content/bolp "$beg" && [[ ${_ble_edit_str:end:1} == $'\n' ]]; then
# 行頭から LF の手前までのときに linewise になる。
# _ble_edit_str の末端までのときは linewise ではないことに注意する。
ble/widget/vi-command/linewise-range.impl "$beg" "$end" "$flag" "$reg" goto_bol
else
ble/widget/vi-command/exclusive-range.impl "$beg" "$end" "$flag" "$reg"
fi
}
function ble/keymap:vi/text-object/paragraph.impl {
local arg=$1 flag=$2 reg=$3 type=$4
local rex ret
local beg= empty_start=
ble-edit/content/find-logical-bol; local bol=$ret
ble-edit/content/find-non-space "$bol"; local nol=$ret
if rex=$'[ \t]*(\n|$)' ble-edit/content/eolp "$nol"; then
# 空行のときは連続する一番初めの空行に移動する
empty_start=1
rex=$'(^|\n)([ \t]*\n)*$'
[[ ${_ble_edit_str::bol} =~ $rex ]]
local rematch1=${BASH_REMATCH[1]} # Note: for bash-3.1 ${#arr[n]} bug
((beg=bol-(${#BASH_REMATCH}-${#rematch1})))
else
# 非空行のときは最初の非空行の先頭まで移動する。
if rex=$'^(.*\n)?[ \t]*\n'; [[ ${_ble_edit_str::bol} =~ $rex ]]; then
((beg=${#BASH_REMATCH}))
else
((beg=0))
fi
fi
local end=$beg
local rex_empty_line=$'([ \t]*\n|[ \t]+$)' rex_paragraph_line=$'([ \t]*[^ \t\n][^\n]*(\n|$))'
if [[ $type == i* ]]; then
rex="$rex_empty_line+|$rex_paragraph_line+"
elif [[ $empty_start ]]; then
rex="$rex_empty_line*$rex_paragraph_line+"
else
rex="$rex_paragraph_line+$rex_empty_line*"
fi
local i
for ((i=0;i<arg;i++)); do
if [[ ${_ble_edit_str:end} =~ $rex ]]; then
((end+=${#BASH_REMATCH}))
else
# paragraph の場合は次が見つからない場合はエラー
ble/widget/vi-command/bell
return 1
fi
done
# at で後続の空行がなければ backward の空行を取り入れる
if [[ $type != i* && ! $empty_start ]]; then
if rex=$'(^|\n)[ \t]*\n$'; ! [[ ${_ble_edit_str::end} =~ $rex ]]; then
if rex=$'(^|\n)([ \t]*\n)*$'; [[ ${_ble_edit_str::beg} =~ $rex ]]; then
local rematch1=${BASH_REMATCH[1]}
((beg-=${#BASH_REMATCH}-${#rematch1}))
fi
fi
fi
((beg<end)) && [[ ${_ble_edit_str:end-1:1} == $'\n' ]] && ((end--))
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
_ble_edit_mark=$beg
ble/widget/vi-command/exclusive-goto.impl "$end"
else
ble/widget/vi-command/linewise-range.impl "$beg" "$end" "$flag" "$reg"
fi
}
## @fn ble/keymap:vi/text-object.impl
##
## @exit テキストオブジェクトの処理が完了したときに 0 を返します。
##
function ble/keymap:vi/text-object.impl {
local arg=$1 flag=$2 reg=$3 type=$4
case $type in
([ia][wW]) ble/keymap:vi/text-object/word.impl "$arg" "$flag" "$reg" "$type" ;;
([ia][\"\'\`]) ble/keymap:vi/text-object/quote.impl "$arg" "$flag" "$reg" "$type" ;;
([ia]['b()']) ble/keymap:vi/text-object/block.impl "$arg" "$flag" "$reg" "${type::1}()" ;;
([ia]['B{}']) ble/keymap:vi/text-object/block.impl "$arg" "$flag" "$reg" "${type::1}{}" ;;
([ia]['<>']) ble/keymap:vi/text-object/block.impl "$arg" "$flag" "$reg" "${type::1}<>" ;;
([ia]['][']) ble/keymap:vi/text-object/block.impl "$arg" "$flag" "$reg" "${type::1}[]" ;;
([ia]t) ble/keymap:vi/text-object/tag.impl "$arg" "$flag" "$reg" "$type" ;;
([ia]s) ble/keymap:vi/text-object/sentence.impl "$arg" "$flag" "$reg" "$type" ;;
([ia]p) ble/keymap:vi/text-object/paragraph.impl "$arg" "$flag" "$reg" "$type" ;;
(*)
ble/widget/vi-command/bell
return 1;;
esac
}
function ble/keymap:vi/text-object.hook {
local key=$1
local ARG FLAG REG; ble/keymap:vi/get-arg 1
if ! ble-decode-key/ischar "$key"; then
ble/widget/vi-command/bell
return 1
fi
local ret; ble/util/c2s "$key"
local type=$_ble_keymap_vi_text_object$ret
ble/keymap:vi/text-object.impl "$ARG" "$FLAG" "$REG" "$type"
return 0
}
function ble/keymap:vi/.attempt-text-object {
local c=${1:-}
if [[ ! $c ]]; then
# If the type is not specified, it is determined from the last key the user
# input (i or a). This is for the backward compatibility.
local n=${#KEYS[@]}; ((n&&n--))
ble-decode-key/ischar "${KEYS[n]}" || return 1
local ret; ble/util/c2s "${KEYS[n]}"; c=$ret
fi
[[ $c == [ia] ]] || return 1
[[ $_ble_keymap_vi_opfunc || $_ble_decode_keymap == vi_[xs]map ]] || return 1
_ble_keymap_vi_text_object=$c
_ble_decode_key__hook=ble/keymap:vi/text-object.hook
return 0
}
function ble/widget/vi-command/text-object {
ble/keymap:vi/.attempt-text-object "$@" && return 147
ble/widget/vi-command/bell
return 1
}
function ble/widget/vi-command/text-object-outer {
ble/widget/vi-command/text-object a
}
function ble/widget/vi-command/text-object-inner {
ble/widget/vi-command/text-object i
}
#------------------------------------------------------------------------------
# Command
#
# map: :cmd
# 既定の cmap 履歴
_ble_keymap_vi_commandline_history=()
_ble_keymap_vi_commandline_history_edit=()
_ble_keymap_vi_commandline_history_dirt=()
_ble_keymap_vi_commandline_history_index=0
function ble/keymap:vi/commandline/empty-cancel.hook {
if [[ ! $_ble_edit_str ]] && ((_ble_edit_async_read_is_cancel_key[KEYS[0]])); then
ble/widget/vi_cmap/cancel
ble/decode/widget/suppress-widget
fi
}
function ble/widget/vi-command/commandline {
ble/keymap:vi/clear-arg
ble/keymap:vi/async-commandline-mode ble/widget/vi-command/commandline.hook
_ble_edit_PS1=:
ble/history/set-prefix _ble_keymap_vi_commandline
_ble_keymap_vi_cmap_before_widget=ble/keymap:vi/commandline/empty-cancel.hook
return 147
}
function ble/widget/vi-command/commandline.hook {
local command
ble/string#split-words command "$1"
local cmd=ble/widget/vi-command:"${command[0]}"
if ble/is-function "$cmd"; then
"$cmd" "${command[@]:1}"; local ext=$?
elif ble/util/compgen cmd -A function -- "ble/widget/vi-command:${command[@]}" && ((${#cmd[@]}==1)); then
"$cmd" "${command[@]:1}"; local ext=$?
else
ble/widget/vi-command/bell "unknown command $1"; local ext=1
fi
[[ $1 ]] && _ble_keymap_vi_register[58]=/$result # ":
return "$ext"
}
function ble/widget/vi-command:w {
local file=
if [[ $1 ]]; then
ble/builtin/history -a "$1"
file=$1
elif [[ ${HISTFILE-} ]]; then
ble/builtin/history -a
file=$HISTFILE
else
ble/widget/vi-command/bell 'w: the history filename is empty or not specified'
return 1
fi
local wc
ble/util/assign-words wc 'ble/bin/wc "$file"'
ble/edit/info/show text "\"$file\" ${wc[0]}L, ${wc[2]}C written"
ble/keymap:vi/adjust-command-mode
return 0
}
function ble/widget/vi-command:q! {
ble/widget/exit force
return 1
}
function ble/widget/vi-command:q {
ble/widget/exit
ble/keymap:vi/adjust-command-mode # ジョブがあるときは終了しないので。
return 1
}
function ble/widget/vi-command:wq {
ble/widget/vi-command:w "$@"
ble/widget/exit
ble/keymap:vi/adjust-command-mode
return 1
}
#------------------------------------------------------------------------------
# Search
#
# map: / ? n N
_ble_keymap_vi_search_obackward=
_ble_keymap_vi_search_ohistory=
_ble_keymap_vi_search_needle=
_ble_keymap_vi_search_activate=
_ble_keymap_vi_search_matched=
_ble_keymap_vi_search_history=()
_ble_keymap_vi_search_history_edit=()
_ble_keymap_vi_search_history_dirt=()
_ble_keymap_vi_search_history_index=0
## @bleopt keymap_vi_search_match_current
## 非空の文字列が設定されている時 /, ?, n, N で
## 現在のカーソルの下にある単語に一致します。
## 既定値は空文字列で vim の振る舞いに倣います。
bleopt/declare -v keymap_vi_search_match_current ''
function ble/highlight/layer:region/mark:vi_search/get-selection {
ble/highlight/layer:region/mark:vi_char/get-selection
}
function ble/keymap:vi/search/matched {
[[ $_ble_keymap_vi_search_matched || $_ble_edit_mark_active == vi_search || $_ble_keymap_vi_search_activate ]]
}
function ble/keymap:vi/search/clear-matched {
_ble_keymap_vi_search_activate=
_ble_keymap_vi_search_matched=
[[ $_ble_edit_mark_active == vi_search ]] && _ble_edit_mark_active=
}
## @fn ble/keymap:vi/search/invoke-search needle
##
## @param[in] needle
## 検索パターンを表す正規表現を指定する。
##
## @var[out] beg end
## 一致範囲を返す。
##
## @exit
## 一致が見つかった場合に正常終了 0。それ以外の時は 0 以外の値を返す。
##
## @var[in] opt_optional_next
## 現在位置より後または前の一致を検索する。
## vi_omap で呼び出した時、現在位置で一致した時に設定される。
## keymap_vi_search_match_current が非空の時はここには来ない。
##
## @var[in] opt_locate
## 現在位置に一致可能な検索を行う。
## 履歴を遡って検索して一致する履歴項目が見つかった時、
## その履歴項目内で最初に見つかったものを特定する為に使われる。
##
## @var[in] opt_backward
## 検索方向を指定する。
##
function ble/keymap:vi/search/invoke-search {
local needle=$1
local dir=+; ((opt_backward)) && dir=B
local ind=$_ble_edit_ind
# 検索開始位置
if ((opt_optional_next)); then
if ((!opt_backward)); then
((_ble_edit_ind<${#_ble_edit_str}&&_ble_edit_ind++))
fi
elif ((opt_locate)) || ! ble/keymap:vi/search/matched; then
# 何にも一致していない状態から
if ((opt_locate)) || [[ $bleopt_keymap_vi_search_match_current ]]; then
# 現在位置に一致可能
# 前方検索: @hello → @hello (そのまま)
# 後方検索: hell@o → hello@ (ずらす)
if ((opt_backward)); then
ble-edit/content/eolp || ((_ble_edit_ind++))
fi
else
# 現在位置には一致させない
# 前方検索: @hello → h@ello (ずらす)
# 後方検索: hell@o → hell@o (そのまま)
if ((!opt_backward)); then
ble-edit/content/eolp || ((_ble_edit_ind++))
fi
fi
else
# _ble_edit_ind .. _ble_edit_mark[+1] に一致しているとき
if ((!opt_backward)); then
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
# vi_xmap, vi_smap では _ble_edit_mark は別の用途に使われていて
# 終端点の情報が失われているので再度一致を試みる。
if ble-edit/isearch/search "$@" && ((beg==_ble_edit_ind)); then
_ble_edit_ind=$end
else
((_ble_edit_ind<${#_ble_edit_str}&&_ble_edit_ind++))
fi
else
((_ble_edit_ind=_ble_edit_mark))
ble-edit/content/eolp || ((_ble_edit_ind++))
fi
else
# 2回目以降の一致では opts=- で検索する。
dir=-
fi
fi
ble-edit/isearch/search "$needle" "$dir":regex; local ext=$?
_ble_edit_ind=$ind
return "$ext"
}
## @fn ble/widget/vi-command/search.core
##
## @var[in] needle
## @var[in] opt_backward
## @var[in] opt_history
## @var[in] opt_locate
## @var[in] start
## @var[in] ntask
##
function ble/widget/vi-command/search.core {
local beg= end= is_empty_match=
if ble/keymap:vi/search/invoke-search "$needle"; then
if ((beg<end)); then
ble-edit/content/bolp "$end" || ((end--))
_ble_edit_ind=$beg # eol 補正は search.impl 側で最後に行う
[[ $_ble_decode_keymap != vi_[xs]map ]] && _ble_edit_mark=$end
_ble_keymap_vi_search_activate=vi_search
return 0
else
# vim では空一致は即座に失敗のようだ。
# 続きを検索するということはしない。
opt_history=
is_empty_match=1
fi
fi
if ((opt_history)) && [[ $_ble_history_load_done || opt_backward -ne 0 ]]; then
ble/history/initialize
local index; ble/history/get-index
[[ $start ]] || start=$index
if ((opt_backward)); then
((index--))
else
((index++))
fi
local _ble_edit_isearch_dir=+; ((opt_backward)) && _ble_edit_isearch_dir=-
local _ble_edit_isearch_str=$needle
local isearch_ntask=$ntask
local isearch_time=0
local isearch_progress_callback=ble-edit/isearch/.show-status-with-progress.fib
if ((opt_backward)); then
ble/history/isearch-backward-blockwise regex:progress
else
ble/history/isearch-forward regex:progress
fi; local r=$?
ble/edit/info/default
if ((r==0)); then
local new_index; ble/history/get-index -v new_index
[[ $index != "$new_index" ]] &&
ble-edit/history/goto "$index" point=none
if ((opt_backward)); then
local i=${#_ble_edit_str}
ble/keymap:vi/needs-eol-fix "$i" && ((i--))
_ble_edit_ind=$i
else
_ble_edit_ind=0
fi
opt_locate=1 opt_history=0 ble/widget/vi-command/search.core
return "$?"
fi
fi
if ((!opt_optional_next)); then
if [[ $is_empty_match ]]; then
ble/widget/.bell "search: empty match"
else
ble/widget/.bell "search: not found"
fi
if [[ $_ble_edit_mark_active == vi_search ]]; then
_ble_keymap_vi_search_activate=vi_search
fi
fi
return 1
}
function ble/widget/vi-command/search.impl {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local opts=$1 needle=$2
[[ :$opts: != *:repeat:* ]]; local opt_repeat=$? # 再検索 n N
[[ :$opts: != *:history:* ]]; local opt_history=$? # 履歴検索が有効か
[[ :$opts: != *:-:* ]]; local opt_backward=$? # 逆方向
local opt_locate=0
local opt_optional_next=0
if ((opt_repeat)); then
# n N
if [[ $_ble_keymap_vi_search_needle ]]; then
needle=$_ble_keymap_vi_search_needle
((opt_backward^=_ble_keymap_vi_search_obackward,
opt_history=_ble_keymap_vi_search_ohistory))
else
ble/widget/vi-command/bell 'no previous search'
return 1
fi
else
# / ? * #
ble/keymap:vi/search/clear-matched
if [[ $needle ]]; then
_ble_keymap_vi_search_needle=$needle
_ble_keymap_vi_search_obackward=$opt_backward
_ble_keymap_vi_search_ohistory=$opt_history
elif [[ $_ble_keymap_vi_search_needle ]]; then
needle=$_ble_keymap_vi_search_needle
_ble_keymap_vi_search_obackward=$opt_backward
_ble_keymap_vi_search_ohistory=$opt_history
else
ble/widget/vi-command/bell 'no previous search'
return 1
fi
fi
local original_ind=$_ble_edit_ind
if [[ $FLAG || $_ble_decode_keymap == vi_[xs]map ]]; then
opt_history=0
else
local old_hindex; ble/history/get-index -v old_hindex
fi
local start= # 初めの履歴番号。search.core 内で最初に履歴を読み込んだあとで設定される。
local ntask=$ARG
while ((ntask)); do
ble/widget/vi-command/search.core || break
((ntask--))
done
if [[ $FLAG ]]; then
if ((ntask)); then
# 検索対象が見つからなかったとき
_ble_keymap_vi_search_activate=
_ble_edit_ind=$original_ind
ble/keymap:vi/adjust-command-mode
return 1
else
# 見つかったとき
if ((_ble_edit_ind==original_index)); then
# 範囲が空のときは次の一致場所まで。
# 次の一致場所がないとき (自分自身のとき) は空領域になる。
opt_optional_next=1 ble/widget/vi-command/search.core
fi
local index=$_ble_edit_ind
_ble_keymap_vi_search_activate=
_ble_edit_ind=$original_ind
ble/widget/vi-command/exclusive-goto.impl "$index" "$FLAG" "$REG" nobell
fi
else
if ((ntask<ARG)); then
# 同じ履歴項目内でのジャンプ
if ((opt_history)); then
local new_hindex; ble/history/get-index -v new_hindex
((new_hindex==old_hindex))
fi && ble/keymap:vi/mark/set-local-mark 96 "$original_index" # ``
# 行末補正
if ble/keymap:vi/needs-eol-fix; then
if ((!opt_backward&&_ble_edit_ind<_ble_edit_mark)); then
((_ble_edit_ind++))
else
((_ble_edit_ind--))
fi
fi
fi
ble/keymap:vi/adjust-command-mode
return 0
fi
}
function ble/widget/vi-command/search-forward {
ble/keymap:vi/async-commandline-mode 'ble/widget/vi-command/search.impl +:history'
_ble_edit_PS1='/'
ble/history/set-prefix _ble_keymap_vi_search
_ble_keymap_vi_cmap_before_widget=ble/keymap:vi/commandline/empty-cancel.hook
return 147
}
function ble/widget/vi-command/search-backward {
ble/keymap:vi/async-commandline-mode 'ble/widget/vi-command/search.impl -:history'
_ble_edit_PS1='?'
ble/history/set-prefix _ble_keymap_vi_search
_ble_keymap_vi_cmap_before_widget=ble/keymap:vi/commandline/empty-cancel.hook
return 147
}
function ble/widget/vi-command/search-repeat {
ble/widget/vi-command/search.impl repeat:+
}
function ble/widget/vi-command/search-reverse-repeat {
ble/widget/vi-command/search.impl repeat:-
}
function ble/widget/vi-command/search-word.impl {
local opts=$1
local rex=$'^([^[:alnum:]_\n]*)([[:alnum:]_]*)'
if ! [[ ${_ble_edit_str:_ble_edit_ind} =~ $rex ]]; then
ble/keymap:vi/clear-arg
ble/widget/vi-command/bell 'word is not found'
return 1
fi
local end=$((_ble_edit_ind+${#BASH_REMATCH}))
local word=${BASH_REMATCH[2]}
if [[ ! ${BASH_REMATCH[1]} ]]; then
rex=$'[[:alnum:]_]+$'
[[ ${_ble_edit_str::_ble_edit_ind} =~ $rex ]] &&
word=$BASH_REMATCH$word
fi
# Note: Bash 正規表現は <regex.h> を用いるので、
# 必ずしも非 POSIX ERE \<\> に対応しているとは限らない。
# また [[:alnum:]_] と符合しているかも分からない。
# 従って適用できるか確認してから境界に一致することを要求する。
local needle=$word
rex='\<'$needle; [[ $word =~ $rex ]] && needle=$rex
rex=$needle'\>'; [[ $word =~ $rex ]] && needle=$rex
if [[ $opts == backward ]]; then
ble/widget/vi-command/search.impl -:history "$needle"
else
local original_ind=$_ble_edit_ind
_ble_edit_ind=$((end-1))
ble/widget/vi-command/search.impl +:history "$needle" && return 0
_ble_edit_ind=$original_ind
return 1
fi
}
# nmap *
function ble/widget/vi-command/search-word-forward {
ble/widget/vi-command/search-word.impl forward
}
# nmap #
function ble/widget/vi-command/search-word-backward {
ble/widget/vi-command/search-word.impl backward
}
#------------------------------------------------------------------------------
# command-help
# nmap K
function ble/widget/vi_nmap/command-help {
ble/keymap:vi/clear-arg
ble/widget/command-help; local ext=$?
ble/keymap:vi/adjust-command-mode
return "$ext"
}
function ble/widget/vi_xmap/command-help.core {
ble/keymap:vi/clear-arg
local get_selection=ble/highlight/layer:region/mark:$_ble_edit_mark_active/get-selection
ble/is-function "$get_selection" || return 1
local selection
"$get_selection" || return 1
((${#selection[*]}==2)) || return 1
local comp_cword=0 comp_line=$_ble_edit_str comp_point=$_ble_edit_ind
local -a comp_words; comp_words=("$cmd")
local cmd=${_ble_edit_str:selection[0]:selection[1]-selection[0]}
ble/widget/command-help.impl "$cmd"; local ext=$?
ble/keymap:vi/adjust-command-mode
return "$ext"
}
function ble/widget/vi_xmap/command-help {
if ! ble/widget/vi_xmap/command-help.core; then
ble/widget/vi-command/bell
return 1
fi
}
#------------------------------------------------------------------------------
## @fn ble/keymap:vi/set-up-command-map
## @var[in] ble_bind_keymap
function ble/keymap:vi/set-up-command-map {
ble-bind -f 0 vi-command/append-arg
ble-bind -f 1 vi-command/append-arg
ble-bind -f 2 vi-command/append-arg
ble-bind -f 3 vi-command/append-arg
ble-bind -f 4 vi-command/append-arg
ble-bind -f 5 vi-command/append-arg
ble-bind -f 6 vi-command/append-arg
ble-bind -f 7 vi-command/append-arg
ble-bind -f 8 vi-command/append-arg
ble-bind -f 9 vi-command/append-arg
ble-bind -f y 'vi-command/operator y'
ble-bind -f d 'vi-command/operator d'
ble-bind -f c 'vi-command/operator c'
ble-bind -f '<' 'vi-command/operator indent-left'
ble-bind -f '>' 'vi-command/operator indent-right'
ble-bind -f '!' 'vi-command/operator filter'
ble-bind -f 'g ~' 'vi-command/operator toggle_case'
ble-bind -f 'g u' 'vi-command/operator u'
ble-bind -f 'g U' 'vi-command/operator U'
ble-bind -f 'g ?' 'vi-command/operator rot13'
ble-bind -f 'g q' 'vi-command/operator fold'
ble-bind -f 'g w' 'vi-command/operator fold-preserve-point'
ble-bind -f 'g @' 'vi-command/operator map'
# ble-bind -f '=' 'vi-command/operator =' # インデント (equalprg, ep)
# ble-bind -f 'z f' 'vi-command/operator f'
ble-bind -f paste_begin vi-command/bracketed-paste
ble-bind -f 'home' vi-command/beginning-of-line
ble-bind -f '$' vi-command/forward-eol
ble-bind -f 'end' vi-command/forward-eol
ble-bind -f '^' vi-command/first-non-space
ble-bind -f '_' vi-command/first-non-space-forward
ble-bind -f '+' vi-command/forward-first-non-space
ble-bind -f 'C-m' vi-command/forward-first-non-space
ble-bind -f 'RET' vi-command/forward-first-non-space
ble-bind -f '-' vi-command/backward-first-non-space
ble-bind -f 'g 0' vi-command/beginning-of-graphical-line
ble-bind -f 'g home' vi-command/beginning-of-graphical-line
ble-bind -f 'g ^' vi-command/graphical-first-non-space
ble-bind -f 'g $' vi-command/graphical-forward-eol
ble-bind -f 'g end' vi-command/graphical-forward-eol
ble-bind -f 'g m' vi-command/middle-of-graphical-line
ble-bind -f 'g _' vi-command/last-non-space
ble-bind -f h vi-command/backward-char
ble-bind -f l vi-command/forward-char
ble-bind -f left vi-command/backward-char
ble-bind -f right vi-command/forward-char
ble-bind -f 'C-?' 'vi-command/backward-char wrap'
ble-bind -f 'DEL' 'vi-command/backward-char wrap'
ble-bind -f 'C-h' 'vi-command/backward-char wrap'
ble-bind -f 'BS' 'vi-command/backward-char wrap'
ble-bind -f SP 'vi-command/forward-char wrap'
ble-bind -f j vi-command/forward-line
ble-bind -f down vi-command/forward-line
ble-bind -f C-n vi-command/forward-line
ble-bind -f C-j vi-command/forward-line
ble-bind -f k vi-command/backward-line
ble-bind -f up vi-command/backward-line
ble-bind -f C-p vi-command/backward-line
ble-bind -f 'g j' vi-command/graphical-forward-line
ble-bind -f 'g down' vi-command/graphical-forward-line
ble-bind -f 'g k' vi-command/graphical-backward-line
ble-bind -f 'g up' vi-command/graphical-backward-line
ble-bind -f w vi-command/forward-vword
ble-bind -f W vi-command/forward-uword
ble-bind -f b vi-command/backward-vword
ble-bind -f B vi-command/backward-uword
ble-bind -f e vi-command/forward-vword-end
ble-bind -f E vi-command/forward-uword-end
ble-bind -f 'g e' vi-command/backward-vword-end
ble-bind -f 'g E' vi-command/backward-uword-end
ble-bind -f C-right vi-command/forward-vword
ble-bind -f S-right vi-command/forward-vword
ble-bind -f C-left vi-command/backward-vword
ble-bind -f S-left vi-command/backward-vword
ble-bind -f 'g o' vi-command/nth-byte
ble-bind -f '|' vi-command/nth-column
ble-bind -f H vi-command/nth-line
ble-bind -f L vi-command/nth-last-line
ble-bind -f 'g g' vi-command/history-beginning
ble-bind -f G vi-command/history-end
ble-bind -f C-home vi-command/first-nol
ble-bind -f C-end vi-command/last-eol
ble-bind -f 'f' vi-command/search-forward-char
ble-bind -f 'F' vi-command/search-backward-char
ble-bind -f 't' vi-command/search-forward-char-prev
ble-bind -f 'T' vi-command/search-backward-char-prev
ble-bind -f ';' vi-command/search-char-repeat
ble-bind -f ',' vi-command/search-char-reverse-repeat
ble-bind -f '%' 'vi-command/search-matchpair-or vi-command/percentage-line'
ble-bind -f 'C-\ C-n' nop
ble-bind -f ':' vi-command/commandline
ble-bind -f '/' vi-command/search-forward
ble-bind -f '?' vi-command/search-backward
ble-bind -f 'n' vi-command/search-repeat
ble-bind -f 'N' vi-command/search-reverse-repeat
ble-bind -f '*' vi-command/search-word-forward
ble-bind -f '#' vi-command/search-word-backward
ble-bind -f '`' 'vi-command/goto-mark'
ble-bind -f \' 'vi-command/goto-mark line'
# bash
ble-bind -c 'C-z' fg
}
#------------------------------------------------------------------------------
# Operator pending mode
function ble/widget/vi_omap/operator-rot13-or-search-backward {
if [[ $_ble_keymap_vi_opfunc == rot13 ]]; then
# g?? の時だけは rot13-encode lines
ble/widget/vi-command/operator rot13
else
ble/widget/vi-command/search-backward
fi
}
# o_v o_V
function ble/widget/vi_omap/switch-visual-mode.impl {
local new_mode=$1
local old=$_ble_keymap_vi_opfunc
[[ $old ]] || return 1
# clear existing visual-mode
local new=$old:
new=${new/:vi_char:/:}
new=${new/:vi_line:/:}
new=${new/:vi_block:/:}
# add new visual-mode
[[ $new_mode ]] && new=$new:$new_mode
_ble_keymap_vi_opfunc=$new
}
# omap v
function ble/widget/vi_omap/switch-to-charwise {
ble/widget/vi_omap/switch-visual-mode.impl vi_char
}
# omap V
function ble/widget/vi_omap/switch-to-linewise {
ble/widget/vi_omap/switch-visual-mode.impl vi_line
}
# omap <C-v>
function ble/widget/vi_omap/switch-to-blockwise {
ble/widget/vi_omap/switch-visual-mode.impl vi_block
}
function ble-decode/keymap:vi_omap/define {
ble/keymap:vi/set-up-command-map
ble-bind -f __default__ vi_omap/__default__
ble-bind -f __line_limit__ nop
ble-bind -f 'ESC' vi_omap/cancel
ble-bind -f 'C-[' vi_omap/cancel
ble-bind -f 'C-c' vi_omap/cancel
ble-bind -f a vi-command/text-object-outer
ble-bind -f i vi-command/text-object-inner
# 範囲の種類の変更 (vim o_v o_V)
ble-bind -f v vi_omap/switch-to-charwise
ble-bind -f V vi_omap/switch-to-linewise
ble-bind -f C-v vi_omap/switch-to-blockwise
ble-bind -f C-q vi_omap/switch-to-blockwise
# 2文字オペレータの短縮形
ble-bind -f '~' 'vi-command/operator toggle_case'
ble-bind -f 'u' 'vi-command/operator u'
ble-bind -f 'U' 'vi-command/operator U'
ble-bind -f '?' 'vi_omap/operator-rot13-or-search-backward'
ble-bind -f 'q' 'vi-command/operator fold'
# Note: w は前方単語。例: {N}gww は format {N} words
# Note: @ は omap では定義されない。例: {N}g@@ は bell
}
#------------------------------------------------------------------------------
# Normal mode
# nmap C-d
function ble/widget/vi-command/exit-on-empty-line {
if [[ $_ble_edit_str ]]; then
ble/widget/vi_nmap/forward-scroll
return "$?"
else
ble/widget/exit
ble/keymap:vi/adjust-command-mode # ジョブがあるときは終了しないので。
return 1
fi
}
# nmap C-g (show line and column)
function ble/widget/vi-command/show-line-info {
local index count
ble/history/get-index -v index
ble/history/get-count -v count
local hist_ratio=$(((100*index+count-1)/count))%
local hist_stat=$'!\e[32m'$index$'\e[m / \e[32m'$count$'\e[m (\e[32m'$hist_ratio$'\e[m)'
local ret
ble/string#count-char "$_ble_edit_str" $'\n'; local nline=$((ret+1))
ble/string#count-char "${_ble_edit_str::_ble_edit_ind}" $'\n'; local iline=$((ret+1))
local line_ratio=$(((100*iline+nline-1)/nline))%
local line_stat=$'line \e[34m'$iline$'\e[m / \e[34m'$nline$'\e[m --\e[34m'$line_ratio$'\e[m--'
ble/edit/info/show ansi "\"$hist_stat\" $line_stat"
ble/keymap:vi/adjust-command-mode
return 0
}
# nmap C-c (jobs)
function ble/widget/vi-command/cancel {
if [[ $_ble_keymap_vi_single_command ]]; then
_ble_keymap_vi_single_command=
_ble_keymap_vi_single_command_overwrite=
ble/keymap:vi/update-mode-indicator
else
local joblist; ble/util/joblist
if ((${#joblist[*]})); then
ble/array#push joblist $'Type \e[35m:q!\e[m and press \e[35m<Enter>\e[m to abandon all \e[31mjobs\e[m and exit Bash'
IFS=$'\n' builtin eval 'ble/edit/info/show ansi "${joblist[*]}"'
else
ble/edit/info/show ansi $'Type \e[35m:q\e[m and press \e[35m<Enter>\e[m to exit Bash'
fi
fi
ble/widget/vi-command/bell
return 0
}
# nmap u, U, C-r
#
# `[`] は設定する。vim と違って実際に変更のあった範囲を抽出する。
# . は設定しない。
#
bleopt/declare -v keymap_vi_imap_undo ''
_ble_keymap_vi_undo_suppress=
function ble/keymap:vi/undo/add {
[[ $_ble_keymap_vi_undo_suppress ]] && return 0
[[ $1 == more && $bleopt_keymap_vi_imap_undo != more ]] && return 0
ble-edit/undo/add
}
function ble/widget/vi_nmap/undo {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local _ble_keymap_vi_undo_suppress=1
ble/keymap:vi/mark/start-edit-area
if ble-edit/undo/undo "$ARG"; then
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/adjust-command-mode
else
ble/widget/vi-command/bell
return 1
fi
}
function ble/widget/vi_nmap/redo {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local _ble_keymap_vi_undo_suppress=1
ble/keymap:vi/mark/start-edit-area
if ble-edit/undo/redo "$ARG"; then
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/adjust-command-mode
else
ble/widget/vi-command/bell
return 1
fi
}
function ble/widget/vi_nmap/revert {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local _ble_keymap_vi_undo_suppress=1
ble/keymap:vi/mark/start-edit-area
if ble-edit/undo/revert-toggle "$ARG"; then
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/adjust-command-mode
else
ble/widget/vi-command/bell
return 1
fi
}
# nmap C-a, C-x
function ble/widget/vi_nmap/increment.impl {
local delta=$1
((delta==0)) && return 0
# 数字の範囲の確定
local line=${_ble_edit_str:_ble_edit_ind}
line=${line%%$'\n'*}
local rex='^([^0-9]*)[0-9]+'
if ! [[ $line =~ $rex ]]; then
# 行末にいる時(空行を意味する)にはベルは鳴らさない。
[[ $line ]] && ble/widget/.bell 'number not found'
ble/keymap:vi/adjust-command-mode
return 0
fi
local rematch1=${BASH_REMATCH[1]}
local beg=$((_ble_edit_ind+${#rematch1}))
local end=$((_ble_edit_ind+${#BASH_REMATCH}))
rex='-?[0-9]*$'; [[ ${_ble_edit_str::beg} =~ $rex ]]
((beg-=${#BASH_REMATCH}))
# 数の抽出
local number=${_ble_edit_str:beg:end-beg}
local abs=${number#-}
if [[ $abs == 0?* ]]; then
if [[ $number == -* ]]; then
number=-$((10#0$abs))
else
number=$((10#0$abs))
fi
fi
# 数の増加・減少
((number+=delta))
if [[ $abs == 0?* ]]; then
# Zero padding
local wsign=$((number<0?1:0))
local zpad=$((wsign+${#abs}-${#number}))
if ((zpad>0)); then
local ret; ble/string#repeat 0 "$zpad"
number=${number::wsign}$ret${number:wsign}
fi
fi
ble/widget/.replace-range "$beg" "$end" "$number"
ble/keymap:vi/mark/set-previous-edit-area "$beg" "$((beg+${#number}))"
ble/keymap:vi/repeat/record
_ble_edit_ind=$((beg+${#number}-1))
ble/keymap:vi/adjust-command-mode
return 0
}
function ble/widget/vi_nmap/increment {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi_nmap/increment.impl "$ARG"
}
function ble/widget/vi_nmap/decrement {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi_nmap/increment.impl "$((-ARG))"
}
function ble/widget/vi_nmap/__line_limit__.edit {
ble/keymap:vi/clear-arg
ble/widget/vi_nmap/.insert-mode
ble/keymap:vi/repeat/clear-insert
ble/widget/edit-and-execute-command.impl "$1"
}
function ble/widget/vi_nmap/__line_limit__ {
ble/widget/__line_limit__ vi_nmap/__line_limit__.edit
}
function ble-decode/keymap:vi_nmap/define {
ble/keymap:vi/set-up-command-map
ble-bind -f __default__ vi-command/decompose-meta
ble-bind -f __line_limit__ vi_nmap/__line_limit__
ble-bind -f 'ESC' vi-command/bell
ble-bind -f 'C-[' vi-command/bell
ble-bind -f 'C-c' vi-command/cancel
ble-bind -f a vi_nmap/append-mode
ble-bind -f A vi_nmap/append-mode-at-end-of-line
ble-bind -f i vi_nmap/insert-mode
ble-bind -f insert vi_nmap/insert-mode
ble-bind -f I vi_nmap/insert-mode-at-first-non-space
ble-bind -f 'g I' vi_nmap/insert-mode-at-beginning-of-line
ble-bind -f o vi_nmap/insert-mode-at-forward-line
ble-bind -f O vi_nmap/insert-mode-at-backward-line
ble-bind -f R vi_nmap/replace-mode
ble-bind -f 'g R' vi_nmap/virtual-replace-mode
ble-bind -f 'g i' vi_nmap/insert-mode-at-previous-point
ble-bind -f '~' vi_nmap/forward-char-toggle-case
ble-bind -f Y vi_nmap/copy-current-line
ble-bind -f S vi_nmap/kill-current-line-and-insert
ble-bind -f D vi_nmap/kill-forward-line
ble-bind -f C vi_nmap/kill-forward-line-and-insert
ble-bind -f p vi_nmap/paste-after
ble-bind -f P vi_nmap/paste-before
ble-bind -f x vi_nmap/kill-forward-char
ble-bind -f s vi_nmap/kill-forward-char-and-insert
ble-bind -f X vi_nmap/kill-backward-char
ble-bind -f delete vi_nmap/kill-forward-char
ble-bind -f 'r' vi_nmap/replace-char
ble-bind -f 'g r' vi_nmap/virtual-replace-char # vim で実際に試すとこの機能はない
ble-bind -f J vi_nmap/connect-line-with-space
ble-bind -f 'g J' vi_nmap/connect-line
ble-bind -f v vi_nmap/charwise-visual-mode
ble-bind -f V vi_nmap/linewise-visual-mode
ble-bind -f C-v vi_nmap/blockwise-visual-mode
ble-bind -f C-q vi_nmap/blockwise-visual-mode
ble-bind -f 'g v' vi-command/previous-visual-area
ble-bind -f 'g h' vi_nmap/charwise-select-mode
ble-bind -f 'g H' vi_nmap/linewise-select-mode
ble-bind -f 'g C-h' vi_nmap/blockwise-select-mode
ble-bind -f . vi_nmap/repeat
ble-bind -f K vi_nmap/command-help
ble-bind -f f1 vi_nmap/command-help
ble-bind -f 'C-d' vi_nmap/forward-line-scroll
ble-bind -f 'C-u' vi_nmap/backward-line-scroll
ble-bind -f 'C-e' vi_nmap/forward-scroll
ble-bind -f 'C-y' vi_nmap/backward-scroll
ble-bind -f 'C-f' vi_nmap/pagedown
ble-bind -f 'next' vi_nmap/pagedown
ble-bind -f 'C-b' vi_nmap/pageup
ble-bind -f 'prior' vi_nmap/pageup
ble-bind -f 'z t' vi_nmap/scroll-to-top-and-redraw
ble-bind -f 'z z' vi_nmap/scroll-to-center-and-redraw
ble-bind -f 'z b' vi_nmap/scroll-to-bottom-and-redraw
ble-bind -f 'z RET' vi_nmap/scroll-to-top-non-space-and-redraw
ble-bind -f 'z C-m' vi_nmap/scroll-to-top-non-space-and-redraw
ble-bind -f 'z +' vi_nmap/scroll-or-pagedown-and-redraw
ble-bind -f 'z -' vi_nmap/scroll-to-bottom-non-space-and-redraw
ble-bind -f 'z .' vi_nmap/scroll-to-center-non-space-and-redraw
ble-bind -f m vi-command/set-mark
ble-bind -f '"' vi-command/register
ble-bind -f 'C-g' vi-command/show-line-info
ble-bind -f 'q' vi_nmap/record-register
ble-bind -f '@' vi_nmap/play-register
ble-bind -f u vi_nmap/undo
ble-bind -f C-r vi_nmap/redo
ble-bind -f U vi_nmap/revert
ble-bind -f C-a vi_nmap/increment
ble-bind -f C-x vi_nmap/decrement
ble-bind -f 'Z Z' 'vi-command:q'
ble-bind -f 'Z Q' 'vi-command:q'
#----------------------------------------------------------------------------
# bash
ble-bind -f 'C-j' 'accept-line'
ble-bind -f 'C-RET' 'accept-line'
ble-bind -f 'C-m' 'accept-single-line-or vi-command/forward-first-non-space'
ble-bind -f 'RET' 'accept-single-line-or vi-command/forward-first-non-space'
#ble-bind -f 'C-x C-e' 'vi-command/edit-and-execute-command'
ble-bind -f 'C-l' 'clear-screen'
ble-bind -f 'C-d' 'vi-command/exit-on-empty-line' # overwrites vi_nmap/forward-scroll
ble-bind -f 'ac_enter' 'auto-complete-enter'
# Note #D1256: Bash vi-command 互換性の為
ble-bind -f M-left 'vi-command/backward-vword'
ble-bind -f M-right 'vi-command/forward-vword'
ble-bind -f C-delete 'vi-rlfunc/kill-word'
ble-bind -f '#' 'vi-rlfunc/insert-comment'
ble-bind -f '&' 'vi_nmap/@edit tilde-expand'
# ble-bind -f 'C-u' 'vi-rlfunc/unix-line-discard'
# ble-bind -f 'C-q' 'vi-rlfunc/quoted-insert'
# ble-bind -f 'C-v' 'vi-rlfunc/quoted-insert'
# ble-bind -f 'C-d' 'vi-rlfunc/eof-maybe'
# ble-bind -f '_' 'vi-rlfunc/yank-arg'
}
# lib/core-decode.vi_nmap-rlfunc.txt 用
function ble/widget/vi-rlfunc/.is-uppercase {
local n=${#KEYS[@]}
local code=$((KEYS[n?n-1:0]&_ble_decode_MaskChar))
((0x41<=code&&code<=0x5a))
}
# d or D
function ble/widget/vi-rlfunc/delete-to {
if ble/widget/vi-rlfunc/.is-uppercase; then
ble/widget/vi_nmap/kill-forward-line
else
ble/widget/vi-command/operator d
fi
}
# c or C
function ble/widget/vi-rlfunc/change-to {
if ble/widget/vi-rlfunc/.is-uppercase; then
ble/widget/vi_nmap/kill-forward-line-and-insert
else
ble/widget/vi-command/operator c
fi
}
# y or Y
function ble/widget/vi-rlfunc/yank-to {
if ble/widget/vi-rlfunc/.is-uppercase; then
ble/widget/vi_nmap/copy-current-line
else
ble/widget/vi-command/operator y
fi
}
function ble/widget/vi-rlfunc/char-search {
local n=${#KEYS[@]}
local code=$((KEYS[n?n-1:0]&_ble_decode_MaskChar))
((code==0)) && return 1
ble/util/c2s "$code"
case $ret in
('f') ble/widget/vi-command/search-forward-char ;;
('F') ble/widget/vi-command/search-backward-char ;;
('t') ble/widget/vi-command/search-forward-char-prev ;;
('T') ble/widget/vi-command/search-backward-char-prev ;;
(';') ble/widget/vi-command/search-char-repeat ;;
(',') ble/widget/vi-command/search-char-reverse-repeat ;;
(*) return 1 ;;
esac
}
# w or W
function ble/widget/vi-rlfunc/next-word {
if ble/widget/vi-rlfunc/.is-uppercase; then
ble/widget/vi-command/forward-uword
else
ble/widget/vi-command/forward-vword
fi
}
# b or B
function ble/widget/vi-rlfunc/prev-word {
if ble/widget/vi-rlfunc/.is-uppercase; then
ble/widget/vi-command/backward-uword
else
ble/widget/vi-command/backward-vword
fi
}
# e or E
function ble/widget/vi-rlfunc/end-word {
if ble/widget/vi-rlfunc/.is-uppercase; then
ble/widget/vi-command/forward-uword-end
else
ble/widget/vi-command/forward-vword-end
fi
}
# p or P
function ble/widget/vi-rlfunc/put {
if ble/widget/vi-rlfunc/.is-uppercase; then
ble/widget/vi_nmap/paste-before
else
ble/widget/vi_nmap/paste-after
fi
}
# / or ?
function ble/widget/vi-rlfunc/search {
local n=${#KEYS[@]}
local code=$((KEYS[n?n-1:0]&_ble_decode_MaskChar))
if ((code==63)); then
ble/widget/vi-command/search-backward
else
ble/widget/vi-command/search-forward
fi
}
# n or N
function ble/widget/vi-rlfunc/search-again {
if ble/widget/vi-rlfunc/.is-uppercase; then
ble/widget/vi-command/search-reverse-repeat
else
ble/widget/vi-command/search-repeat
fi
}
# s or S
function ble/widget/vi-rlfunc/subst {
if ble/widget/vi-rlfunc/.is-uppercase; then
ble/widget/vi_nmap/kill-current-line-and-insert
else
ble/widget/vi_nmap/kill-forward-char-and-insert
fi
}
# rl_nmap C-delete
function ble/widget/vi-rlfunc/kill-word {
_ble_keymap_vi_opfunc=d
ble/widget/vi-command/forward-vword-end
}
# rl_nmap -
function ble/widget/vi-rlfunc/backward-delete-char {
_ble_keymap_vi_opfunc=d
_ble_keymap_vi_reg=95 # use black-hole register ("_) to discard the text
ble/widget/vi-command/backward-char
}
function ble/widget/vi-rlfunc/backward-kill-line {
_ble_keymap_vi_opfunc=d
ble/widget/vi-command/beginning-of-line
}
function ble/widget/vi-rlfunc/backward-kill-word {
_ble_keymap_vi_opfunc=d
ble/widget/vi-command/backward-vword
}
function ble/widget/vi-rlfunc/copy-backward-word {
# Note: We use vword instead of cword used in the emacs keymap. This may be
# replaced with a cword implementation later.
_ble_keymap_vi_opfunc=y
ble/widget/vi-command/backward-vword
}
function ble/widget/vi-rlfunc/copy-forward-word {
# Note: We use vword instead of cword used in the emacs keymap. This may be
# replaced with a cword implementation later.
_ble_keymap_vi_opfunc=y
ble/widget/vi-command/forward-vword
}
# rl_nmap C-u
function ble/widget/vi-rlfunc/unix-line-discard {
_ble_keymap_vi_opfunc=d
ble/widget/vi-command/beginning-of-line
}
# rl_nmap #
function ble/widget/vi-rlfunc/insert-comment {
local ARG FLAG REG; ble/keymap:vi/get-arg ''
ble/keymap:vi/mark/start-edit-area
ble/widget/insert-comment/.insert "$ARG"
ble/keymap:vi/mark/end-edit-area
ble/widget/vi_nmap/accept-line
}
# rl_nmap C-v, C-q
function ble/widget/vi-rlfunc/quoted-insert-char.hook {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/keymap:vi/mark/start-edit-area
_ble_edit_arg=$ARG ble/widget/quoted-insert-char.hook
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/repeat/record
ble/keymap:vi/adjust-command-mode
return 0
}
function ble/widget/vi-rlfunc/quoted-insert-char {
_ble_edit_mark_active=
_ble_decode_char__hook=ble/widget/vi-rlfunc/quoted-insert-char.hook
return 147
}
function ble/widget/vi-rlfunc/quoted-insert.hook {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/keymap:vi/mark/start-edit-area
_ble_edit_arg=$ARG ble/widget/quoted-insert.hook
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/repeat/record
ble/keymap:vi/adjust-command-mode
return 0
}
function ble/widget/vi-rlfunc/quoted-insert {
_ble_edit_mark_active=
_ble_decode_key__hook=ble/widget/vi-rlfunc/quoted-insert.hook
return 147
}
# rl_nmap C-d
function ble/widget/vi-rlfunc/eof-maybe {
if [[ ! $_ble_edit_str ]]; then
ble/widget/exit
ble/keymap:vi/adjust-command-mode # ジョブがあるときは終了しないので。
return 1
elif ble-edit/is-single-complete-line; then
ble/widget/vi_nmap/accept-line
else
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/keymap:vi/mark/start-edit-area
_ble_edit_ind=${#_ble_edit_str}
_ble_edit_arg=$ARG
ble/widget/self-insert
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/adjust-command-mode
fi
}
# rl_nmap _
function ble/widget/vi-rlfunc/yank-arg {
ble/widget/vi_nmap/append-mode
ble/keymap:vi/imap-repeat/reset
local -a KEYS; KEYS=(32)
ble/widget/self-insert
ble/util/unlocal KEYS
ble/widget/insert-last-argument
return "$?"
}
# rlfunc: forward-byte, backward-byte
function ble/widget/vi-command/forward-byte {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local index=$_ble_edit_ind
ble/widget/.locate-forward-byte "$ARG" || [[ $FLAG ]] || ble/widget/.bell
ble/widget/vi-command/exclusive-goto.impl "$index" "$FLAG" "$REG"
}
function ble/widget/vi-command/backward-byte {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local index=$_ble_edit_ind
ble/widget/.locate-forward-byte "$((-ARG))" || [[ $FLAG ]] || ble/widget/.bell
ble/widget/vi-command/exclusive-goto.impl "$index" "$FLAG" "$REG"
}
function ble/widget/vi-command/execute-named-command/accept.hook {
ble/keymap:vi/update-mode-indicator
ble/widget/execute-named-command/accept.hook "$1"
}
function ble/widget/vi-command/execute-named-command {
ble/keymap:vi/async-commandline-mode 'ble/widget/vi-command/execute-named-command/accept.hook'
_ble_keymap_vi_cmap_before_widget=ble/edit/async-read-mode/empty-cancel.hook
ble/history/set-prefix _ble_edit_rlfunc
_ble_edit_PS1='!'
_ble_syntax_lang=edit.named-command
_ble_highlight_layer_list=(plain syntax region overwrite_mode)
return 147
}
# rlfunc: capitalize-word, downcase-word, upcase-word
#%define 2
function ble/widget/vi_nmap/capitalize-XWORD { ble/widget/filter-word.impl XWORD ble/string#capitalize; }
function ble/widget/vi_nmap/downcase-XWORD { ble/widget/filter-word.impl XWORD ble/string#tolower; }
function ble/widget/vi_nmap/upcase-XWORD { ble/widget/filter-word.impl XWORD ble/string#toupper; }
#%end
#%expand 2.r/XWORD/eword/
#%expand 2.r/XWORD/cword/
#%expand 2.r/XWORD/uword/
#%expand 2.r/XWORD/sword/
#%expand 2.r/XWORD/fword/
function ble/widget/vi_nmap/@edit {
ble/keymap:vi/clear-arg
ble/keymap:vi/repeat/record
ble/keymap:vi/mark/start-edit-area
ble/widget/"$@"
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/adjust-command-mode
}
function ble/widget/vi_nmap/@adjust {
ble/keymap:vi/clear-arg
ble/widget/"$@"
ble/keymap:vi/adjust-command-mode
}
function ble/widget/vi_nmap/@motion {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local _ble_edit_ind=$_ble_edit_ind _ble_edit_arg=$ARG
if ble/widget/"$@"; then
local index=$_ble_edit_ind
ble/util/unlocal _ble_edit_ind
ble/widget/vi-command/exclusive-goto.impl "$index" "$FLAG" "$REG" nobell
else
ble/keymap:vi/adjust-command-mode
fi
}
#------------------------------------------------------------------------------
# Visual mode
# 選択の種類は _ble_edit_mark_active に設定される文字列で区別する。
#
# _ble_edit_mark_active は vi_char, vi_line, vi_block のどれかである
# 更に末尾拡張 (行末までの選択範囲の拡張) が設定されているときには
# vi_char+, vi_line+, vi_block+ などの様に + が末尾に付く。
function ble/keymap:vi/xmap/has-eol-extension {
[[ $_ble_edit_mark_active == *+ ]]
}
function ble/keymap:vi/xmap/add-eol-extension {
[[ $_ble_edit_mark_active ]] &&
_ble_edit_mark_active=${_ble_edit_mark_active%+}+
}
function ble/keymap:vi/xmap/remove-eol-extension {
[[ $_ble_edit_mark_active ]] &&
_ble_edit_mark_active=${_ble_edit_mark_active%+}
}
function ble/keymap:vi/xmap/switch-type {
local suffix; [[ $_ble_edit_mark_active == *+ ]] && suffix=+
_ble_edit_mark_active=$1$suffix
}
#--------------------------------------
# xmap/矩形範囲の抽出
## @fn local p0 q0 lx ly rx ry; ble/keymap:vi/get-graphical-rectangle [index1 [index2]]
## @fn local p0 q0 lx ly rx ry; ble/keymap:vi/get-logical-rectangle [index1 [index2]]
## @fn local p0 q0 lx ly rx ry; ble/keymap:vi/get-rectangle [index1 [index2]]
## @fn local ret ; ble/keymap:vi/get-rectangle-height [index1 [index2]]
##
## @param[in,opt] index1 [=_ble_edit_mark]
## @param[in,opt] index2 [=_ble_edit_ind]
##
## @var[out] p0 q0
## @var[out] lx ly rx ry
##
function ble/keymap:vi/get-graphical-rectangle {
local p=${1:-$_ble_edit_mark} q=${2:-$_ble_edit_ind}
local ret
ble-edit/content/find-logical-bol "$p"; p0=$ret
ble-edit/content/find-logical-bol "$q"; q0=$ret
local p0x p0y q0x q0y
ble/textmap#getxy.out --prefix=p0 "$p0"
ble/textmap#getxy.out --prefix=q0 "$q0"
local plx ply qlx qly
ble/textmap#getxy.cur --prefix=pl "$p"
ble/textmap#getxy.cur --prefix=ql "$q"
local prx=$plx pry=$ply qrx=$qlx qry=$qly
ble-edit/content/eolp "$p" && ((prx++)) || ble/textmap#getxy.out --prefix=pr "$((p+1))"
ble-edit/content/eolp "$q" && ((qrx++)) || ble/textmap#getxy.out --prefix=qr "$((q+1))"
((ply-=p0y,qly-=q0y,pry-=p0y,qry-=q0y,
(ply<qly||ply==qly&&plx<qlx)?(lx=plx,ly=ply):(lx=qlx,ly=qly),
(pry>qry||pry==qry&&prx>qrx)?(rx=prx,ry=pry):(rx=qrx,ry=qry)))
}
function ble/keymap:vi/get-logical-rectangle {
local p=${1:-$_ble_edit_mark} q=${2:-$_ble_edit_ind}
local ret
ble-edit/content/find-logical-bol "$p"; p0=$ret
ble-edit/content/find-logical-bol "$q"; q0=$ret
((p-=p0,q-=q0,p<=q)) || local p=$q q=$p
lx=$p rx=$((q+1)) ly=0 ry=0
}
function ble/keymap:vi/get-rectangle {
if ble/edit/use-textmap; then
ble/keymap:vi/get-graphical-rectangle "$@"
else
ble/keymap:vi/get-logical-rectangle "$@"
fi
}
## @fn ble/keymap:vi/get-rectangle-height [index1 [index2]]
## @var[out] ret
function ble/keymap:vi/get-rectangle-height {
local p0 q0 lx ly rx ry
ble/keymap:vi/get-rectangle "$@"
ble/string#count-char "${_ble_edit_str:p0:q0-p0}" $'\n'
((ret++))
return 0
}
## @fn ble/keymap:vi/extract-graphical-block-by-geometry bol1 bol2 x1:y1 x2:y2 opts
## @fn ble/keymap:vi/extract-logical-block-by-geometry bol1 bol2 c1 c2 opts
## 指定した引数の範囲を元に矩形範囲を抽出します。
## @fn ble/keymap:vi/extract-graphical-block [index1 [index2 [opts]]]
## @fn ble/keymap:vi/extract-logical-block [index1 [index2 [opts]]]
## @fn ble/keymap:vi/extract-block [index1 [index2 [opts]]]
## 現在位置 (_ble_edit_ind) とマーク (_ble_edit_mark) を元に矩形範囲を抽出します。
##
## @param[in] bol1 bol2
## 2つの行の行頭を指定します。
## @param[in] x1:y1 x2:y2
## 2つの列を行頭からの相対位置で指定します。
## @param[in] c1 c2
## 2つの列を論理列で指定します。
##
## @param[in,opt] index1 [$_ble_edit_mark]
## @param[in,opt] index2 [$_ble_edit_ind]
## 矩形の端点の文字インデックスを指定します。
##
## @param[in,opt] opts
## コロン区切りのフラグ指定です。
##
## first_line
## 矩形を構成する最初の行についてだけ情報を取得します。
## その他の行については空の情報 (':::::') を sub_ranges に格納します。
##
## skip_middle
## 矩形を構成する最初と最後の行についてだけ情報を取得します。
## その他の行については空の情報 (':::::') を sub_ranges に格納します。
##
## @var[in] _ble_edit_mark_active
## 末尾拡張を行うばあいにこの引数の末端に + を指定します。
##
## @arr[out] sub_ranges
## 矩形を構成する各行の情報を格納します。
## 各要素は以下の形式を持ちます。
##
## smin:smax:slpad:srpad:sfill:stext
##
## smin smax
## 選択範囲を強調するとき・切り取るときの範囲を指定します。
## slpad srpad
## 選択範囲を切り取ったときに左右に補填する空白の数を指定します。
## sfill
## 矩形の挿入時に右端に補填するべき空白の数を指定します。
## stext
## 選択範囲から読み取られる文字列を指定します。
## 全角文字などが範囲の境界を跨ぐとき、
## その文字は (範囲に被る幅と同数の) 空白に置き換えられます。
## @var[out] sub_x1 sub_x2
##
function ble/keymap:vi/extract-graphical-block-by-geometry {
local bol1=$1 bol2=$2 x1=$3 x2=$4 y1=0 y2=0 opts=$5
((bol1<=bol2||(bol1=$2,bol2=$1)))
[[ $x1 == *:* ]] && local x1=${x1%%:*} y1=${x1#*:}
[[ $x2 == *:* ]] && local x2=${x2%%:*} y2=${x2#*:}
local cols=$_ble_textmap_cols
local c1=$((cols*y1+x1)) c2=$((cols*y2+x2))
sub_x1=$c1 sub_x2=$c2
local ret index lx ly rx ly
ble-edit/content/find-logical-eol "$bol2"; local eol2=$ret
local lines; ble/string#split-lines lines "${_ble_edit_str:bol1:eol2-bol1}"
sub_ranges=()
local min_sfill=0
local line bol=$bol1 eol bolx boly
local c1l c1r c2l c2r
for line in "${lines[@]}"; do
((eol=bol+${#line}))
if [[ :$opts: == *:first_line:* ]] && ((${#sub_ranges[@]})); then
ble/array#push sub_ranges :::::
elif [[ :$opts: == *:skip_middle:* ]] && ((0<${#sub_ranges[@]}&&${#sub_ranges[@]}<${#lines[@]}-1)); then
ble/array#push sub_ranges :::::
else
ble/textmap#getxy.out --prefix=bol "$bol"
ble/textmap#hit out "$x1" "$((boly+y1))" "$bol" "$eol"
local smin=$index x1l=$lx y1l=$ly x1r=$rx y1r=$ry
if ble/keymap:vi/xmap/has-eol-extension; then
local eolx eoly; ble/textmap#getxy.out --prefix=eol "$eol"
local smax=$eol x2l=$eolx y2l=$eoly x2r=$eolx y2r=$eoly
else
ble/textmap#hit out "$x2" "$((boly+y2))" "$bol" "$eol"
local smax=$index x2l=$lx y2l=$ly x2r=$rx y2r=$ry
fi
local sfill=0 slpad=0 srpad=0
local stext=${_ble_edit_str:smin:smax-smin}
if ((smin<smax)); then
# 1. 左の境界 c1 を大きな文字が跨いでいるときは空白に変換する。
((c1l=(y1l-boly)*cols+x1l))
if ((c1l<c1)); then
((slpad=c1-c1l))
# assert: smin < smax <= eol なので行末ではない
ble/util/assert '! ble-edit/content/eolp "$smin"'
((c1r=(y1r-boly)*cols+x1r))
ble/util/assert '((c1r>c1))' || ((c1r=c1))
ble/string#repeat ' ' "$((c1r-c1))"
stext=$ret${stext:1}
fi
# 2. 右の境界 c2 を大きな文字が跨いでいるときは空白に変換する
((c2l=(y2l-boly)*cols+x2l))
if ((c2l<c2)); then
if ((smax==eol)); then
((sfill=c2-c2l))
else
ble/string#repeat ' ' "$((c2-c2l))"
stext=$stext$ret
((smax++))
((c2r=(y2r-boly)*cols+x2r))
ble/util/assert '((c2r>c2))' || ((c2r=c2))
((srpad=c2r-c2))
fi
elif ((c2l>c2)); then
# ここに来るのは ble/keymap:vi/xmap/has-eol-extension のときのみの筈
((sfill=c2-c2l,
sfill<min_sfill&&(min_sfill=sfill)))
fi
else
if ((smin==eol)); then
# 行末
((sfill=c2-c1))
elif ((c2>c1)); then
# 範囲の両端が単一の文字の左端または内部にある
ble/string#repeat ' ' "$((c2-c1))"
stext=$ret${stext:1}
((smax++))
((c1l=(y1l-boly)*cols+x1l,slpad=c1-c1l))
((c1r=(y1r-boly)*cols+x1r,srpad=c1r-c1))
fi
fi
ble/array#push sub_ranges "$smin:$smax:$slpad:$srpad:$sfill:$stext"
fi
((bol=eol+1))
done
if ((min_sfill<0)); then
local isub=${#sub_ranges[@]}
while ((isub--)); do
local sub=${sub_ranges[isub]}
local sub45=${sub#*:*:*:*:}
local sfill=${sub45%%:*}
sub_ranges[isub]=${sub::${#sub}-${#sub45}}$((sfill-min_sfill))${sub45:${#sfill}}
done
fi
}
function ble/keymap:vi/extract-graphical-block {
local opts=$3
local p0 q0 lx ly rx ry
ble/keymap:vi/get-graphical-rectangle "$@"
ble/keymap:vi/extract-graphical-block-by-geometry "$p0" "$q0" "$lx:$ly" "$rx:$ry" "$opts"
}
function ble/keymap:vi/extract-logical-block-by-geometry {
local bol1=$1 bol2=$2 x1=$3 x2=$4 opts=$5
((bol1<=bol2||(bol1=$2,bol2=$1)))
sub_x1=$c1 sub_x2=$c2
local ret min_sfill=0
local bol=$bol1 eol smin smax slpad srpad sfill
sub_ranges=()
while ((1)); do
ble-edit/content/find-logical-eol "$bol"; eol=$ret
slpad=0 srpad=0 sfill=0
((smin=bol+x1,smin>eol&&(smin=eol)))
if ble/keymap:vi/xmap/has-eol-extension; then
((smax=eol,
sfill=bol+x2-eol,
sfill<min_sfill&&(min_sfill=sfill)))
else
((smax=bol+x2,smax>eol&&(sfill=smax-eol,smax=eol)))
fi
local stext=${_ble_edit_str:smin:smax-smin}
ble/array#push sub_ranges "$smin:$smax:$slpad:$srpad:$sfill:$stext"
((bol>=bol2)) && break
ble-edit/content/find-logical-bol "$bol" 1; bol=$ret
done
if ((min_sfill<0)); then
local isub=${#sub_ranges[@]}
while ((isub--)); do
local sub=${sub_ranges[isub]}
local sub45=${sub#*:*:*:*:}
local sfill=${sub45%%:*}
sub_ranges[isub]=${sub::${#sub}-${#sub45}}$((sfill-min_sfill))${sub45:${#sfill}}
done
fi
}
function ble/keymap:vi/extract-logical-block {
local opts=$3
local p0 q0 lx ly rx ry
ble/keymap:vi/get-logical-rectangle "$@"
ble/keymap:vi/extract-logical-block-by-geometry "$p0" "$q0" "$lx" "$rx" "$opts"
}
function ble/keymap:vi/extract-block {
if ble/edit/use-textmap; then
ble/keymap:vi/extract-graphical-block "$@"
else
ble/keymap:vi/extract-logical-block "$@"
fi
}
#--------------------------------------
# xmap/選択範囲の着色の設定
## @fn ble/highlight/layer:region/mark:vi_char/get-selection
## @fn ble/highlight/layer:region/mark:vi_line/get-selection
## @fn ble/highlight/layer:region/mark:vi_block/get-selection
## @arr[out] selection
function ble/highlight/layer:region/mark:vi_char/get-selection {
local rmin rmax
if ((_ble_edit_mark<_ble_edit_ind)); then
rmin=$_ble_edit_mark rmax=$_ble_edit_ind
else
rmin=$_ble_edit_ind rmax=$_ble_edit_mark
fi
ble-edit/content/eolp "$rmax" || ((rmax++))
selection=("$rmin" "$rmax")
}
function ble/highlight/layer:region/mark:vi_line/get-selection {
local rmin rmax
if ((_ble_edit_mark<_ble_edit_ind)); then
rmin=$_ble_edit_mark rmax=$_ble_edit_ind
else
rmin=$_ble_edit_ind rmax=$_ble_edit_mark
fi
local ret
ble-edit/content/find-logical-bol "$rmin"; rmin=$ret
ble-edit/content/find-logical-eol "$rmax"; rmax=$ret
selection=("$rmin" "$rmax")
}
function ble/highlight/layer:region/mark:vi_block/get-selection {
local sub_ranges sub_x1 sub_x2
ble/keymap:vi/extract-block
selection=()
local sub
for sub in "${sub_ranges[@]}"; do
ble/string#split sub : "$sub"
((sub[0]<sub[1])) || continue
ble/array#push selection "${sub[0]}" "${sub[1]}"
done
}
function ble/highlight/layer:region/mark:vi_char+/get-selection {
ble/highlight/layer:region/mark:vi_char/get-selection
}
function ble/highlight/layer:region/mark:vi_line+/get-selection {
ble/highlight/layer:region/mark:vi_line/get-selection
}
function ble/highlight/layer:region/mark:vi_block+/get-selection {
ble/highlight/layer:region/mark:vi_block/get-selection
}
function ble/highlight/layer:region/mark:vi_char/get-face { [[ $_ble_edit_overwrite_mode ]] && face=region_target; }
function ble/highlight/layer:region/mark:vi_char+/get-face { ble/highlight/layer:region/mark:vi_char/get-face; }
function ble/highlight/layer:region/mark:vi_line/get-face { ble/highlight/layer:region/mark:vi_char/get-face; }
function ble/highlight/layer:region/mark:vi_line+/get-face { ble/highlight/layer:region/mark:vi_char/get-face; }
function ble/highlight/layer:region/mark:vi_block/get-face { ble/highlight/layer:region/mark:vi_char/get-face; }
function ble/highlight/layer:region/mark:vi_block+/get-face { ble/highlight/layer:region/mark:vi_char/get-face; }
#--------------------------------------
# xmap/前回の選択サイズ
_ble_keymap_vi_xmap_prev_edit=vi_char:1:1
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_xmap_prev_edit
function ble/widget/vi_xmap/.save-visual-state {
local nline nchar mark_type=${_ble_edit_mark_active%+}
if [[ $mark_type == vi_block ]]; then
local p0 q0 lx rx ly ry
if ble/edit/use-textmap; then
local cols=$_ble_textmap_cols
ble/keymap:vi/get-graphical-rectangle
((lx+=ly*cols,rx+=ry*cols))
else
ble/keymap:vi/get-logical-rectangle
fi
nchar=$((rx-lx))
local ret
((p0<=q0)) || local p0=$q0 q0=$p0
ble/string#count-char "${_ble_edit_str:p0:q0-p0}" $'\n'
nline=$((ret+1))
else
local ret
local p=$_ble_edit_mark q=$_ble_edit_ind
((p<=q)) || local p=$q q=$p
ble/string#count-char "${_ble_edit_str:p:q-p}" $'\n'
nline=$((ret+1))
local base
if ((nline==1)) && [[ $mark_type != vi_line ]]; then
base=$p
else
ble-edit/content/find-logical-bol "$q"; base=$ret
fi
if ble/edit/use-textmap; then
local cols=$_ble_textmap_cols
local bx by x y
ble/textmap#getxy.cur --prefix=b "$base"
ble/textmap#getxy.cur "$q"
nchar=$((x-bx+(y-by)*cols+1))
else
nchar=$((q-base+1))
fi
fi
_ble_keymap_vi_xmap_prev_edit=$_ble_edit_mark_active:$nchar:$nline
}
function ble/widget/vi_xmap/.restore-visual-state {
local arg=$1; ((arg>0)) || arg=1
local prev; ble/string#split prev : "$_ble_keymap_vi_xmap_prev_edit"
_ble_edit_mark_active=${prev[0]:-vi_char}
local nchar=${prev[1]:-1}
local nline=${prev[2]:-1}
((nchar<1&&(nchar=1),nline<1&&(nline=1)))
local is_x_relative=0
if [[ ${_ble_edit_mark_active%+} == vi_block ]]; then
((is_x_relative=1,nchar*=arg,nline*=arg))
elif [[ ${_ble_edit_mark_active%+} == vi_line ]]; then
((nline*=arg,is_x_relative=1,nchar=1))
else
((nline==1?(is_x_relative=1,nchar*=arg):(nline*=arg)))
fi
((nchar--,nline--))
local index ret
ble-edit/content/find-logical-bol "$_ble_edit_ind" 0; local b1=$ret
ble-edit/content/find-logical-bol "$_ble_edit_ind" "$nline"; local b2=$ret
ble-edit/content/find-logical-eol "$b2"; local e2=$ret
if ble/keymap:vi/xmap/has-eol-extension; then
index=$e2
elif ble/edit/use-textmap; then
local cols=$_ble_textmap_cols
local b1x b1y b2x b2y x y
ble/textmap#getxy.out --prefix=b1 "$b1"
ble/textmap#getxy.out --prefix=b2 "$b2"
if ((is_x_relative)); then
ble/textmap#getxy.out "$_ble_edit_ind"
local c=$((x+(y-b1y)*cols+nchar))
else
local c=$nchar
fi
((y=c/cols,x=c%cols))
local lx ly rx ry
ble/textmap#hit out "$x" "$((b2y+y))" "$b2" "$e2"
else
local c=$((is_x_relative?_ble_edit_ind-b1+nchar:nchar))
((index=b2+c,index>e2&&(index=e2)))
fi
_ble_edit_mark=$_ble_edit_ind
_ble_edit_ind=$index
}
#--------------------------------------
# xmap/前回の選択範囲
# mark `< `>
_ble_keymap_vi_xmap_prev_visual=
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_xmap_prev_visual
function ble/keymap:vi/xmap/set-previous-visual-area {
local beg end
local mark_type=${_ble_edit_mark_active%+}
if [[ $mark_type == vi_block ]]; then
local sub_ranges sub_x1 sub_x2
ble/keymap:vi/extract-block
local nrange=${#sub_ranges[*]}
((nrange)) || return 1
local beg=${sub_ranges[0]%%:*}
local sub2_slice1=${sub_ranges[nrange-1]#*:}
local end=${sub2_slice1%%:*}
((beg<end)) && ! ble-edit/content/bolp "$end" && ((end--))
else
local beg=$_ble_edit_mark end=$_ble_edit_ind
((beg<=end)) || local beg=$end end=$beg
if [[ $mark_type == vi_line ]]; then
local ret
ble-edit/content/find-logical-bol "$beg"; beg=$ret
ble-edit/content/find-logical-eol "$end"; end=$ret
ble-edit/content/bolp "$end" || ((end--))
fi
fi
_ble_keymap_vi_xmap_prev_visual=$_ble_edit_mark_active
ble/keymap:vi/mark/set-local-mark 60 "$beg" # `<
ble/keymap:vi/mark/set-local-mark 62 "$end" # `>
}
# nmap/xmap gv
function ble/widget/vi-command/previous-visual-area {
local mark=$_ble_keymap_vi_xmap_prev_visual
local ret beg= end=
ble/keymap:vi/mark/get-local-mark 60 && beg=$ret # `<
ble/keymap:vi/mark/get-local-mark 62 && end=$ret # `>
[[ $beg && $end ]] || return 1
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
ble/keymap:vi/clear-arg
ble/keymap:vi/xmap/set-previous-visual-area
_ble_edit_ind=$end
_ble_edit_mark=$beg
_ble_edit_mark_active=$mark
ble/keymap:vi/update-mode-indicator
else
ble/keymap:vi/clear-arg
ble/widget/vi-command/visual-mode.impl vi_xmap "$mark"
_ble_edit_ind=$end
_ble_edit_mark=$beg
fi
return 0
}
#--------------------------------------
# xmap/モード遷移
function ble/widget/vi-command/visual-mode.impl {
local keymap=$1 visual_type=$2
local ARG FLAG REG; ble/keymap:vi/get-arg 0
if [[ $FLAG ]]; then
ble/widget/vi-command/bell
return 1
fi
_ble_edit_overwrite_mode=
_ble_edit_mark=$_ble_edit_ind
_ble_edit_mark_active=$visual_type
_ble_keymap_vi_xmap_insert_data= # ※矩形挿入の途中で更に xmap に入ったときはキャンセル
((ARG)) && ble/widget/vi_xmap/.restore-visual-state "$ARG"
ble/decode/keymap/push "$keymap"
ble/keymap:vi/update-mode-indicator
return 0
}
function ble/widget/vi_nmap/charwise-visual-mode {
ble/widget/vi-command/visual-mode.impl vi_xmap vi_char
}
function ble/widget/vi_nmap/linewise-visual-mode {
ble/widget/vi-command/visual-mode.impl vi_xmap vi_line
}
function ble/widget/vi_nmap/blockwise-visual-mode {
ble/widget/vi-command/visual-mode.impl vi_xmap vi_block
}
function ble/widget/vi_nmap/charwise-select-mode {
ble/widget/vi-command/visual-mode.impl vi_smap vi_char
}
function ble/widget/vi_nmap/linewise-select-mode {
ble/widget/vi-command/visual-mode.impl vi_smap vi_line
}
function ble/widget/vi_nmap/blockwise-select-mode {
ble/widget/vi-command/visual-mode.impl vi_smap vi_block
}
function ble/widget/vi_xmap/exit {
# Note: xmap operator:c
# -> vi_xmap/block-insert-mode.impl
# → vi_xmap/cancel 経由で呼び出されるとき、
# 既に vi_nmap に戻っていることがあるので、vi_xmap, vi_smap のときだけ処理する。
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
ble/keymap:vi/xmap/set-previous-visual-area
_ble_edit_mark_active=
ble/decode/keymap/pop
ble/keymap:vi/update-mode-indicator
ble/keymap:vi/adjust-command-mode
fi
return 0
}
function ble/widget/vi_xmap/cancel {
# もし single-command-mode にいたとしても消去して normal へ移動する
_ble_keymap_vi_single_command=
_ble_keymap_vi_single_command_overwrite=
ble-edit/content/nonbol-eolp && ((_ble_edit_ind--))
ble/widget/vi_xmap/exit
}
function ble/widget/vi_xmap/switch-visual-mode.impl {
local visual_type=$1
local ARG FLAG REG; ble/keymap:vi/get-arg 0
if [[ $FLAG ]]; then
ble/widget/.bell
return 1
fi
if [[ ${_ble_edit_mark_active%+} == "$visual_type" ]]; then
ble/widget/vi_xmap/cancel
else
ble/keymap:vi/xmap/switch-type "$visual_type"
ble/keymap:vi/update-mode-indicator
return 0
fi
}
# xmap v
function ble/widget/vi_xmap/switch-to-charwise {
ble/widget/vi_xmap/switch-visual-mode.impl vi_char
}
# xmap V
function ble/widget/vi_xmap/switch-to-linewise {
ble/widget/vi_xmap/switch-visual-mode.impl vi_line
}
# xmap <C-v>
function ble/widget/vi_xmap/switch-to-blockwise {
ble/widget/vi_xmap/switch-visual-mode.impl vi_block
}
# xmap <C-g>
function ble/widget/vi_xmap/switch-to-select {
if [[ $_ble_decode_keymap == vi_xmap ]]; then
ble/decode/keymap/pop
ble/decode/keymap/push vi_smap
ble/keymap:vi/update-mode-indicator
fi
}
# smap <C-g>
function ble/widget/vi_xmap/switch-to-visual {
if [[ $_ble_decode_keymap == vi_smap ]]; then
ble/decode/keymap/pop
ble/decode/keymap/push vi_xmap
ble/keymap:vi/update-mode-indicator
fi
}
# smap <C-v>
function ble/widget/vi_xmap/switch-to-visual-blockwise {
if [[ $_ble_decode_keymap == vi_smap ]]; then
ble/decode/keymap/pop
ble/decode/keymap/push vi_xmap
fi
if [[ ${_ble_edit_mark_active%+} != vi_block ]]; then
ble/widget/vi_xmap/switch-to-blockwise
else
xble/keymap:vi/update-mode-indicator
fi
}
## @bleopt keymap_vi_keymodel
## 選択モードにおける移動コマンドの振る舞いを制御します。
bleopt/declare -v keymap_vi_keymodel ''
function ble/widget/vi_smap/@nomarked {
[[ ,$bleopt_keymap_vi_keymodel, == *,stopsel,* ]] &&
ble/widget/vi_xmap/exit
ble/widget/"$@"
}
#--------------------------------------
# xmap/各種コマンド
function ble/widget/vi_smap/self-insert {
# Note: repeat (nmap .) についてはこの実装で良い。
# KEYS=(...) vi_smap/self-insert として記録されるので。
ble/widget/vi-command/operator c
ble/widget/self-insert
}
# xmap o
function ble/widget/vi_xmap/exchange-points {
ble/keymap:vi/xmap/remove-eol-extension
ble/widget/exchange-point-and-mark
return 0
}
# xmap O
function ble/widget/vi_xmap/exchange-boundaries {
if [[ ${_ble_edit_mark_active%+} == vi_block ]]; then
ble/keymap:vi/xmap/remove-eol-extension
local sub_ranges sub_x1 sub_x2
ble/keymap:vi/extract-block '' '' skip_middle
local nline=${#sub_ranges[@]}
ble/util/assert '((nline))'
local data1; ble/string#split data1 : "${sub_ranges[0]}"
local lpos1=${data1[0]} rpos1=$((data1[4]?data1[1]:data1[1]-1))
if ((nline==1)); then
local lpos2=$lpos1 rpos2=$rpos1
else
local data2; ble/string#split data2 : "${sub_ranges[nline-1]}"
local lpos2=${data2[0]} rpos2=$((data2[4]?data2[1]:data2[1]-1))
fi
# lpos2:rpos2 が _ble_edit_ind に対応していないとき swap する
if ! ((lpos2<=_ble_edit_ind&&_ble_edit_ind<=rpos2)); then
local lpos1=$lpos2 lpos2=$lpos1
local rpos1=$rpos2 rpos2=$rpos1
fi
_ble_edit_mark=$((_ble_edit_mark==lpos1?rpos1:lpos1))
_ble_edit_ind=$((_ble_edit_ind==lpos2?rpos2:lpos2))
return 0
else
ble/widget/vi_xmap/exchange-points
fi
}
# xmap r{char}
function ble/widget/vi_xmap/visual-replace-char.hook {
local key=$1
_ble_edit_overwrite_mode=
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret
if [[ $FLAG ]]; then
ble/widget/.bell
return 1
elif ((key==(_ble_decode_Ctrl|91))); then # C-[ -> cancel
return 27
elif ! ble/keymap:vi/k2c "$key"; then
ble/widget/.bell
return 1
fi
local c=$ret
ble/util/c2s "$c"; local s=$ret
local old_mark_active=$_ble_edit_mark_active # save
local mark_type=${_ble_edit_mark_active%+}
ble/widget/vi_xmap/.save-visual-state
ble/widget/vi_xmap/exit # Note: _ble_edit_mark_active will be cleared here
if [[ $mark_type == vi_block ]]; then
ble/util/c2w "$c"; local w=$ret
((w<=0)) && w=1
local sub_ranges sub_x1 sub_x2
_ble_edit_mark_active=$old_mark_active ble/keymap:vi/extract-block
local n=${#sub_ranges[@]}
if ((n==0)); then
ble/widget/.bell
return 1
fi
# create ins
local width=$((sub_x2-sub_x1))
local count=$((width/w))
ble/string#repeat "$s" "$count"; local ins=$ret
local pad=$((width-count*w))
if ((pad)); then
ble/string#repeat ' ' "$pad"; ins=$ins$ret
fi
local i=$n sub smin=0
ble/keymap:vi/mark/start-edit-area
while ((i--)); do
ble/string#split sub : "${sub_ranges[i]}"
local smin=${sub[0]} smax=${sub[1]}
local slpad=${sub[2]} srpad=${sub[3]} sfill=${sub[4]}
local ins1=$ins
((sfill)) && ins1=${ins1::(width-sfill)/w}
((slpad)) && { ble/string#repeat ' ' "$slpad"; ins1=$ret$ins1; }
((srpad)) && { ble/string#repeat ' ' "$srpad"; ins1=$ins1$ret; }
ble/widget/.replace-range "$smin" "$smax" "$ins1"
done
local beg=$smin
ble/keymap:vi/needs-eol-fix "$beg" && ((beg--))
_ble_edit_ind=$beg
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/repeat/record
else
local beg=$_ble_edit_mark end=$_ble_edit_ind
((beg<=end)) || local beg=$end end=$beg
if [[ $mark_type == vi_line ]]; then
ble-edit/content/find-logical-bol "$beg"; local beg=$ret
ble-edit/content/find-logical-eol "$end"; local end=$ret
else
ble-edit/content/eolp "$end" || ((end++))
fi
local ins=${_ble_edit_str:beg:end-beg}
ins=${ins//[!$'\n']/"$s"}
ble/widget/.replace-range "$beg" "$end" "$ins"
ble/keymap:vi/needs-eol-fix "$beg" && ((beg--))
_ble_edit_ind=$beg
ble/keymap:vi/mark/set-previous-edit-area "$beg" "$end"
ble/keymap:vi/repeat/record
fi
return 0
}
function ble/widget/vi_xmap/visual-replace-char {
_ble_edit_overwrite_mode=R
ble/keymap:vi/async-read-char ble/widget/vi_xmap/visual-replace-char.hook
}
function ble/widget/vi_xmap/linewise-operator.impl {
local op=$1 opts=$2
local ARG FLAG REG; ble/keymap:vi/get-arg 1
if [[ $FLAG ]]; then
ble/widget/.bell 'wrong keymap: xmap ではオペレータは設定されないはず'
return 1
fi
local mark_type=${_ble_edit_mark_active%+}
local beg=$_ble_edit_mark end=$_ble_edit_ind
((beg<=end)) || local beg=$end end=$beg
local call_operator=
if [[ :$opts: != *:force_line:* && $mark_type == vi_block ]]; then
call_operator=ble/keymap:vi/call-operator-blockwise
_ble_edit_mark_active=vi_block
[[ :$opts: == *:extend:* ]] && _ble_edit_mark_active=vi_block+
else
call_operator=ble/keymap:vi/call-operator-linewise
_ble_edit_mark_active=vi_line
fi
local ble_keymap_vi_mark_active=$_ble_edit_mark_active
ble/widget/vi_xmap/.save-visual-state
ble/widget/vi_xmap/exit
"$call_operator" "$op" "$beg" "$end" "$ARG" "$REG"; local ext=$?
((ext==147)) && return 147
((ext)) && ble/widget/.bell
ble/keymap:vi/adjust-command-mode
return "$ext"
}
# xmap C
function ble/widget/vi_xmap/replace-block-lines { ble/widget/vi_xmap/linewise-operator.impl c extend; }
# xmap D X
function ble/widget/vi_xmap/delete-block-lines { ble/widget/vi_xmap/linewise-operator.impl d extend; }
# xmap R S
function ble/widget/vi_xmap/delete-lines { ble/widget/vi_xmap/linewise-operator.impl d force_line; }
# xmap Y
function ble/widget/vi_xmap/copy-block-or-lines { ble/widget/vi_xmap/linewise-operator.impl y; }
function ble/widget/vi_xmap/connect-line.impl {
local name=$1
local ARG FLAG REG; ble/keymap:vi/get-arg 1 # ignored
local beg=$_ble_edit_mark end=$_ble_edit_ind
((beg<=end)) || local beg=$end end=$beg
local ret; ble/string#count-char "${_ble_edit_str:beg:end-beg}" $'\n'; local nline=$((ret+1))
ble/widget/vi_xmap/.save-visual-state
ble/widget/vi_xmap/exit # Note: _ble_edit_mark_active will be cleared here
_ble_edit_ind=$beg
_ble_edit_arg=$nline
_ble_keymap_vi_oparg=
_ble_keymap_vi_opfunc=
_ble_keymap_vi_reg=
ble/widget/"$name"
}
# xmap J
function ble/widget/vi_xmap/connect-line-with-space {
ble/widget/vi_xmap/connect-line.impl vi_nmap/connect-line-with-space
}
# xmap gJ
function ble/widget/vi_xmap/connect-line {
ble/widget/vi_xmap/connect-line.impl vi_nmap/connect-line
}
#--------------------------------------
# xmap/矩形挿入モード
## @var _ble_keymap_vi_xmap_insert_data
## 矩形挿入モードの情報を保持します。
## iline:x1:width:content の形式です。
##
## iline
## 編集を行う行の番号を保持します。
## x1
## 挿入開始位置を表示列で保持します。
## width
## 編集行の元々の幅を保持します。
## nline
## 行数を保持します。
##
_ble_keymap_vi_xmap_insert_data=
_ble_keymap_vi_xmap_insert_dbeg=-1
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_xmap_insert_data \
_ble_keymap_vi_xmap_insert_dbeg
function ble/keymap:vi/xmap/update-dirty-range {
[[ $_ble_keymap_vi_insert_leave == ble/widget/vi_xmap/block-insert-mode.onleave ]] &&
((_ble_keymap_vi_xmap_insert_dbeg<0||beg<_ble_keymap_vi_xmap_insert_dbeg)) &&
_ble_keymap_vi_xmap_insert_dbeg=$beg
}
## @fn ble/widget/vi_xmap/block-insert-mode.impl
## @var[in] sub_ranges sub_x1 sub_x2
function ble/widget/vi_xmap/block-insert-mode.impl {
local type=$1
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local nline=${#sub_ranges[@]}
ble/util/assert '((nline))'
local index ins_x
if [[ $type == append ]]; then
local sub=${sub_ranges[0]#*:}
local smax=${sub%%:*}
index=$smax
if ble/keymap:vi/xmap/has-eol-extension; then
ins_x='$'
else
ins_x=$sub_x2
fi
else
local sub=${sub_ranges[0]}
local smin=${sub%%:*}
index=$smin
ins_x=$sub_x1
fi
ble/widget/vi_xmap/cancel
_ble_edit_ind=$index
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
ble/keymap:vi/mark/set-local-mark 1 "$_ble_edit_ind"
_ble_keymap_vi_xmap_insert_dbeg=-1
local ret display_width
ble/string#count-char "${_ble_edit_str::_ble_edit_ind}" $'\n'; local iline=$ret
ble-edit/content/find-logical-bol; local bol=$ret
ble-edit/content/find-logical-eol; local eol=$ret
if ble/edit/use-textmap; then
local bx by ex ey
ble/textmap#getxy.out --prefix=b "$bol"
ble/textmap#getxy.out --prefix=e "$eol"
((display_width=ex+_ble_textmap_cols*(ey-by)))
else
((display_width=eol-bol))
fi
_ble_keymap_vi_xmap_insert_data=$iline:$ins_x:$display_width:$nline
_ble_keymap_vi_insert_leave=ble/widget/vi_xmap/block-insert-mode.onleave
return 0
}
function ble/widget/vi_xmap/block-insert-mode.onleave {
local data=$_ble_keymap_vi_xmap_insert_data
[[ $data ]] || continue
_ble_keymap_vi_xmap_insert_data=
ble/string#split data : "$data"
# カーソル行が記録行と同じか
local ret
ble-edit/content/find-logical-bol; local bol=$ret
ble/string#count-char "${_ble_edit_str::bol}" $'\n'; ((ret==data[0])) || return 1 # 行番号
ble/keymap:vi/mark/get-local-mark 1 || return 1; local mark=$ret # `[
ble-edit/content/find-logical-bol "$mark"; ((bol==ret)) || return 1 # 記録行 `[ と同じか
local has_textmap=
if ble/edit/use-textmap; then
local cols=$_ble_textmap_cols
has_textmap=1
fi
# 表示幅の変量
local new_width delta
ble-edit/content/find-logical-eol; local eol=$ret
if [[ $has_textmap ]]; then
local bx by ex ey
ble/textmap#getxy.out --prefix=b "$bol"
ble/textmap#getxy.out --prefix=e "$eol"
((new_width=ex+cols*(ey-by)))
else
((new_width=eol-bol))
fi
((delta=new_width-data[2]))
((delta>0)) || return 1 # 縮んだ場合は処理しない
# 切り出し列の決定
local x1=${data[1]}
[[ $x1 == '$' ]] && ((x1=data[2]))
((x1>new_width&&(x1=new_width)))
if ((bol<=_ble_keymap_vi_xmap_insert_dbeg&&_ble_keymap_vi_xmap_insert_dbeg<=eol)); then
local px py
if [[ $has_textmap ]]; then
ble/textmap#getxy.out --prefix=p "$_ble_keymap_vi_xmap_insert_dbeg"
((px+=cols*(py-by)))
else
((px=_ble_keymap_vi_xmap_insert_dbeg-bol))
fi
((px>x1&&(x1=px)))
fi
local x2=$((x1+delta))
# 切り出し
local ins= p1 p2
if [[ $has_textmap ]]; then
local index lx ly rx ry
ble/textmap#hit out "$((x1%cols))" "$((by+x1/cols))" "$bol" "$eol"; p1=$index
ble/textmap#hit out "$((x2%cols))" "$((by+x2/cols))" "$bol" "$eol"; p2=$index
((lx+=(ly-by)*cols,rx+=(ry-by)*cols,lx!=rx&&p2++))
else
((p1=bol+x1,p2=bol+x2))
fi
ins=${_ble_edit_str:p1:p2-p1}
# 挿入の決定
local -a ins_beg=() ins_text=()
local iline=1 nline=${data[3]} strlen=${#_ble_edit_str}
for ((iline=1;iline<nline;iline++)); do
local index= lpad=
if ((eol<strlen)); then
bol=$((eol+1))
ble-edit/content/find-logical-eol "$bol"; eol=$ret
else
bol=$eol lpad=$'\n'
fi
if [[ ${data[1]} == '$' ]]; then
index=$eol
elif [[ $has_textmap ]]; then
ble/textmap#getxy.out --prefix=b "$bol"
ble/textmap#hit out "$((x1%cols))" "$((by+x1/cols))" "$bol" "$eol" # -> index
local nfill
if ((index==eol&&(nfill=x1-lx+(ly-by)*cols)>0)); then
ble/string#repeat ' ' "$nfill"; lpad=$lpad$ret
fi
else
index=$((bol+x1))
if ((index>eol)); then
ble/string#repeat ' ' "$((index-eol))"; lpad=$lpad$ret
((index=eol))
fi
fi
ble/array#push ins_beg "$index"
ble/array#push ins_text "$lpad$ins"
done
# 挿入実行
local i=${#ins_beg[@]}
ble/keymap:vi/mark/start-edit-area
ble/keymap:vi/mark/commit-edit-area "$p1" "$p2"
while ((i--)); do
local index=${ins_beg[i]} text=${ins_text[i]}
ble/widget/.replace-range "$index" "$index" "$text"
done
ble/keymap:vi/mark/end-edit-area
# Note: この編集は record-insert 経由で記録されるので
# ここで明示的に ble/keymap:vi/repeat/record を呼び出す必要はない。
# 領域の最初に
local index
if ble/keymap:vi/mark/get-local-mark 60 && index=$ret; then
ble/widget/vi-command/goto-mark.impl "$index"
else
ble-edit/content/find-logical-bol; index=$ret
fi
# ノーマルモードに戻る時に一文字カーソルが戻るので一文字進めておく。
ble-edit/content/eolp || ((index++))
_ble_edit_ind=$index
return 0
}
# xmap I
function ble/widget/vi_xmap/insert-mode {
local mark_type=${_ble_edit_mark_active%+}
if [[ $mark_type == vi_block ]]; then
local sub_ranges sub_x1 sub_x2
ble/keymap:vi/extract-block '' '' first_line
ble/widget/vi_xmap/block-insert-mode.impl insert
else
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local beg=$_ble_edit_mark end=$_ble_edit_ind
((beg<=end)) || local beg=$end end=$beg
if [[ $mark_type == vi_line ]]; then
local ret
ble-edit/content/find-logical-bol "$beg"; beg=$ret
fi
ble/widget/vi_xmap/cancel
_ble_edit_ind=$beg
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
fi
}
# xmap A
function ble/widget/vi_xmap/append-mode {
local mark_type=${_ble_edit_mark_active%+}
if [[ $mark_type == vi_block ]]; then
local sub_ranges sub_x1 sub_x2
ble/keymap:vi/extract-block '' '' first_line
ble/widget/vi_xmap/block-insert-mode.impl append
else
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local beg=$_ble_edit_mark end=$_ble_edit_ind
((beg<=end)) || local beg=$end end=$beg
if [[ $mark_type == vi_line ]]; then
# 行指向のときは最終行の先頭か _ble_edit_ind の内、
# 後にある文字の後に移動する。
if ((_ble_edit_mark>_ble_edit_ind)); then
local ret
ble-edit/content/find-logical-bol "$end"; end=$ret
fi
fi
ble-edit/content/eolp "$end" || ((end++))
ble/widget/vi_xmap/cancel
_ble_edit_ind=$end
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
fi
}
#--------------------------------------
# xmap/貼り付け
# xmap: p, P
function ble/widget/vi_xmap/paste.impl {
local opts=$1
[[ :$opts: != *:after:* ]]; local is_after=$?
local ARG FLAG REG; ble/keymap:vi/get-arg 1
[[ $REG ]] && ble/keymap:vi/register#load "$REG"
local mark_type=${_ble_edit_mark_active%+}
local kill_ring=$_ble_edit_kill_ring
local kill_type=$_ble_edit_kill_type
local adjustment=
if [[ $mark_type == vi_block ]]; then
if [[ $kill_type == L ]]; then
# P: V → C-v のときは C-v の最終行直後に挿入
if ((is_after)); then
local ret; ble/keymap:vi/get-rectangle-height; local nline=$ret
adjustment=lastline:$nline
fi
elif [[ $kill_type == B:* ]]; then
# C-v → C-v
is_after=0
else
# 単純 v → C-v はブロック挿入に切り替え
is_after=0
if [[ $kill_ring != *$'\n'* ]]; then
((${#kill_ring}>=2)) && adjustment=index:$((${#kill_ring}*ARG-1))
local ret; ble/keymap:vi/get-rectangle-height; local nline=$ret
ble/string#repeat "$kill_ring"$'\n' "$nline"; kill_ring=${ret%$'\n'}
ble/string#repeat '0 ' "$nline"; kill_type=B:${ret% }
fi
fi
elif [[ $mark_type == vi_line ]]; then
if [[ $kill_type == L ]]; then
# V → V のとき
is_after=0
elif [[ $kill_type == B:* ]]; then
# C-v → V のとき、行貼り付け。
# kill_type=B:* のとき kill_ring の末端の改行は空行を意味するので、
# 空行が消えないように $'\n' を付加する必要がある。
is_after=0 kill_type=L kill_ring=$kill_ring$'\n'
else
# v → V のとき、行貼り付けになる。
is_after=0 kill_type=L
[[ $kill_ring == *$'\n' ]] && kill_ring=$kill_ring$'\n'
fi
else
# v, V, C-v → v のとき
is_after=0
[[ $kill_type == L ]] && adjustment=newline
fi
ble/keymap:vi/mark/start-edit-area
local _ble_keymap_vi_mark_suppress_edit=1
{
ble/widget/vi-command/operator d; local ext=$? # _ble_edit_kill_{ring,type} is set here
if [[ $adjustment == newline ]]; then
local -a KEYS=(10)
ble/widget/self-insert
elif [[ $adjustment == lastline:* ]]; then
local ret
ble-edit/content/find-logical-bol "$_ble_edit_ind" "$((${adjustment#*:}-1))"
_ble_edit_ind=$ret
fi
local _ble_edit_kill_ring=$kill_ring
local _ble_edit_kill_type=$kill_type
ble/widget/vi_nmap/paste.impl "$ARG" '' "$is_after"
if [[ $adjustment == index:* ]]; then
local index=$((_ble_edit_ind+${adjustment#*:}))
((index>${#_ble_edit_str}&&(index=${#_ble_edit_str})))
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
_ble_edit_ind=$index
fi
}
ble/util/unlocal _ble_keymap_vi_mark_suppress_edit
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/repeat/record
return "$ext"
}
function ble/widget/vi_xmap/paste-after {
ble/widget/vi_xmap/paste.impl after
}
function ble/widget/vi_xmap/paste-before {
ble/widget/vi_xmap/paste.impl before
}
#--------------------------------------
# xmap <C-a>, <C-x>, g<C-a>, g<C-x>
## @fn ble/widget/vi_xmap/increment.impl opts
##
## @param[in] opts
## 以下の項目をコロンで区切って指定したものです。
##
## - increase [既定]
## 数字を増加させます。
## - decrease
## 数字を減少させるます。
## - progressive
## k 個目の数字について増加・減少量を k 倍します。
##
function ble/widget/vi_xmap/increment.impl {
local opts=$1
local ARG FLAG REG; ble/keymap:vi/get-arg 1
if [[ $FLAG ]]; then
ble/widget/.bell
return 1
fi
local delta=$ARG
[[ :$opts: == *:decrease:* ]] && ((delta=-delta))
local progress=0
[[ :$opts: == *:progressive:* ]] && progress=$delta
local old_mark_active=$_ble_edit_mark_active # save
local mark_type=${_ble_edit_mark_active%+}
ble/widget/vi_xmap/.save-visual-state
ble/widget/vi_xmap/exit # Note: _ble_edit_mark_active will be cleared here
if [[ $mark_type == vi_block ]]; then
local sub_ranges sub_x1 sub_x2
_ble_edit_mark_active=$old_mark_active ble/keymap:vi/extract-block
if ((${#sub_ranges[@]}==0)); then
ble/widget/.bell
return 1
fi
else
local beg=$_ble_edit_mark end=$_ble_edit_ind
((beg<=end)) || local beg=$end end=$beg
if [[ $mark_type == vi_line ]]; then
local ret
ble-edit/content/find-logical-bol "$beg"; local beg=$ret
ble-edit/content/find-logical-eol "$end"; local end=$ret
else
ble-edit/content/eolp "$end" || ((end++))
fi
local -a lines
ble/string#split-lines lines "${_ble_edit_str:beg:end-beg}"
# sub_ranges 生成
local line index=$beg
local -a sub_ranges
for line in "${lines[@]}"; do
[[ $line ]] && ble/array#push sub_ranges "$index:::::$line"
((index+=${#line}+1))
done
((${#sub_ranges[@]})) || return 0
fi
local sub rex_number='^([^0-9]*)([0-9]+)' shift=0 dmin=-1 dmax=-1
for sub in "${sub_ranges[@]}"; do
local stext=${sub#*:*:*:*:*:}
[[ $stext =~ $rex_number ]] || continue
# 元々の数
local rematch1=${BASH_REMATCH[1]}
local rematch2=${BASH_REMATCH[2]}
local offset=${#rematch1} length=${#rematch2}
local number=$((10#0$rematch2))
[[ $rematch1 == *- ]] && ((number=-number,offset--,length++))
# 新しい数
((number+=delta,delta+=progress))
if [[ $rematch2 == 0?* ]]; then
# Zero padding
local wsign=$((number<0?1:0))
local zpad=$((wsign+${#rematch2}-${#number}))
if ((zpad>0)); then
local ret; ble/string#repeat 0 "$zpad"
number=${number::wsign}$ret${number:wsign}
fi
fi
local smin=${sub%%:*}
local beg=$((shift+smin+offset))
local end=$((beg+length))
ble/widget/.replace-range "$beg" "$end" "$number"
((shift+=${#number}-length,
dmin<0&&(dmin=beg),
dmax=beg+${#number}))
done
local beg=${sub_ranges[0]%%:*}
ble/keymap:vi/needs-eol-fix "$beg" && ((beg--))
_ble_edit_ind=$beg
((dmin>=0)) && ble/keymap:vi/mark/set-previous-edit-area "$dmin" "$dmax"
ble/keymap:vi/repeat/record
return 0
}
# xmap <C-a>
function ble/widget/vi_xmap/increment { ble/widget/vi_xmap/increment.impl increase; }
# xmap <C-x>
function ble/widget/vi_xmap/decrement { ble/widget/vi_xmap/increment.impl decrease; }
# xmap g<C-a>
function ble/widget/vi_xmap/progressive-increment { ble/widget/vi_xmap/increment.impl progressive:increase; }
# xmap g<C-x>
function ble/widget/vi_xmap/progressive-decrement { ble/widget/vi_xmap/increment.impl progressive:decrease; }
#--------------------------------------
function ble-decode/keymap:vi_xmap/define {
ble/keymap:vi/set-up-command-map
ble-bind -f __default__ vi-command/decompose-meta
ble-bind -f 'ESC' vi_xmap/exit
ble-bind -f 'C-[' vi_xmap/exit
ble-bind -f 'C-c' vi_xmap/cancel
ble-bind -f '"' vi-command/register
ble-bind -f a vi-command/text-object-outer
ble-bind -f i vi-command/text-object-inner
ble-bind -f 'C-\ C-n' vi_xmap/cancel
ble-bind -f 'C-\ C-g' vi_xmap/cancel
ble-bind -f v vi_xmap/switch-to-charwise
ble-bind -f V vi_xmap/switch-to-linewise
ble-bind -f C-v vi_xmap/switch-to-blockwise
ble-bind -f C-q vi_xmap/switch-to-blockwise
ble-bind -f 'g v' vi-command/previous-visual-area
ble-bind -f C-g vi_xmap/switch-to-select
ble-bind -f o vi_xmap/exchange-points
ble-bind -f O vi_xmap/exchange-boundaries
ble-bind -f '~' 'vi-command/operator toggle_case'
ble-bind -f 'u' 'vi-command/operator u'
ble-bind -f 'U' 'vi-command/operator U'
ble-bind -f 's' 'vi-command/operator c'
ble-bind -f 'x' 'vi-command/operator d'
ble-bind -f delete 'vi-command/operator d'
ble-bind -f r vi_xmap/visual-replace-char
ble-bind -f C vi_xmap/replace-block-lines
ble-bind -f D vi_xmap/delete-block-lines
ble-bind -f X vi_xmap/delete-block-lines
ble-bind -f S vi_xmap/delete-lines
ble-bind -f R vi_xmap/delete-lines
ble-bind -f Y vi_xmap/copy-block-or-lines
ble-bind -f J vi_xmap/connect-line-with-space
ble-bind -f 'g J' vi_xmap/connect-line
ble-bind -f I vi_xmap/insert-mode
ble-bind -f A vi_xmap/append-mode
ble-bind -f p vi_xmap/paste-after
ble-bind -f P vi_xmap/paste-before
ble-bind -f 'C-a' vi_xmap/increment
ble-bind -f 'C-x' vi_xmap/decrement
ble-bind -f 'g C-a' vi_xmap/progressive-increment
ble-bind -f 'g C-x' vi_xmap/progressive-decrement
ble-bind -f f1 vi_xmap/command-help
ble-bind -f K vi_xmap/command-help
}
function ble-decode/keymap:vi_smap/define {
ble-bind -f __default__ vi-command/decompose-meta
ble-bind -f 'ESC' vi_xmap/exit
ble-bind -f 'C-[' vi_xmap/exit
ble-bind -f 'C-c' vi_xmap/cancel
ble-bind -f 'C-\ C-n' nop
ble-bind -f 'C-\ C-n' vi_xmap/cancel
ble-bind -f 'C-\ C-g' vi_xmap/cancel
ble-bind -f C-v vi_xmap/switch-to-visual-blockwise
ble-bind -f C-q vi_xmap/switch-to-visual-blockwise
ble-bind -f C-g vi_xmap/switch-to-visual
ble-bind -f delete 'vi-command/operator d'
ble-bind -f 'C-?' 'vi-command/operator d'
ble-bind -f 'DEL' 'vi-command/operator d'
ble-bind -f 'C-h' 'vi-command/operator d'
ble-bind -f 'BS' 'vi-command/operator d'
#----------------------------------------------------------------------------
ble-bind -f __defchar__ vi_smap/self-insert
ble-bind -f paste_begin vi-command/bracketed-paste
ble-bind -f 'C-a' vi_xmap/increment
ble-bind -f 'C-x' vi_xmap/decrement
ble-bind -f f1 vi_xmap/command-help
ble-bind -c 'C-z' fg
#----------------------------------------------------------------------------
# motion, etc.
ble-bind -f home 'vi_smap/@nomarked vi-command/beginning-of-line'
ble-bind -f end 'vi_smap/@nomarked vi-command/forward-eol'
ble-bind -f C-m 'vi_smap/@nomarked vi-command/forward-first-non-space'
ble-bind -f RET 'vi_smap/@nomarked vi-command/forward-first-non-space'
ble-bind -f S-home 'vi-command/beginning-of-line'
ble-bind -f S-end 'vi-command/forward-eol'
ble-bind -f S-C-m 'vi-command/forward-first-non-space'
ble-bind -f S-RET 'vi-command/forward-first-non-space'
ble-bind -f C-right 'vi_smap/@nomarked vi-command/forward-vword'
ble-bind -f C-left 'vi_smap/@nomarked vi-command/backward-vword'
ble-bind -f S-C-right 'vi-command/forward-vword'
ble-bind -f S-C-left 'vi-command/backward-vword'
ble-bind -f left 'vi_smap/@nomarked vi-command/backward-char'
ble-bind -f right 'vi_smap/@nomarked vi-command/forward-char'
ble-bind -f 'C-?' 'vi_smap/@nomarked vi-command/backward-char wrap'
ble-bind -f 'DEL' 'vi_smap/@nomarked vi-command/backward-char wrap'
ble-bind -f 'C-h' 'vi_smap/@nomarked vi-command/backward-char wrap'
ble-bind -f 'BS' 'vi_smap/@nomarked vi-command/backward-char wrap'
ble-bind -f SP 'vi_smap/@nomarked vi-command/forward-char wrap'
ble-bind -f S-left 'vi-command/backward-char'
ble-bind -f S-right 'vi-command/forward-char'
ble-bind -f 'S-C-?' 'vi-command/backward-char wrap'
ble-bind -f 'S-DEL' 'vi-command/backward-char wrap'
ble-bind -f 'S-C-h' 'vi-command/backward-char wrap'
ble-bind -f 'S-BS' 'vi-command/backward-char wrap'
ble-bind -f S-SP 'vi-command/forward-char wrap'
ble-bind -f down 'vi_smap/@nomarked vi-command/forward-line'
ble-bind -f C-n 'vi_smap/@nomarked vi-command/forward-line'
ble-bind -f C-j 'vi_smap/@nomarked vi-command/forward-line'
ble-bind -f up 'vi_smap/@nomarked vi-command/backward-line'
ble-bind -f C-p 'vi_smap/@nomarked vi-command/backward-line'
ble-bind -f C-home 'vi_smap/@nomarked vi-command/first-nol'
ble-bind -f C-end 'vi_smap/@nomarked vi-command/last-eol'
ble-bind -f S-down 'vi-command/forward-line'
ble-bind -f S-C-n 'vi-command/forward-line'
ble-bind -f S-C-j 'vi-command/forward-line'
ble-bind -f S-up 'vi-command/backward-line'
ble-bind -f S-C-p 'vi-command/backward-line'
ble-bind -f S-C-home 'vi-command/first-nol'
ble-bind -f S-C-end 'vi-command/last-eol'
}
#------------------------------------------------------------------------------
# vi_imap
function ble/widget/vi_imap/__attach__ {
ble/keymap:vi/update-mode-indicator
return 0
}
function ble/widget/vi_imap/__detach__ {
ble/edit/info/default clear
ble/keymap:vi/clear-arg
ble/keymap:vi/search/clear-matched
return 0
}
function ble/widget/vi_imap/accept-single-line-or {
if ble-edit/is-single-complete-line; then
ble/keymap:vi/imap-repeat/reset
ble/widget/accept-line
else
ble/widget/"$@"
fi
}
function ble/widget/vi_imap/delete-region-or {
if [[ $_ble_edit_mark_active ]]; then
ble/keymap:vi/imap-repeat/reset
if ((_ble_edit_ind!=_ble_edit_mark)); then
ble/keymap:vi/undo/add more
ble/widget/delete-region
ble/keymap:vi/undo/add more
fi
else
ble/widget/"$@"
fi
}
function ble/widget/vi_imap/overwrite-mode {
ble-edit/content/clear-arg
if [[ $_ble_edit_overwrite_mode ]]; then
_ble_edit_overwrite_mode=
else
_ble_edit_overwrite_mode=${_ble_keymap_vi_insert_overwrite:-R}
fi
ble/keymap:vi/update-mode-indicator
return 0
}
# imap C-w
## @fn ble/widget/vi_imap/.locate-backward-vword
## 直前の単語 (vword) の開始点を特定します。
## @var[out] ret
function ble/widget/vi_imap/.locate-backward-word {
local space=$' \t' nl=$'\n'
local rex='('$_ble_keymap_vi_REX_WORD')['$space']*$|['$space']+$|'$nl'$'
[[ ${_ble_edit_str::_ble_edit_ind} =~ $rex ]] || return 1
((ret=_ble_edit_ind-${#BASH_REMATCH}))
return 0
}
function ble/widget/vi_imap/delete-backward-word {
if local ret; ble/widget/vi_imap/.locate-backward-word; then
if ((ret!=_ble_edit_ind)); then
ble/keymap:vi/undo/add more
ble/widget/.delete-range "$ret" "$_ble_edit_ind"
ble/keymap:vi/undo/add more
fi
return 0
else
ble/widget/.bell
return 1
fi
}
function ble/widget/vi_imap/kill-backward-word {
if local ret; ble/widget/vi_imap/.locate-backward-word; then
if ((ret!=_ble_edit_ind)); then
ble/keymap:vi/undo/add more
ble/widget/.kill-range "$ret" "$_ble_edit_ind"
ble/keymap:vi/undo/add more
fi
return 0
else
ble/widget/.bell
return 1
fi
}
function ble/widget/vi_imap/copy-backward-word {
if local ret; ble/widget/vi_imap/.locate-backward-word; then
if ((ret!=_ble_edit_ind)); then
ble/widget/.copy-range "$ret" "$_ble_edit_ind"
fi
return 0
else
ble/widget/.bell
return 1
fi
}
# imap C-q, C-v
function ble/widget/vi_imap/quoted-insert-char {
ble/keymap:vi/imap-repeat/pop
_ble_edit_mark_active=
_ble_decode_char__hook=ble/widget/vi_imap/quoted-insert-char.hook
return 147
}
function ble/widget/vi_imap/quoted-insert-char.hook {
ble/keymap:vi/imap/invoke-widget ble/widget/self-insert "$1"
}
function ble/widget/vi_imap/quoted-insert {
ble/keymap:vi/imap-repeat/pop
_ble_edit_mark_active=
_ble_decode_key__hook=ble/widget/vi_imap/quoted-insert.hook
return 147
}
function ble/widget/vi_imap/quoted-insert.hook {
ble/keymap:vi/imap/invoke-widget ble/widget/quoted-insert.hook "$1"
}
# bracketed paste mode
function ble/widget/vi_imap/bracketed-paste {
ble/keymap:vi/imap-repeat/pop
ble/widget/bracketed-paste
_ble_edit_bracketed_paste_proc=ble/widget/vi_imap/bracketed-paste.proc
return 147
}
function ble/widget/vi_imap/bracketed-paste.proc {
local WIDGET=ble/widget/batch-insert
local -a KEYS; KEYS=("$@")
ble/keymap:vi/imap-repeat/push
builtin eval -- "$WIDGET"
}
_ble_keymap_vi_bracketed_paste_mark_active=
function ble/widget/vi-command/bracketed-paste {
local ARG FLAG REG; ble/keymap:vi/get-arg 1 # discard args
_ble_keymap_vi_bracketed_paste_mark_active=$_ble_edit_mark_active
_ble_edit_mark_active=
ble/widget/bracketed-paste
_ble_edit_bracketed_paste_proc=ble/widget/vi-command/bracketed-paste.proc
return 147
}
function ble/widget/vi-command/bracketed-paste.proc {
if [[ $_ble_decode_keymap == vi_nmap ]]; then
local isbol index=$_ble_edit_ind
ble-edit/content/bolp && isbol=1
ble/decode/widget/call-interactively 'ble/widget/vi_nmap/append-mode' 97
[[ $isbol ]] && ((_ble_edit_ind=index)) # 行頭にいたときは戻る
ble/widget/vi_imap/bracketed-paste.proc "$@"
ble/keymap:vi/imap/invoke-widget \
ble/widget/vi_imap/normal-mode "$((_ble_decode_Ctrl|0x5b))"
elif [[ $_ble_decode_keymap == vi_[xs]map ]]; then
local _ble_edit_mark_active=$_ble_keymap_vi_bracketed_paste_mark_active
ble/decode/widget/call-interactively 'ble/widget/vi-command/operator c' 99 || return 1
ble/widget/vi_imap/bracketed-paste.proc "$@"
ble/keymap:vi/imap/invoke-widget \
ble/widget/vi_imap/normal-mode "$((_ble_decode_Ctrl|0x5b))"
elif [[ $_ble_decode_keymap == vi_omap ]]; then
ble/widget/vi_omap/cancel
ble/widget/.bell
return 1
else # vi_omap
ble/widget/.bell
return 1
fi
}
#------------------------------------------------------------------------------
# imap: C-k (digraph)
function ble/widget/vi_imap/insert-digraph.hook {
local -a KEYS; KEYS=("$1")
ble/widget/self-insert
}
function ble/widget/vi_imap/insert-digraph {
ble/decode/keymap/push vi_digraph
_ble_keymap_vi_digraph__hook=ble/widget/vi_imap/insert-digraph.hook
return 0
}
# imap: CR, LF (newline)
function ble/widget/vi_imap/newline {
local ret
ble-edit/content/find-logical-bol; local bol=$ret
ble-edit/content/find-non-space "$bol"; local nol=$ret
ble/widget/default/newline
((bol<nol)) && ble/widget/.insert-string "${_ble_edit_str:bol:nol-bol}"
return 0
}
# imap: C-h, DEL
function ble/widget/vi_imap/delete-backward-indent-or {
local rex=$'(^|\n)([ \t]+)$'
if [[ ${_ble_edit_str::_ble_edit_ind} =~ $rex ]]; then
local rematch2=${BASH_REMATCH[2]} # Note: for bash-3.1 ${#arr[n]} bug
if [[ $rematch2 ]]; then
ble/keymap:vi/undo/add more
ble/widget/.delete-range "$((_ble_edit_ind-${#rematch2}))" "$_ble_edit_ind"
ble/keymap:vi/undo/add more
fi
return 0
else
ble/widget/"$@"
fi
}
#------------------------------------------------------------------------------
function ble-decode/keymap:vi_imap/define {
#----------------------------------------------------------------------------
# common bindings
local ble_bind_nometa=1
ble-decode/keymap:safe/bind-common
ble-decode/keymap:safe/bind-history
#----------------------------------------------------------------------------
# from ble-decode/keymap:safe/define
ble-bind -f 'C-d' 'delete-region-or delete-forward-char-or-exit'
ble-bind -f 'SP' 'magic-space'
ble-bind -f '/' 'magic-slash'
ble-bind -f 'C-j' 'accept-line'
ble-bind -f 'C-RET' 'accept-line'
ble-bind -f 'C-m' 'accept-single-line-or-newline'
ble-bind -f 'RET' 'accept-single-line-or-newline'
ble-bind -f 'C-x C-e' 'edit-and-execute-command'
ble-bind -f 'C-g' 'bell'
ble-bind -f 'C-x C-g' 'bell'
ble-bind -f 'C-l' 'clear-screen'
ble-bind -f 'f1' 'command-help'
ble-bind -f 'C-x C-v' 'display-shell-version'
ble-bind -c 'C-z' 'fg'
# args
local key
for key in {,C-}{0..9}; do
ble-bind -f "$key" 'append-arg'
done
#----------------------------------------------------------------------------
# vi_imap modifications
ble-bind -f insert 'vi_imap/overwrite-mode'
# insert
ble-bind -f 'C-q' 'vi_imap/quoted-insert'
ble-bind -f 'C-v' 'vi_imap/quoted-insert'
ble-bind -f paste_begin 'vi_imap/bracketed-paste'
# charwise operations
ble-bind -f 'C-?' 'vi_imap/delete-region-or vi_imap/delete-backward-indent-or delete-backward-char'
ble-bind -f 'DEL' 'vi_imap/delete-region-or vi_imap/delete-backward-indent-or delete-backward-char'
ble-bind -f 'C-h' 'vi_imap/delete-region-or vi_imap/delete-backward-indent-or delete-backward-char'
ble-bind -f 'BS' 'vi_imap/delete-region-or vi_imap/delete-backward-indent-or delete-backward-char'
# wordwise operations
ble-bind -f 'C-w' 'vi_imap/kill-backward-word'
# complete
ble-decode/keymap:vi_imap/bind-complete
#----------------------------------------------------------------------------
# shell functions (from keymap emacs-standard)
ble-bind -f 'C-\' bell
ble-bind -f 'C-^' bell
#----------------------------------------------------------------------------
# vi bindings
ble-bind -f __attach__ vi_imap/__attach__
ble-bind -f __detach__ vi_imap/__detach__
ble-bind -f __default__ vi_imap/__default__
ble-bind -f __before_widget__ vi_imap/__before_widget__
ble-bind -f __line_limit__ __line_limit__
# Note: in the typical shells, [C-c] is used to discard the current line,
# which is implemented as 'discard-line'. The choice of ble.sh is to make it
# consistent with vi, i.e. drop to the normal mode by [C-c]
ble-bind -f 'ESC' 'vi_imap/normal-mode'
ble-bind -f 'C-[' 'vi_imap/normal-mode'
ble-bind -f 'C-c' 'vi_imap/normal-mode-without-insert-leave'
# Note: in the emacs editing mode, [C-o] has been 'accept-and-next', which
# might be more useful in the command line.
ble-bind -f 'C-o' 'vi_imap/single-command-mode'
# Note: these bindings also conflict with the typical binding in
# shells. [C-l] can be replaced by ESC or C-c, so we drop it. [C-k]
# conflicts with kill-forward-line. The digraph inputting is only needed
# when the terminal does not support it, so I think we don't need to support
# it by default.
# ble-bind -f 'C-l' vi_imap/normal-mode
# ble-bind -f 'C-k' vi_imap/insert-digraph
}
## @fn ble-decode/keymap:vi_imap/define-meta-bindings
## M- で始まるキーバインディングを定義します。
## ユーザから呼び出すための関数です。
function ble-decode/keymap:vi_imap/define-meta-bindings {
local ble_bind_keymap=vi_imap
#----------------------------------------------------------------------------
# from ble-decode/keymap:safe/define
ble-bind -f 'M-^' 'history-expand-line'
ble-bind -f 'C-M-l' 'redraw-line'
ble-bind -f 'M-#' 'insert-comment'
ble-bind -f 'M-C-e' 'shell-expand-line'
ble-bind -f 'M-&' 'tilde-expand'
ble-bind -f 'C-M-g' 'bell'
ble-bind -f 'M-z' 'zap-to-char'
#----------------------------------------------------------------------------
# from ble-decode/keymap:safe/bind-common
# from ble-decode/keymap:selection/bind-shift
ble-bind -f 'M-C-m' 'newline'
ble-bind -f 'M-RET' 'newline'
ble-bind -f 'M-SP' 'set-mark'
ble-bind -f 'M-w' 'copy-region-or copy-uword'
ble-bind -f 'M-y' 'yank-pop'
ble-bind -f 'M-S-y' 'yank-pop backward'
ble-bind -f 'M-Y' 'yank-pop backward'
ble-bind -f 'M-\' 'delete-horizontal-space'
ble-bind -f 'M-right' 'forward-sword'
ble-bind -f 'M-left' 'backward-sword'
ble-bind -f 'S-M-right' '@marked forward-sword'
ble-bind -f 'S-M-left' '@marked backward-sword'
ble-bind -f 'M-d' 'kill-forward-cword'
ble-bind -f 'M-h' 'kill-backward-cword'
ble-bind -f 'M-delete' 'copy-forward-sword'
ble-bind -f 'M-C-?' 'copy-backward-sword'
ble-bind -f 'M-DEL' 'copy-backward-sword'
ble-bind -f 'M-C-h' 'copy-backward-sword'
ble-bind -f 'M-BS' 'copy-backward-sword'
ble-bind -f 'M-f' 'forward-cword'
ble-bind -f 'M-b' 'backward-cword'
ble-bind -f 'M-F' '@marked forward-cword'
ble-bind -f 'M-B' '@marked backward-cword'
ble-bind -f 'M-S-f' '@marked forward-cword'
ble-bind -f 'M-S-b' '@marked backward-cword'
ble-bind -f 'M-c' 'capitalize-eword'
ble-bind -f 'M-l' 'downcase-eword'
ble-bind -f 'M-u' 'upcase-eword'
ble-bind -f 'M-t' 'transpose-ewords'
ble-bind -f 'M-m' 'non-space-beginning-of-line'
ble-bind -f 'M-S-m' '@marked non-space-beginning-of-line'
ble-bind -f 'M-M' '@marked non-space-beginning-of-line'
ble-bind -f 'M-x' 'vi-command/execute-named-command'
#----------------------------------------------------------------------------
# from ble-decode/keymap:safe/bind-history
ble-bind -f 'M-<' 'history-beginning'
ble-bind -f 'M->' 'history-end'
ble-bind -f 'M-.' 'insert-last-argument'
ble-bind -f 'M-_' 'insert-last-argument'
ble-bind -f 'M-C-y' 'insert-nth-argument'
#----------------------------------------------------------------------------
# from ble-decode/keymap:safe/bind-complete
ble-bind -f 'M-?' 'complete show_menu'
ble-bind -f 'M-*' 'complete insert_all'
ble-bind -f 'M-{' 'complete insert_braces'
ble-bind -f 'M-/' 'complete context=filename'
ble-bind -f 'M-~' 'complete context=username'
ble-bind -f 'M-$' 'complete context=variable'
ble-bind -f 'M-@' 'complete context=hostname'
ble-bind -f 'M-!' 'complete context=command'
ble-bind -f "M-'" 'sabbrev-expand'
ble-bind -f 'M-g' 'complete context=glob'
ble-bind -f 'M-C-i' 'complete context=dynamic-history'
ble-bind -f 'M-TAB' 'complete context=dynamic-history'
#----------------------------------------------------------------------------
# from ble-decode/keymap:safe/bind-arg
ble-bind -f 'M-0' 'append-arg'
ble-bind -f 'M-1' 'append-arg'
ble-bind -f 'M-2' 'append-arg'
ble-bind -f 'M-3' 'append-arg'
ble-bind -f 'M-4' 'append-arg'
ble-bind -f 'M-5' 'append-arg'
ble-bind -f 'M-6' 'append-arg'
ble-bind -f 'M-7' 'append-arg'
ble-bind -f 'M-8' 'append-arg'
ble-bind -f 'M-9' 'append-arg'
}
#------------------------------------------------------------------------------
# vi_cmap
_ble_keymap_vi_cmap_accept_hook=
_ble_keymap_vi_cmap_cancel_hook=
_ble_keymap_vi_cmap_before_widget=
# 既定の cmap 履歴
_ble_keymap_vi_cmap_history=()
_ble_keymap_vi_cmap_history_edit=()
_ble_keymap_vi_cmap_history_dirt=()
_ble_keymap_vi_cmap_history_index=0
function ble/keymap:vi/async-commandline-mode {
ble/edit/async-read-mode "$1" _ble_keymap_vi_cmap vi_cmap
ble/keymap:vi/update-mode-indicator
return 147
}
function ble/widget/vi_cmap/accept {
local hook=$_ble_keymap_vi_cmap_accept_hook
_ble_keymap_vi_cmap_accept_hook=
local ret
ble/edit/async-read-mode/accept
ble/keymap:vi/update-mode-indicator
if [[ $hook ]]; then
builtin eval -- "$hook \"\$ret\""
else
ble/keymap:vi/adjust-command-mode
return 0
fi
}
function ble/widget/vi_cmap/cancel {
_ble_keymap_vi_cmap_accept_hook=$_ble_keymap_vi_cmap_cancel_hook
ble/widget/vi_cmap/accept
}
function ble/widget/vi_cmap/__before_widget__ {
if [[ $_ble_keymap_vi_cmap_before_widget ]]; then
builtin eval -- "$_ble_keymap_vi_cmap_before_widget"
fi
}
function ble/widget/vi_cmap/__line_limit__.edit {
local content=$1
ble/widget/edit-and-execute-command.edit "$content" no-newline; local ext=$?
((ext==127)) && return "$ext"
ble-edit/content/reset "$ret"
ble/widget/vi_cmap/accept
}
function ble/widget/vi_cmap/__line_limit__ {
ble/widget/__line_limit__ vi_cmap/__line_limit__.edit
}
function ble-decode/keymap:vi_cmap/define {
#----------------------------------------------------------------------------
# common bindings + modifications
local ble_bind_nometa=
ble-decode/keymap:safe/bind-common
ble-decode/keymap:safe/bind-history
ble-decode/keymap:safe/bind-complete
#----------------------------------------------------------------------------
ble-bind -f __before_widget__ vi_cmap/__before_widget__
ble-bind -f __line_limit__ vi_cmap/__line_limit__
# accept/cancel
ble-bind -f 'ESC' vi_cmap/cancel
ble-bind -f 'C-[' vi_cmap/cancel
ble-bind -f 'C-c' vi_cmap/cancel
ble-bind -f 'C-m' vi_cmap/accept
ble-bind -f 'RET' vi_cmap/accept
ble-bind -f 'C-j' vi_cmap/accept
ble-bind -f 'C-g' bell
ble-bind -f 'C-x C-g' bell
ble-bind -f 'C-M-g' bell
# shell function
# ble-bind -f 'C-l' clear-screen
ble-bind -f 'C-l' redraw-line
ble-bind -f 'C-M-l' redraw-line
ble-bind -f 'C-x C-v' display-shell-version
# command-history
# ble-bind -f 'M-^' history-expand-line
# ble-bind -f 'SP' magic-space
# ble-bind -f '/' magic-slash
ble-bind -f 'C-\' bell
ble-bind -f 'C-^' bell
}
#------------------------------------------------------------------------------
function ble-decode/keymap:vi/initialize {
local fname_keymap_cache=$_ble_base_cache/keymap.vi
if [[ -s $fname_keymap_cache &&
$fname_keymap_cache -nt $_ble_base/lib/keymap.vi.sh &&
$fname_keymap_cache -nt $_ble_base/lib/init-cmap.sh ]]; then
source -- "$fname_keymap_cache" && return 0
fi
ble/edit/info/immediate-show text "ble.sh: updating cache/keymap.vi..."
{
ble/decode/keymap#load isearch dump
ble/decode/keymap#load nsearch dump
ble/decode/keymap#load vi_imap dump
ble/decode/keymap#load vi_nmap dump
ble/decode/keymap#load vi_omap dump
ble/decode/keymap#load vi_xmap dump
ble/decode/keymap#load vi_cmap dump
} 3>| "$fname_keymap_cache"
ble/edit/info/immediate-show text "ble.sh: updating cache/keymap.vi... done"
}
ble-decode/keymap:vi/initialize
ble_bind_keymap=vi_imap blehook/invoke keymap_load
ble_bind_keymap=vi_imap blehook/invoke keymap_vi_load
return 0
|