xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
<root> / src / cmd_handlers.c (79df65ef05950db4d9709947693bb747b87ccf1a) (152KiB) (mode 100644) [raw]
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
/* vifm
 * Copyright (C) 2001 Ken Steen.
 * Copyright (C) 2011 xaizek.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "cmd_handlers.h"

#include <regex.h>

#include <curses.h>

#include <sys/stat.h> /* gid_t uid_t */

#include <assert.h> /* assert() */
#include <ctype.h> /* isdigit() */
#include <errno.h> /* errno */
#include <limits.h> /* INT_MAX */
#include <signal.h>
#include <stddef.h> /* NULL size_t */
#include <stdio.h> /* snprintf() */
#include <stdlib.h> /* EXIT_SUCCESS atoi() free() realloc() */
#include <string.h> /* strchr() strcmp() strcspn() strcasecmp() strcpy()
                       strdup() strerror() strlen() strrchr() strspn() */
#include <wctype.h> /* iswspace() */
#include <wchar.h> /* wcslen() wcsncmp() */

#include "cfg/config.h"
#include "cfg/info.h"
#include "compat/fs_limits.h"
#include "compat/os.h"
#include "engine/abbrevs.h"
#include "engine/autocmds.h"
#include "engine/cmds.h"
#include "engine/keys.h"
#include "engine/mode.h"
#include "engine/options.h"
#include "engine/parsing.h"
#include "engine/text_buffer.h"
#include "engine/var.h"
#include "engine/variables.h"
#include "int/path_env.h"
#include "int/vim.h"
#include "lua/vlua.h"
#include "modes/normal.h"
#include "modes/dialogs/attr_dialog.h"
#include "modes/dialogs/change_dialog.h"
#include "modes/dialogs/msg_dialog.h"
#include "modes/dialogs/sort_dialog.h"
#include "menus/all.h"
#include "menus/menus.h"
#include "modes/modes.h"
#include "modes/wk.h"
#include "ui/color_scheme.h"
#include "ui/colors.h"
#include "ui/fileview.h"
#include "ui/quickview.h"
#include "ui/statusbar.h"
#include "ui/tabs.h"
#include "ui/ui.h"
#include "utils/env.h"
#include "utils/filter.h"
#include "utils/fs.h"
#include "utils/hist.h"
#include "utils/log.h"
#include "utils/matcher.h"
#include "utils/matchers.h"
#include "utils/path.h"
#include "utils/regexp.h"
#include "utils/str.h"
#include "utils/string_array.h"
#include "utils/trie.h"
#include "utils/utf8.h"
#include "utils/utils.h"
#include "background.h"
#include "bmarks.h"
#include "bracket_notation.h"
#include "cmd_actions.h"
#include "cmd_completion.h"
#include "cmd_core.h"
#include "compare.h"
#include "dir_stack.h"
#include "filelist.h"
#include "filetype.h"
#include "filtering.h"
#include "flist_hist.h"
#include "flist_pos.h"
#include "flist_sel.h"
#include "fops_cpmv.h"
#include "fops_misc.h"
#include "fops_put.h"
#include "fops_rename.h"
#include "instance.h"
#include "macros.h"
#include "marks.h"
#include "ops.h"
#include "opt_handlers.h"
#include "plugins.h"
#include "registers.h"
#include "running.h"
#include "sort.h"
#include "trash.h"
#include "undo.h"
#include "vifm.h"

static int goto_cmd(const cmd_info_t *cmd_info);
static int emark_cmd(const cmd_info_t *cmd_info);
static int alink_cmd(const cmd_info_t *cmd_info);
static int amap_cmd(const cmd_info_t *cmd_info);
static int anoremap_cmd(const cmd_info_t *cmd_info);
static int apropos_cmd(const cmd_info_t *cmd_info);
static int autocmd_cmd(const cmd_info_t *cmd_info);
static void aucmd_list_cb(const char event[], const char pattern[], int negated,
		const char action[], void *arg);
static void aucmd_action_handler(const char action[], void *arg);
static int aunmap_cmd(const cmd_info_t *cmd_info);
static int bmark_cmd(const cmd_info_t *cmd_info);
static int bmarks_cmd(const cmd_info_t *cmd_info);
static int bmgo_cmd(const cmd_info_t *cmd_info);
static int bmarks_do(const cmd_info_t *cmd_info, int go);
static char * make_tags_list(const cmd_info_t *cmd_info);
static char * args_to_csl(const cmd_info_t *cmd_info);
static int cabbrev_cmd(const cmd_info_t *cmd_info);
static int chistory_cmd(const cmd_info_t *cmd_info);
static int cnoreabbrev_cmd(const cmd_info_t *cmd_info);
static int handle_cabbrevs(const cmd_info_t *cmd_info, int no_remap);
static int list_abbrevs(const char prefix[]);
static int add_cabbrev(const cmd_info_t *cmd_info, int no_remap);
static int cd_cmd(const cmd_info_t *cmd_info);
static int cds_cmd(const cmd_info_t *cmd_info);
static int change_cmd(const cmd_info_t *cmd_info);
static int chmod_cmd(const cmd_info_t *cmd_info);
#ifndef _WIN32
static int chown_cmd(const cmd_info_t *cmd_info);
#endif
static int clone_cmd(const cmd_info_t *cmd_info);
static int cmap_cmd(const cmd_info_t *cmd_info);
static int cnoremap_cmd(const cmd_info_t *cmd_info);
static int copy_cmd(const cmd_info_t *cmd_info);
static int cquit_cmd(const cmd_info_t *cmd_info);
static int cunabbrev_cmd(const cmd_info_t *cmd_info);
static int colorscheme_cmd(const cmd_info_t *cmd_info);
static int is_colorscheme_assoc_form(const cmd_info_t *cmd_info);
static int assoc_colorscheme(const char name[], const char path[]);
static void set_colorscheme(char *names[], int count);
static int command_cmd(const cmd_info_t *cmd_info);
static int compare_cmd(const cmd_info_t *cmd_info);
static int copen_cmd(const cmd_info_t *cmd_info);
static int parse_compare_properties(const cmd_info_t *cmd_info, CompareType *ct,
		ListType *lt, int *flags);
static int cunmap_cmd(const cmd_info_t *cmd_info);
static int delete_cmd(const cmd_info_t *cmd_info);
static int delmarks_cmd(const cmd_info_t *cmd_info);
static int delbmarks_cmd(const cmd_info_t *cmd_info);
static void remove_bmark(const char path[], const char tags[], time_t timestamp,
		void *arg);
static char * get_bmark_dir(const cmd_info_t *cmd_info);
static char * make_bmark_path(const char path[]);
static int delsession_cmd(const cmd_info_t *cmd_info);
static int dirs_cmd(const cmd_info_t *cmd_info);
static int dmap_cmd(const cmd_info_t *cmd_info);
static int dnoremap_cmd(const cmd_info_t *cmd_info);
static int dialog_map(const cmd_info_t *cmd_info, int no_remap);
static int dunmap_cmd(const cmd_info_t *cmd_info);
static int echo_cmd(const cmd_info_t *cmd_info);
static int edit_cmd(const cmd_info_t *cmd_info);
static int else_cmd(const cmd_info_t *cmd_info);
static int elseif_cmd(const cmd_info_t *cmd_info);
static int empty_cmd(const cmd_info_t *cmd_info);
static int endif_cmd(const cmd_info_t *cmd_info);
static int exe_cmd(const cmd_info_t *cmd_info);
static char * try_eval_args(const cmd_info_t *cmd_info);
static int file_cmd(const cmd_info_t *cmd_info);
static int filetype_cmd(const cmd_info_t *cmd_info);
static int filextype_cmd(const cmd_info_t *cmd_info);
static int fileviewer_cmd(const cmd_info_t *cmd_info);
static int add_assoc(const cmd_info_t *cmd_info, int viewer, int for_x);
static int filter_cmd(const cmd_info_t *cmd_info);
static int update_filter(view_t *view, const cmd_info_t *cmd_info);
static void display_filters_info(const view_t *view);
static char * get_filter_info(const char name[], const filter_t *filter);
static char * get_matcher_info(const char name[], const matcher_t *matcher);
static int set_view_filter(view_t *view, const char filter[],
		const char fallback[], int invert);
static int get_filter_inversion_state(const cmd_info_t *cmd_info);
static int find_cmd(const cmd_info_t *cmd_info);
static int finish_cmd(const cmd_info_t *cmd_info);
static int goto_path_cmd(const cmd_info_t *cmd_info);
static int grep_cmd(const cmd_info_t *cmd_info);
static int help_cmd(const cmd_info_t *cmd_info);
static int hideui_cmd(const cmd_info_t *cmd_info);
static int highlight_cmd(const cmd_info_t *cmd_info);
static int highlight_clear(const cmd_info_t *cmd_info);
static int highlight_column(const cmd_info_t *cmd_info);
static int find_column_id(const char arg[]);
static int highlight_file(const cmd_info_t *cmd_info);
static void display_file_highlights(const matchers_t *matchers);
static int highlight_group(const cmd_info_t *cmd_info);
static const char * get_all_highlights(void);
static const char * get_group_str(int group, const col_attr_t *col);
static const char * get_column_hi_str(int index, const col_attr_t *col);
static const char * get_file_hi_str(const matchers_t *matchers,
		const col_attr_t *col);
static const char * get_hi_str(const char title[], const col_attr_t *col);
static int parse_highlight_attrs(const cmd_info_t *cmd_info,
		col_attr_t *color);
static int try_parse_cterm_color(const char str[], int is_fg,
		col_attr_t *color);
static int try_parse_gui_color(const char str[], int *color);
static int parse_color_name_value(const char str[], int fg, int *attr);
static int is_default_color(const char str[]);
static int get_attrs(const char text[], int *combine_attrs);
static int history_cmd(const cmd_info_t *cmd_info);
static int histnext_cmd(const cmd_info_t *cmd_info);
static int histprev_cmd(const cmd_info_t *cmd_info);
static int if_cmd(const cmd_info_t *cmd_info);
static int eval_if_condition(const cmd_info_t *cmd_info);
static int invert_cmd(const cmd_info_t *cmd_info);
static void print_inversion_state(char state_type);
static void invert_state(char state_type);
static int jobs_cmd(const cmd_info_t *cmd_info);
static int keepsel_cmd(const cmd_info_t *cmd_info);
static int let_cmd(const cmd_info_t *cmd_info);
static int locate_cmd(const cmd_info_t *cmd_info);
static int ls_cmd(const cmd_info_t *cmd_info);
static int lstrash_cmd(const cmd_info_t *cmd_info);
static int map_cmd(const cmd_info_t *cmd_info);
static int mark_cmd(const cmd_info_t *cmd_info);
static int marks_cmd(const cmd_info_t *cmd_info);
#ifndef _WIN32
static int media_cmd(const cmd_info_t *cmd_info);
#endif
static int messages_cmd(const cmd_info_t *cmd_info);
static int mkdir_cmd(const cmd_info_t *cmd_info);
static int mmap_cmd(const cmd_info_t *cmd_info);
static int mnoremap_cmd(const cmd_info_t *cmd_info);
static int move_cmd(const cmd_info_t *cmd_info);
static int cpmv_cmd(const cmd_info_t *cmd_info, int move);
static int munmap_cmd(const cmd_info_t *cmd_info);
static int nmap_cmd(const cmd_info_t *cmd_info);
static int nnoremap_cmd(const cmd_info_t *cmd_info);
static int nohlsearch_cmd(const cmd_info_t *cmd_info);
static int noremap_cmd(const cmd_info_t *cmd_info);
static int map_or_remap(const cmd_info_t *cmd_info, int no_remap);
static int normal_cmd(const cmd_info_t *cmd_info);
static int nunmap_cmd(const cmd_info_t *cmd_info);
static int only_cmd(const cmd_info_t *cmd_info);
static int plugin_cmd(const cmd_info_t *cmd_info);
static int plugins_cmd(const cmd_info_t *cmd_info);
static int popd_cmd(const cmd_info_t *cmd_info);
static int pushd_cmd(const cmd_info_t *cmd_info);
static int put_cmd(const cmd_info_t *cmd_info);
static int pwd_cmd(const cmd_info_t *cmd_info);
static int qmap_cmd(const cmd_info_t *cmd_info);
static int qnoremap_cmd(const cmd_info_t *cmd_info);
static int qunmap_cmd(const cmd_info_t *cmd_info);
static int redraw_cmd(const cmd_info_t *cmd_info);
static int regedit_cmd(const cmd_info_t *cmd_info);
static int registers_cmd(const cmd_info_t *cmd_info);
static int regular_cmd(const cmd_info_t *cmd_info);
static int rename_cmd(const cmd_info_t *cmd_info);
static int restart_cmd(const cmd_info_t *cmd_info);
static int restore_cmd(const cmd_info_t *cmd_info);
static int rlink_cmd(const cmd_info_t *cmd_info);
static int link_cmd(const cmd_info_t *cmd_info, int absolute);
static int parse_cpmv_flags(int *argc, char ***argv);
static int screen_cmd(const cmd_info_t *cmd_info);
static int select_cmd(const cmd_info_t *cmd_info);
static int session_cmd(const cmd_info_t *cmd_info);
static int switch_to_a_session(const char session_name[]);
static int restart_into_session(const char session[], int full);
static int set_cmd(const cmd_info_t *cmd_info);
static int setlocal_cmd(const cmd_info_t *cmd_info);
static int setglobal_cmd(const cmd_info_t *cmd_info);
static int shell_cmd(const cmd_info_t *cmd_info);
static int siblnext_cmd(const cmd_info_t *cmd_info);
static int siblprev_cmd(const cmd_info_t *cmd_info);
static int sort_cmd(const cmd_info_t *cmd_info);
static int source_cmd(const cmd_info_t *cmd_info);
static int split_cmd(const cmd_info_t *cmd_info);
static int stop_cmd(const cmd_info_t *cmd_info);
static int substitute_cmd(const cmd_info_t *cmd_info);
static int sync_cmd(const cmd_info_t *cmd_info);
static int sync_selectively(const cmd_info_t *cmd_info);
static int parse_sync_properties(const cmd_info_t *cmd_info, int *location,
		int *cursor_pos, int *local_options, int *filters, int *filelist,
		int *tree);
static void sync_location(const char path[], int cv, int sync_cursor_pos,
		int sync_filters, int tree);
static void sync_local_opts(int defer_slow);
static void sync_filters(void);
static int tabclose_cmd(const cmd_info_t *cmd_info);
static int tabmove_cmd(const cmd_info_t *cmd_info);
static int tabname_cmd(const cmd_info_t *cmd_info);
static int tabnew_cmd(const cmd_info_t *cmd_info);
static int tabnext_cmd(const cmd_info_t *cmd_info);
static int tabonly_cmd(const cmd_info_t *cmd_info);
static int tabprevious_cmd(const cmd_info_t *cmd_info);
static int touch_cmd(const cmd_info_t *cmd_info);
static int get_at(const view_t *view, const cmd_info_t *cmd_info);
static int tr_cmd(const cmd_info_t *cmd_info);
static int trashes_cmd(const cmd_info_t *cmd_info);
static int tree_cmd(const cmd_info_t *cmd_info);
static int parse_tree_properties(const cmd_info_t *cmd_info, int *depth);
static int undolist_cmd(const cmd_info_t *cmd_info);
static int unlet_cmd(const cmd_info_t *cmd_info);
static int unmap_cmd(const cmd_info_t *cmd_info);
static int unselect_cmd(const cmd_info_t *cmd_info);
static int view_cmd(const cmd_info_t *cmd_info);
static int vifm_cmd(const cmd_info_t *cmd_info);
static int vmap_cmd(const cmd_info_t *cmd_info);
static int vnoremap_cmd(const cmd_info_t *cmd_info);
#ifdef _WIN32
static int volumes_cmd(const cmd_info_t *cmd_info);
#endif
static int vsplit_cmd(const cmd_info_t *cmd_info);
static int do_split(const cmd_info_t *cmd_info, SPLIT orientation);
static int do_map(const cmd_info_t *cmd_info, const char map_type[], int mode,
		int no_remap);
static int parse_map_args(const char **args);
static int vunmap_cmd(const cmd_info_t *cmd_info);
static int do_unmap(const char *keys, int mode);
static int wincmd_cmd(const cmd_info_t *cmd_info);
static int windo_cmd(const cmd_info_t *cmd_info);
static int winrun_cmd(const cmd_info_t *cmd_info);
static int winrun(view_t *view, const char cmd[]);
static int write_cmd(const cmd_info_t *cmd_info);
static int qall_cmd(const cmd_info_t *cmd_info);
static int quit_cmd(const cmd_info_t *cmd_info);
static int wq_cmd(const cmd_info_t *cmd_info);
static int wqall_cmd(const cmd_info_t *cmd_info);
static int yank_cmd(const cmd_info_t *cmd_info);
static int get_reg_and_count(const cmd_info_t *cmd_info, int *reg);
static int get_reg(const char arg[], int *reg);
static int usercmd_cmd(const cmd_info_t* cmd_info);
static int parse_bg_mark(char cmd[]);

const cmd_add_t cmds_list[] = {
	{ .name = "",                  .abbr = NULL,    .id = COM_GOTO,
	  .descr = "put cursor at specific line",
	  .flags = HAS_RANGE | HAS_COMMENT,
	  .handler = &goto_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "!",                 .abbr = NULL,    .id = COM_EXECUTE,
	  .descr = "execute external command",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_BG_FLAG | HAS_MACROS_FOR_SHELL
	         | HAS_SELECTION_SCOPE,
	  .handler = &emark_cmd,       .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "alink",             .abbr = NULL,    .id = COM_ALINK,
	  .descr = "create absolute links",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_QUOTED_ARGS | HAS_COMMENT
	         | HAS_QMARK_NO_ARGS | HAS_SELECTION_SCOPE,
	  .handler = &alink_cmd,       .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "amap",              .abbr = NULL,    .id = COM_AMAP,
	  .descr = "map keys in navigation mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &amap_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "anoremap",          .abbr = NULL,    .id = COM_ANOREMAP,
	  .descr = "noremap keys in navigation mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &anoremap_cmd,    .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "apropos",           .abbr = NULL,    .id = -1,
	  .descr = "query apropos results",
	  .flags = 0,
	  .handler = &apropos_cmd,     .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "aunmap",            .abbr = NULL,    .id = -1,
	  .descr = "unmap user keys in navigation mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &aunmap_cmd,      .min_args = 1,   .max_args = 1, },
	{ .name = "autocmd",           .abbr = "au",    .id = COM_AUTOCMD,
	  .descr = "manage autocommands",
	  .flags = HAS_EMARK | HAS_QUOTED_ARGS,
	  .handler = &autocmd_cmd,     .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "bmark",             .abbr = NULL,    .id = COM_BMARKS,
	  .descr = "set bookmark",
	  .flags = HAS_EMARK | HAS_QUOTED_ARGS | HAS_COMMENT,
	  .handler = &bmark_cmd,       .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "bmarks",            .abbr = NULL,    .id = COM_BMARKS,
	  .descr = "list bookmarks",
	  .flags = HAS_COMMENT,
	  .handler = &bmarks_cmd,      .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "bmgo",              .abbr = NULL,    .id = COM_BMARKS,
	  .descr = "navigate to a bookmark",
	  .flags = HAS_COMMENT,
	  .handler = &bmgo_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "cabbrev",           .abbr = "ca",    .id = COM_CABBR,
	  .descr = "display/create cmdline abbrevs",
	  .flags = 0,
	  .handler = &cabbrev_cmd,     .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "chistory",          .abbr = "chi",   .id = -1,
	  .descr = "display history of menus",
	  .flags = 0,
	  .handler = &chistory_cmd,    .min_args = 0,   .max_args = 0, },
	{ .name = "cnoreabbrev",       .abbr = "cnorea", .id = COM_CABBR,
	  .descr = "display/create noremap cmdline abbrevs",
	  .flags = 0,
	  .handler = &cnoreabbrev_cmd, .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "cd",                .abbr = NULL,    .id = COM_CD,
	  .descr = "navigate to a directory",
	  .flags = HAS_EMARK | HAS_QUOTED_ARGS | HAS_COMMENT | HAS_MACROS_FOR_CMD
	         | HAS_ENVVARS,
	  .handler = &cd_cmd,          .min_args = 0,   .max_args = 2, },
	{ .name = "cds",               .abbr = NULL,    .id = COM_CDS,
	  .descr = "navigate to path obtained by substitution in current path",
	  .flags = HAS_EMARK | HAS_REGEXP_ARGS | HAS_CUST_SEP,
	  .handler = &cds_cmd,         .min_args = 2,   .max_args = 3, },
	{ .name = "change",            .abbr = "c",     .id = -1,
	  .descr = "change file traits",
	  .flags = HAS_COMMENT,
	  .handler = &change_cmd,      .min_args = 0,   .max_args = 0, },
#ifndef _WIN32
	{ .name = "chmod",             .abbr = NULL,    .id = -1,
	  .descr = "change permissions",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_COMMENT | HAS_SELECTION_SCOPE,
	  .handler = &chmod_cmd,       .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "chown",             .abbr = NULL,    .id = COM_CHOWN,
	  .descr = "change owner/group",
	  .flags = HAS_RANGE | HAS_COMMENT | HAS_SELECTION_SCOPE,
	  .handler = &chown_cmd,       .min_args = 0,   .max_args = 1, },
#else
	{ .name = "chmod",             .abbr = NULL,    .id = -1,
	  .descr = "change attributes",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_COMMENT | HAS_SELECTION_SCOPE,
	  .handler = &chmod_cmd,       .min_args = 0,   .max_args = 0, },
#endif
	{ .name = "clone",             .abbr = NULL,    .id = COM_CLONE,
	  .descr = "clone selection",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_QUOTED_ARGS | HAS_COMMENT
	         | HAS_QMARK_NO_ARGS | HAS_MACROS_FOR_CMD | HAS_SELECTION_SCOPE,
	  .handler = &clone_cmd,       .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "cmap",              .abbr = "cm",    .id = COM_CMAP,
	  .descr = "map keys in cmdline mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &cmap_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "cnoremap",          .abbr = "cno",   .id = COM_CNOREMAP,
	  .descr = "noremap keys in cmdline mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &cnoremap_cmd,    .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "colorscheme",       .abbr = "colo",  .id = COM_COLORSCHEME,
	  .descr = "display/select color schemes",
	  .flags = HAS_QUOTED_ARGS | HAS_COMMENT | HAS_QMARK_NO_ARGS,
	  .handler = &colorscheme_cmd, .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "command",           .abbr = "com",   .id = COM_COMMAND,
	  .descr = "display/define :commands",
	  .flags = HAS_EMARK,
	  .handler = &command_cmd,     .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "compare",           .abbr = NULL,    .id = COM_COMPARE,
	  .descr = "compare directories in two panes",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &compare_cmd,     .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "copen",             .abbr = "cope",  .id = -1,
	  .descr = "reopen last displayed navigation menu",
	  .flags = HAS_COMMENT,
	  .handler = &copen_cmd,       .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "copy",              .abbr = "co",    .id = COM_COPY,
	  .descr = "copy files",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_BG_FLAG | HAS_QUOTED_ARGS | HAS_COMMENT
	         | HAS_QMARK_NO_ARGS | HAS_SELECTION_SCOPE,
	  .handler = &copy_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "cquit",             .abbr = "cq",    .id = -1,
	  .descr = "quit with error",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &cquit_cmd,       .min_args = 0,   .max_args = 0, },
	{ .name = "cunabbrev",         .abbr = "cuna",  .id = COM_CABBR,
	  .descr = "remove cmdline abbrev",
	  .flags = 0,
	  .handler = &cunabbrev_cmd,   .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "cunmap",            .abbr = "cu",    .id = -1,
	  .descr = "unmap user keys in cmdline mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &cunmap_cmd,      .min_args = 1,   .max_args = 1, },
	{ .name = "delete",            .abbr = "d",     .id = -1,
	  .descr = "delete files",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_BG_FLAG | HAS_SELECTION_SCOPE,
	  .handler = &delete_cmd,      .min_args = 0,   .max_args = 2, },
	{ .name = "delmarks",          .abbr = "delm",  .id = -1,
	  .descr = "delete marks",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &delmarks_cmd,    .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "delbmarks",         .abbr = NULL,    .id = COM_DELBMARKS,
	  .descr = "delete bookmarks",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &delbmarks_cmd,   .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "delsession",        .abbr = NULL,    .id = COM_DELSESSION,
	  .descr = "remove a session",
	  .flags = HAS_COMMENT,
	  .handler = &delsession_cmd,  .min_args = 1,   .max_args = 1, },
	{ .name = "display",           .abbr = "di",    .id = -1,
	  .descr = "display registers",
	  .flags = 0,
	  .handler = &registers_cmd,   .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "dirs",              .abbr = NULL,    .id = -1,
	  .descr = "display directory stack",
	  .flags = HAS_COMMENT,
	  .handler = &dirs_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "dmap",              .abbr = NULL,    .id = COM_DMAP,
	  .descr = "map keys in dialog modes",
	  .flags = HAS_RAW_ARGS,
	  .handler = &dmap_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "dnoremap",          .abbr = NULL,    .id = COM_DNOREMAP,
	  .descr = "noremap keys in dialog modes",
	  .flags = HAS_RAW_ARGS,
	  .handler = &dnoremap_cmd,    .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "dunmap",            .abbr = NULL,    .id = -1,
	  .descr = "unmap user keys in dialog modes",
	  .flags = HAS_RAW_ARGS,
	  .handler = &dunmap_cmd,      .min_args = 1,   .max_args = 1, },
	{ .name = "echo",             .abbr = "ec",     .id = COM_ECHO,
	  .descr = "eval and print expressions",
	  .flags = 0,
	  .handler = &echo_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "edit",              .abbr = "e",     .id = COM_EDIT,
	  .descr = "edit files",
	  .flags = HAS_RANGE | HAS_QUOTED_ARGS | HAS_COMMENT | HAS_MACROS_FOR_CMD
	         | HAS_SELECTION_SCOPE | HAS_ENVVARS,
	  .handler = &edit_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "else",              .abbr = "el",    .id = COM_ELSE_STMT,
	  .descr = "start alternative control-flow",
	  .flags = HAS_COMMENT,
	  .handler = &else_cmd,        .min_args = 0,   .max_args = 0, },
	/* engine/parsing unit handles comments to resolve parsing ambiguity. */
	{ .name = "elseif",            .abbr = "elsei", .id = COM_ELSEIF_STMT,
	  .descr = "conditional control-flow branching",
	  .flags = 0,
	  .handler = &elseif_cmd,      .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "empty",             .abbr = NULL,    .id = -1,
	  .descr = "start emptying trashes in background",
	  .flags = HAS_COMMENT,
	  .handler = &empty_cmd,       .min_args = 0,   .max_args = 0, },
	{ .name = "endif",             .abbr = "en",    .id = COM_ENDIF_STMT,
	  .descr = "if-else construction terminator",
	  .flags = HAS_COMMENT,
	  .handler = &endif_cmd,       .min_args = 0,   .max_args = 0, },
	{ .name = "execute",           .abbr = "exe",   .id = COM_EXE,
	  .descr = "execute expressions as :commands",
	  .flags = 0,
	  .handler = &exe_cmd,         .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "exit",              .abbr = "exi",   .id = -1,
	  .descr = "exit the application",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &quit_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "file",              .abbr = "f",     .id = COM_FILE,
	  .descr = "display/apply file associations",
	  .flags = HAS_BG_FLAG | HAS_COMMENT,
	  .handler = &file_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "filetype",          .abbr = "filet", .id = COM_FILETYPE,
	  .descr = "display/define file associations",
	  .flags = 0,
	  .handler = &filetype_cmd,    .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "fileviewer",        .abbr = "filev", .id = COM_FILEVIEWER,
	  .descr = "display/define file viewers",
	  .flags = 0,
	  .handler = &fileviewer_cmd,  .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "filextype",         .abbr = "filex", .id = COM_FILEXTYPE,
	  .descr = "display/define file associations in X",
	  .flags = 0,
	  .handler = &filextype_cmd,   .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "filter",            .abbr = NULL,    .id = COM_FILTER,
	  .descr = "set/reset file filter",
	  .flags = HAS_EMARK | HAS_REGEXP_ARGS | HAS_QMARK_NO_ARGS,
	  .handler = &filter_cmd,      .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "find",              .abbr = "fin",   .id = COM_FIND,
	  .descr = "query find results",
	  .flags = HAS_RANGE | HAS_QUOTED_ARGS | HAS_MACROS_FOR_CMD
	         | HAS_SELECTION_SCOPE,
	  .handler = &find_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "finish",            .abbr = "fini",  .id = -1,
	  .descr = "stop script processing",
	  .flags = HAS_COMMENT,
	  .handler = &finish_cmd,      .min_args = 0,   .max_args = 0, },
	{ .name = "goto",              .abbr = "go",    .id = COM_GOTO_PATH,
	  .descr = "navigate to specified file/directory",
	  .flags = HAS_ENVVARS | HAS_COMMENT | HAS_MACROS_FOR_CMD | HAS_QUOTED_ARGS,
	  .handler = &goto_path_cmd,   .min_args = 1,   .max_args = 1, },
	{ .name = "grep",              .abbr = "gr",    .id = COM_GREP,
	  .descr = "query grep results",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_SELECTION_SCOPE,
	  .handler = &grep_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "help",              .abbr = "h",     .id = COM_HELP,
	  .descr = "display help",
	  .flags = HAS_QUOTED_ARGS,
	  .handler = &help_cmd,        .min_args = 0,   .max_args = 1, },
	{ .name = "hideui",            .abbr = NULL,    .id = -1,
	  .descr = "hide interface to show previous commands' output",
	  .flags = HAS_COMMENT,
	  .handler = &hideui_cmd,      .min_args = 0,   .max_args = 0, },
	{ .name = "highlight",         .abbr = "hi",    .id = COM_HIGHLIGHT,
	  .descr = "display/define TUI highlighting",
	  .flags = HAS_COMMENT,
	  .handler = &highlight_cmd,   .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "history",           .abbr = "his",   .id = COM_HISTORY,
	  .descr = "display/use history items",
	  .flags = HAS_QUOTED_ARGS | HAS_COMMENT,
	  .handler = &history_cmd,     .min_args = 0,   .max_args = 1, },
	{ .name = "histnext",          .abbr = NULL,    .id = -1,
	  .descr = "go forward through directory history",
	  .flags = HAS_COMMENT,
	  .handler = &histnext_cmd,    .min_args = 0,   .max_args = 0, },
	{ .name = "histprev",          .abbr = NULL,    .id = -1,
	  .descr = "go backward through directory history",
	  .flags = HAS_COMMENT,
	  .handler = &histprev_cmd,    .min_args = 0,   .max_args = 0, },
	/* engine/parsing unit handles comments to resolve parsing ambiguity. */
	{ .name = "if",                .abbr = NULL,    .id = COM_IF_STMT,
	  .descr = "start conditional statement",
	  .flags = 0,
	  .handler = &if_cmd,          .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "invert",            .abbr = NULL,    .id = COM_INVERT,
	  .descr = "invert filter/selection/sorting",
	  .flags = HAS_COMMENT | HAS_QMARK_WITH_ARGS,
	  .handler = &invert_cmd,      .min_args = 0,   .max_args = 1, },
	{ .name = "jobs",              .abbr = NULL,    .id = -1,
	  .descr = "display active jobs",
	  .flags = HAS_COMMENT,
	  .handler = &jobs_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "keepsel",           .abbr = NULL,    .id = COM_KEEPSEL,
	  .descr = "preserve selection during :command by default",
	  .flags = 0,
	  .handler = &keepsel_cmd,     .min_args = 0,   .max_args = NOT_DEF, },
	/* engine/parsing unit handles comments to resolve parsing ambiguity. */
	{ .name = "let",               .abbr = NULL,    .id = COM_LET,
	  .descr = "assign variables",
	  .flags = 0,
	  .handler = &let_cmd,         .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "locate",            .abbr = NULL,    .id = -1,
	  .descr = "query locate results",
	  .flags = 0,
	  .handler = &locate_cmd,      .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "ls",                .abbr = NULL,    .id = -1,
	  .descr = "list terminal multiplexer windows",
	  .flags = HAS_COMMENT,
	  .handler = &ls_cmd,          .min_args = 0,   .max_args = 0, },
	{ .name = "lstrash",           .abbr = NULL,    .id = -1,
	  .descr = "list files in trashes",
	  .flags = HAS_COMMENT,
	  .handler = &lstrash_cmd,     .min_args = 0,   .max_args = 0, },
	{ .name = "map",               .abbr = NULL,    .id = COM_MAP,
	  .descr = "map keys in normal and visual modes",
	  .flags = HAS_EMARK | HAS_RAW_ARGS,
	  .handler = &map_cmd,         .min_args = 2,   .max_args = NOT_DEF, },
	{ .name = "mark",              .abbr = "ma",    .id = -1,
	  .descr = "set mark",
	  .flags = HAS_RANGE | HAS_QUOTED_ARGS | HAS_COMMENT | HAS_QMARK_WITH_ARGS
	         | HAS_MACROS_FOR_CMD,
	  .handler = &mark_cmd,        .min_args = 1,   .max_args = 3, },
	{ .name = "marks",             .abbr = NULL,    .id = -1,
	  .descr = "display marks",
	  .flags = HAS_COMMENT,
	  .handler = &marks_cmd,       .min_args = 0,   .max_args = NOT_DEF, },
#ifndef _WIN32
	{ .name = "media",             .abbr = NULL,    .id = -1,
	  .descr = "list and manage media devices",
	  .flags = HAS_COMMENT,
	  .handler = &media_cmd,       .min_args = 0,   .max_args = 0, },
#endif
	{ .name = "messages",          .abbr = "mes",   .id = -1,
	  .descr = "display previous status bar messages",
	  .flags = HAS_COMMENT,
	  .handler = &messages_cmd,    .min_args = 0,   .max_args = 0, },
	{ .name = "mkdir",             .abbr = NULL,    .id = COM_MKDIR,
	  .descr = "create directories",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_QUOTED_ARGS | HAS_COMMENT
	         | HAS_MACROS_FOR_CMD,
	  .handler = &mkdir_cmd,       .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "mmap",              .abbr = "mm",    .id = COM_MMAP,
	  .descr = "map keys in menu mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &mmap_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "mnoremap",          .abbr = "mno",   .id = COM_MNOREMAP,
	  .descr = "noremap keys in menu mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &mnoremap_cmd,    .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "move",              .abbr = "m",     .id = COM_MOVE,
	  .descr = "move files",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_BG_FLAG | HAS_QUOTED_ARGS | HAS_COMMENT
	         | HAS_QMARK_NO_ARGS | HAS_SELECTION_SCOPE,
	  .handler = &move_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "munmap",            .abbr = "mu",    .id = -1,
	  .descr = "unmap user keys in menu mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &munmap_cmd,      .min_args = 1,   .max_args = 1, },
	{ .name = "nmap",              .abbr = "nm",    .id = COM_NMAP,
	  .descr = "map keys in normal mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &nmap_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "nnoremap",          .abbr = "nn",    .id = COM_NNOREMAP,
	  .descr = "noremap keys in normal mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &nnoremap_cmd,    .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "nohlsearch",        .abbr = "noh",   .id = -1,
	  .descr = "reset highlighting of search matches",
	  .flags = HAS_COMMENT,
	  .handler = &nohlsearch_cmd,  .min_args = 0,   .max_args = 0, },
	{ .name = "noremap",           .abbr = "no",    .id = COM_NOREMAP,
	  .descr = "noremap keys in normal and visual modes",
	  .flags = HAS_EMARK | HAS_RAW_ARGS,
	  .handler = &noremap_cmd,     .min_args = 2,   .max_args = NOT_DEF, },
	{ .name = "normal",            .abbr = "norm",  .id = COM_NORMAL,
	  .descr = "emulate keys typed in normal mode",
	  .flags = HAS_EMARK,
	  .handler = &normal_cmd,      .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "nunmap",            .abbr = "nun",   .id = -1,
	  .descr = "unmap user keys in normal mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &nunmap_cmd,      .min_args = 1,   .max_args = 1, },
	{ .name = "only",              .abbr = "on",    .id = -1,
	  .descr = "switch to single-view mode",
	  .flags = HAS_COMMENT,
	  .handler = &only_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "plugin",            .abbr = NULL,    .id = COM_PLUGIN,
	  .descr = "manage plugins",
	  .flags = HAS_COMMENT,
	  .handler = &plugin_cmd,      .min_args = 1,   .max_args = 2, },
	{ .name = "plugins",           .abbr = NULL,    .id = -1,
	  .descr = "display plugins menu",
	  .flags = HAS_COMMENT,
	  .handler = &plugins_cmd,     .min_args = 0,   .max_args = 0, },
	{ .name = "popd",              .abbr = NULL,    .id = -1,
	  .descr = "pop top of directory stack",
	  .flags = HAS_COMMENT,
	  .handler = &popd_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "pushd",             .abbr = NULL,    .id = COM_PUSHD,
	  .descr = "push onto directory stack",
	  .flags = HAS_EMARK | HAS_QUOTED_ARGS | HAS_COMMENT | HAS_ENVVARS,
	  .handler = &pushd_cmd,       .min_args = 0,   .max_args = 2, },
	{ .name = "put",               .abbr = "pu",    .id = -1,
	  .descr = "paste files from a register",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_BG_FLAG,
	  .handler = &put_cmd,         .min_args = 0,   .max_args = 1, },
	{ .name = "pwd",               .abbr = "pw",    .id = -1,
	  .descr = "display current location",
	  .flags = HAS_COMMENT,
	  .handler = &pwd_cmd,         .min_args = 0,   .max_args = 0, },
	{ .name = "qall",              .abbr = "qa",    .id = -1,
	  .descr = "close all tabs and exit the application",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &qall_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "qmap",              .abbr = "qm",    .id = COM_QMAP,
	  .descr = "map keys in preview mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &qmap_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "qnoremap",          .abbr = "qno",   .id = COM_QNOREMAP,
	  .descr = "noremap keys in preview mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &qnoremap_cmd,    .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "quit",              .abbr = "q",     .id = -1,
	  .descr = "close a tab or exit the application",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &quit_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "qunmap",            .abbr = "qun",   .id = -1,
	  .descr = "unmap user keys in preview mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &qunmap_cmd,      .min_args = 1,   .max_args = 1, },
	{ .name = "redraw",            .abbr = "redr",  .id = -1,
	  .descr = "force screen redraw",
	  .flags = HAS_COMMENT,
	  .handler = &redraw_cmd,      .min_args = 0,   .max_args = 0, },
	{ .name = "regedit",           .abbr = "rege",  .id = -1,
	  .descr = "edit register contents",
	  .flags = HAS_COMMENT | HAS_RAW_ARGS,
	  .handler = &regedit_cmd,     .min_args = 0,   .max_args = 1 },
	{ .name = "registers",         .abbr = "reg",   .id = -1,
	  .descr = "display registers",
	  .flags = 0,
	  .handler = &registers_cmd,   .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "regular",           .abbr = NULL,    .id = -1,
	  .descr = "switch to regular view leaving custom view",
	  .flags = HAS_COMMENT,
	  .handler = &regular_cmd,     .min_args = 0,   .max_args = 0, },
	{ .name = "rename",            .abbr = NULL,    .id = COM_RENAME,
	  .descr = "rename files",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_QUOTED_ARGS | HAS_COMMENT
	         | HAS_SELECTION_SCOPE,
	  .handler = &rename_cmd,      .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "restart",           .abbr = NULL,    .id = -1,
	  .descr = "reset state and reread configuration",
	  .flags = HAS_COMMENT,
	  .handler = &restart_cmd,     .min_args = 0,   .max_args = 1, },
	{ .name = "restore",           .abbr = NULL,    .id = -1,
	  .descr = "restore files from a trash",
	  .flags = HAS_RANGE | HAS_COMMENT | HAS_SELECTION_SCOPE,
	  .handler = &restore_cmd,     .min_args = 0,   .max_args = 0, },
	{ .name = "rlink",             .abbr = NULL,    .id = COM_RLINK,
	  .descr = "create relative links",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_QUOTED_ARGS | HAS_COMMENT
	         | HAS_QMARK_NO_ARGS | HAS_SELECTION_SCOPE,
	  .handler = &rlink_cmd,       .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "screen",            .abbr = NULL,    .id = -1,
	  .descr = "view/toggle terminal multiplexer support",
	  .flags = HAS_COMMENT | HAS_EMARK | HAS_QMARK_NO_ARGS,
	  .handler = &screen_cmd,      .min_args = 0,   .max_args = 0, },
	{ .name = "select",            .abbr = NULL,    .id = COM_SELECT,
	  .descr = "select files matching pattern or range",
	  .flags = HAS_EMARK | HAS_RANGE | HAS_REGEXP_ARGS,
	  .handler = &select_cmd,      .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "session",           .abbr = NULL,    .id = COM_SESSION,
	  .descr = "shows, detaches or switches active session",
	  .flags = HAS_COMMENT | HAS_QMARK_NO_ARGS,
	  .handler = &session_cmd,     .min_args = 0,   .max_args = 1, },
	/* engine/options unit handles comments to resolve parsing ambiguity. */
	{ .name = "set",               .abbr = "se",    .id = COM_SET,
	  .descr = "set global and local options",
	  .flags = 0,
	  .handler = &set_cmd,         .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "setlocal",          .abbr = "setl",  .id = COM_SETLOCAL,
	  .descr = "set local options",
	  .flags = 0,
	  .handler = &setlocal_cmd,    .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "setglobal",         .abbr = "setg",  .id = COM_SET,
	  .descr = "set global options",
	  .flags = 0,
	  .handler = &setglobal_cmd,   .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "shell",             .abbr = "sh",    .id = -1,
	  .descr = "spawn shell",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &shell_cmd,       .min_args = 0,   .max_args = 0, },
	{ .name = "siblnext",          .abbr = NULL,    .id = -1,
	  .descr = "navigate to next sibling directory",
	  .flags = HAS_RANGE | HAS_EMARK | HAS_COMMENT,
	  .handler = &siblnext_cmd,    .min_args = 0,   .max_args = 0, },
	{ .name = "siblprev",          .abbr = NULL,    .id = -1,
	  .descr = "navigate to previous sibling directory",
	  .flags = HAS_RANGE | HAS_EMARK | HAS_COMMENT,
	  .handler = &siblprev_cmd,    .min_args = 0,   .max_args = 0, },
	{ .name = "sort",              .abbr = "sor",   .id = -1,
	  .descr = "display sorting dialog",
	  .flags = HAS_COMMENT,
	  .handler = &sort_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "source",            .abbr = "so",    .id = COM_SOURCE,
	  .descr = "source file with :commands",
	  .flags = HAS_QUOTED_ARGS | HAS_COMMENT | HAS_ENVVARS,
	  .handler = &source_cmd,      .min_args = 1,   .max_args = 1, },
	{ .name = "split",             .abbr = "sp",    .id = COM_SPLIT,
	  .descr = "horizontal split layout",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &split_cmd,       .min_args = 0,   .max_args = 1, },
	{ .name = "stop",              .abbr = "st",    .id = -1,
	  .descr = "suspend the process (same as pressing Ctrl-Z)",
	  .flags = HAS_COMMENT,
	  .handler = &stop_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "substitute",        .abbr = "s",     .id = COM_SUBSTITUTE,
	  .descr = "perform substitutions in file names",
	  .flags = HAS_RANGE | HAS_REGEXP_ARGS | HAS_COMMENT | HAS_CUST_SEP
	         | HAS_SELECTION_SCOPE,
	  .handler = &substitute_cmd,  .min_args = 0,   .max_args = 3, },
	{ .name = "sync",              .abbr = NULL,    .id = COM_SYNC,
	  .descr = "synchronize properties of views",
	  .flags = HAS_EMARK | HAS_COMMENT | HAS_MACROS_FOR_CMD,
	  .handler = &sync_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "tabclose",          .abbr = "tabc",  .id = -1,
	  .descr = "close current tab unless it's the only one",
	  .flags = HAS_COMMENT,
	  .handler = &tabclose_cmd,    .min_args = 0,   .max_args = 0, },
	{ .name = "tabmove",           .abbr = "tabm",  .id = -1,
	  .descr = "position current tab after another tab",
	  .flags = HAS_COMMENT,
	  .handler = &tabmove_cmd,     .min_args = 0,   .max_args = 1, },
	{ .name = "tabname",           .abbr = NULL,    .id = -1,
	  .descr = "set name of current tab",
	  .flags = HAS_COMMENT,
	  .handler = &tabname_cmd,     .min_args = 0,   .max_args = 1, },
	{ .name = "tabnew",            .abbr = NULL,    .id = COM_TABNEW,
	  .descr = "make new tab and switch to it",
	  .flags = HAS_QUOTED_ARGS | HAS_ENVVARS | HAS_MACROS_FOR_CMD | HAS_COMMENT,
	  .handler = &tabnew_cmd,      .min_args = 0,   .max_args = 1, },
	{ .name = "tabnext",           .abbr = "tabn",  .id = -1,
	  .descr = "go to next or n-th tab",
	  .flags = HAS_COMMENT,
	  .handler = &tabnext_cmd,     .min_args = 0,   .max_args = 1, },
	{ .name = "tabonly",           .abbr = "tabo",  .id = -1,
	  .descr = "close all tabs but the current one",
	  .flags = HAS_COMMENT,
	  .handler = &tabonly_cmd,     .min_args = 0,   .max_args = 0, },
	{ .name = "tabprevious",       .abbr = "tabp",  .id = -1,
	  .descr = "go to previous or n-th previous tab",
	  .flags = HAS_COMMENT,
	  .handler = &tabprevious_cmd, .min_args = 0,   .max_args = 1, },
	{ .name = "touch",             .abbr = NULL,    .id = COM_TOUCH,
	  .descr = "create files",
	  .flags = HAS_RANGE | HAS_QUOTED_ARGS | HAS_COMMENT | HAS_MACROS_FOR_CMD,
	  .handler = &touch_cmd,       .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "tr",                .abbr = NULL,    .id = COM_TR,
	  .descr = "replace characters in file names",
	  .flags = HAS_RANGE | HAS_REGEXP_ARGS | HAS_COMMENT | HAS_CUST_SEP
	         | HAS_SELECTION_SCOPE,
	  .handler = &tr_cmd,          .min_args = 2,   .max_args = 2, },
	{ .name = "trashes",           .abbr = NULL,    .id = -1,
	  .descr = "display trash directories",
	  .flags = HAS_COMMENT | HAS_QMARK_NO_ARGS,
	  .handler = &trashes_cmd,     .min_args = 0,   .max_args = 0, },
	{ .name = "tree",              .abbr = NULL,    .id = COM_TREE,
	  .descr = "display filesystem as a tree",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &tree_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "undolist",          .abbr = "undol", .id = -1,
	  .descr = "display list of operations",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &undolist_cmd,    .min_args = 0,   .max_args = 0, },
	{ .name = "unlet",             .abbr = "unl",   .id = COM_UNLET,
	  .descr = "undefine variable",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &unlet_cmd,       .min_args = 1,   .max_args = NOT_DEF, },
	{ .name = "unmap",             .abbr = "unm",   .id = -1,
	  .descr = "unmap user keys in normal and visual modes",
	  .flags = HAS_EMARK | HAS_RAW_ARGS,
	  .handler = &unmap_cmd,       .min_args = 1,   .max_args = 1, },
	{ .name = "unselect",          .abbr = NULL,    .id = COM_SELECT,
	  .descr = "unselect files matching pattern or range",
	  .flags = HAS_RANGE | HAS_REGEXP_ARGS,
	  .handler = &unselect_cmd,    .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "version",           .abbr = "ve",    .id = -1,
	  .descr = "display version information",
	  .flags = HAS_COMMENT,
	  .handler = &vifm_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "view",              .abbr = "vie",   .id = -1,
	  .descr = "control visibility of preview",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &view_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "vifm",              .abbr = NULL,    .id = -1,
	  .descr = "display version information",
	  .flags = HAS_COMMENT,
	  .handler = &vifm_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "vmap",              .abbr = "vm",    .id = COM_VMAP,
	  .descr = "map keys in visual mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &vmap_cmd,        .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "vnoremap",          .abbr = "vn",    .id = COM_VNOREMAP,
	  .descr = "noremap keys in visual mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &vnoremap_cmd,    .min_args = 0,   .max_args = NOT_DEF, },
#ifdef _WIN32
	{ .name = "volumes",           .abbr = NULL,    .id = -1,
	  .descr = "display list of drives",
	  .flags = HAS_COMMENT,
	  .handler = &volumes_cmd,     .min_args = 0,   .max_args = 0, },
#endif
	{ .name = "vsplit",            .abbr = "vs",    .id = COM_VSPLIT,
	  .descr = "vertical split layout",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &vsplit_cmd,      .min_args = 0,   .max_args = 1, },
	{ .name = "vunmap",            .abbr = "vu",    .id = -1,
	  .descr = "unmap user keys in visual mode",
	  .flags = HAS_RAW_ARGS,
	  .handler = &vunmap_cmd,      .min_args = 1,   .max_args = 1, },
	{ .name = "wincmd",            .abbr = "winc",  .id = COM_WINCMD,
	  .descr = "cmdline Ctrl-W substitute",
	  .flags = HAS_RANGE,
	  .handler = &wincmd_cmd,      .min_args = 1,   .max_args = 1, },
	{ .name = "windo",             .abbr = NULL,    .id = COM_WINDO,
	  .descr = "run command for each pane",
	  .flags = 0,
	  .handler = &windo_cmd,       .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "winrun",            .abbr = NULL,    .id = COM_WINRUN,
	  .descr = "run command for specific pane(s)",
	  .flags = 0,
	  .handler = &winrun_cmd,      .min_args = 0,   .max_args = NOT_DEF, },
	{ .name = "write",             .abbr = "w",     .id = -1,
	  .descr = "write vifminfo file",
	  .flags = HAS_COMMENT,
	  .handler = &write_cmd,       .min_args = 0,   .max_args = 0, },
	{ .name = "wq",                .abbr = NULL,    .id = -1,
	  .descr = "close a tab or exit the application",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &wq_cmd,          .min_args = 0,   .max_args = 0, },
	{ .name = "wqall",             .abbr = "wqa",   .id = -1,
	  .descr = "exit the application",
	  .flags = HAS_EMARK | HAS_COMMENT,
	  .handler = &wqall_cmd,       .min_args = 0,   .max_args = 0, },
	{ .name = "xall",              .abbr = "xa",    .id = -1,
	  .descr = "exit the application",
	  .flags = HAS_COMMENT,
	  .handler = &qall_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "xit",               .abbr = "x",     .id = -1,
	  .descr = "exit the application",
	  .flags = HAS_COMMENT,
	  .handler = &quit_cmd,        .min_args = 0,   .max_args = 0, },
	{ .name = "yank",              .abbr = "y",     .id = -1,
	  .descr = "yank files",
	  .flags = HAS_RANGE | HAS_SELECTION_SCOPE,
	  .handler = &yank_cmd,        .min_args = 0,   .max_args = 2, },

	{ .name = "<USERCMD>",         .abbr = NULL,    .id = -1,
	  .descr = "user-defined command",
	  .flags = HAS_RANGE | HAS_QUOTED_ARGS | HAS_MACROS_FOR_CMD
	         | HAS_SELECTION_SCOPE,
	  .handler = &usercmd_cmd,     .min_args = 0,   .max_args = NOT_DEF, },
};
const size_t cmds_list_size = ARRAY_LEN(cmds_list);

/* Return value of all functions below which name ends with "_cmd" mean:
 *  - <0 -- one of CMDS_* errors from cmds.h;
 *  - =0 -- nothing was outputted to the status bar, don't need to save its
 *          state;
 *  - >0 -- something was printed to the status bar, need to save its state. */
static int
goto_cmd(const cmd_info_t *cmd_info)
{
	cmds_preserve_selection();
	fpos_set_pos(curr_view, cmd_info->end);
	return 0;
}

/* Handles :! command, which executes external command via shell. */
static int
emark_cmd(const cmd_info_t *cmd_info)
{
	int save_msg = 0;
	const char *com = cmd_info->args;
	char buf[COMMAND_GROUP_INFO_LEN];

	if(cmd_info->argc == 0)
	{
		if(cmd_info->emark)
		{
			const char *const last_cmd = curr_stats.last_cmdline_command;
			if(last_cmd == NULL)
			{
				ui_sb_msg("No previous command-line command");
				return 1;
			}
			return cmds_dispatch(last_cmd, curr_view, CIT_COMMAND) != 0;
		}
		return CMDS_ERR_TOO_FEW_ARGS;
	}

	com = skip_whitespace(com);
	if(com[0] == '\0')
	{
		return 0;
	}

	MacroFlags flags = (MacroFlags)cmd_info->usr1;
	char *title = format_str("!%s", cmd_info->raw_args);
	int handled = rn_ext(curr_view, com, title, flags, /*pause=*/cmd_info->emark,
			cmd_info->bg, &save_msg);
	free(title);

	if(handled > 0)
	{
		/* Do nothing. */
	}
	else if(handled < 0)
	{
		return save_msg;
	}
	else if(cmd_info->bg)
	{
		rn_start_bg_command(curr_view, com, flags);
	}
	else
	{
		const int use_term_mux = ma_flags_missing(flags, MF_NO_TERM_MUX);
		const ShellPause pause = (cmd_info->emark ? PAUSE_ALWAYS : PAUSE_ON_ERROR);

		flist_sel_stash(curr_view);

		const char *cmd_to_run = com;
		char *expanded_cmd = NULL;

		if(cfg.fast_run)
		{
			expanded_cmd = fast_run_complete(com);
			if(expanded_cmd != NULL)
			{
				cmd_to_run = expanded_cmd;
			}
		}

		if(ma_flags_present(flags, MF_PIPE_FILE_LIST) ||
				ma_flags_present(flags, MF_PIPE_FILE_LIST_Z))
		{
			(void)rn_pipe(cmd_to_run, curr_view, flags, pause);
		}
		else
		{
			(void)rn_shell(cmd_to_run, pause, use_term_mux, SHELL_BY_USER);
		}

		free(expanded_cmd);
	}

	snprintf(buf, sizeof(buf), "in %s: !%s",
			replace_home_part(flist_get_dir(curr_view)), cmd_info->raw_args);
	un_group_open(buf);
	un_group_add_op(OP_USR, strdup(com), NULL, "", "");
	un_group_close();

	return save_msg;
}

/* Creates symbolic links with absolute paths to files. */
static int
alink_cmd(const cmd_info_t *cmd_info)
{
	return link_cmd(cmd_info, 1);
}

/* Registers a navigation mapping that allows remapping. */
static int
amap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "Navigation", NAV_MODE, /*no_remap=*/0) != 0;
}

/* Registers a navigation mapping that doesn't allow remapping. */
static int
anoremap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "Navigation", NAV_MODE, /*no_remap=*/1) != 0;
}

static int
apropos_cmd(const cmd_info_t *cmd_info)
{
	static char *last_args;

	if(cmd_info->argc > 0)
	{
		(void)replace_string(&last_args, cmd_info->args);
	}
	else if(last_args == NULL)
	{
		ui_sb_err("Nothing to repeat");
		return CMDS_ERR_CUSTOM;
	}

	return show_apropos_menu(curr_view, last_args) != 0;
}

/* Adds/lists/removes autocommands. */
static int
autocmd_cmd(const cmd_info_t *cmd_info)
{
	enum { ADDITION, LISTING, REMOVAL } type = cmd_info->emark
	                                         ? REMOVAL : (cmd_info->argc < 3)
	                                         ? LISTING : ADDITION;

	const char *event = NULL;
	const char *patterns = NULL;
	const char *action;

	/* Check usage. */
	if(cmd_info->emark && cmd_info->argc > 2)
	{
		return CMDS_ERR_TRAILING_CHARS;
	}

	/* Parse event and patterns. */
	if(cmd_info->argc > 0)
	{
		if(type == ADDITION || strcmp(cmd_info->argv[0], "*") != 0)
		{
			event = cmd_info->argv[0];
		}
		if(cmd_info->argc > 1)
		{
			patterns = cmd_info->argv[1];
		}
	}

	/* Check validity of event. */
	if(event != NULL)
	{
		/* Non-const for is_in_string_array_case(). */
		static char *events[] = { "DirEnter" };
		if(!is_in_string_array_case(events, ARRAY_LEN(events), event))
		{
			ui_sb_errf("No such event: %s", event);
			return CMDS_ERR_CUSTOM;
		}
	}

	if(type == REMOVAL)
	{
		vle_aucmd_remove(event, patterns);
		return 0;
	}

	if(type == LISTING)
	{
		vle_textbuf *const msg = vle_tb_create();
		vle_aucmd_list(event, patterns, &aucmd_list_cb, msg);
		ui_sb_msg(vle_tb_get_data(msg));
		vle_tb_free(msg);
		return 1;
	}

	/* Addition. */

	action = &cmd_info->args[cmd_info->argvp[2][0]];

	if(vle_aucmd_on_execute(event, patterns, action, &aucmd_action_handler) != 0)
	{
		ui_sb_err("Failed to register autocommand");
		return CMDS_ERR_CUSTOM;
	}

	return 0;
}

/* Implementation of autocommand action. */
static void
aucmd_action_handler(const char action[], void *arg)
{
	view_t *view = arg;
	view_t *tmp_curr, *tmp_other;
	ui_view_pick(view, &tmp_curr, &tmp_other);

	char *saved_cwd = save_cwd();
	(void)vifm_chdir(flist_get_dir(view));

	const int prev_global_local_settings = curr_stats.global_local_settings;
	curr_stats.global_local_settings = 0;

	(void)cmds_dispatch(action, view, CIT_COMMAND);

	curr_stats.global_local_settings = prev_global_local_settings;

	restore_cwd(saved_cwd);

	ui_view_unpick(view, tmp_curr, tmp_other);
}

/* Handler of list callback for autocommands. */
static void
aucmd_list_cb(const char event[], const char pattern[], int negated,
		const char action[], void *arg)
{
	vle_textbuf *msg = arg;

	int pattern_len = (negated ? 1 : 0) + strlen(pattern);
	const char *fmt = (pattern_len <= 10)
	                ? "%-10s %s%-*s %s"
	                : "%-10s %s%-*s\n                      %s";
	int max_pattern_width = (negated ? 9 : 10);

	vle_tb_append_linef(msg, fmt, event, negated ? "!" : "",
			max_pattern_width, pattern, action);
}

/* Unregisters a navigation mapping. */
static int
aunmap_cmd(const cmd_info_t *cmd_info)
{
	return do_unmap(cmd_info->argv[0], NAV_MODE);
}

/* Marks directory with set of tags. */
static int
bmark_cmd(const cmd_info_t *cmd_info)
{
	char *const tags = make_tags_list(cmd_info);
	char *const path = get_bmark_dir(cmd_info);
	const int err = (tags == NULL || bmarks_set(path, tags) != 0);
	if(err && tags != NULL)
	{
		ui_sb_err("Failed to add bookmark");
	}
	free(path);
	free(tags);
	return (err ? CMDS_ERR_CUSTOM : 0);
}

/* Lists either all bookmarks or those matching specified tags. */
static int
bmarks_cmd(const cmd_info_t *cmd_info)
{
	return bmarks_do(cmd_info, 0);
}

/* When there are more than 1 match acts like :bmarks, otherwise navigates to
 * single match immediately. */
static int
bmgo_cmd(const cmd_info_t *cmd_info)
{
	return bmarks_do(cmd_info, 1);
}

/* Runs bookmarks menu in either view or go mode (different only on single
 * match). */
static int
bmarks_do(const cmd_info_t *cmd_info, int go)
{
	char *const tags = args_to_csl(cmd_info);
	const int result = (show_bmarks_menu(curr_view, tags, go) != 0);
	free(tags);
	return result;
}

/* Converts command arguments into comma-separated list of tags.  Returns newly
 * allocated string or NULL on error or invalid tag name (in which case an error
 * is printed on the status bar). */
static char *
make_tags_list(const cmd_info_t *cmd_info)
{
	int i;

	if(cmd_info->emark && cmd_info->argc == 1)
	{
		ui_sb_err("Too few arguments");
		return NULL;
	}

	for(i = cmd_info->emark ? 1 : 0; i < cmd_info->argc; ++i)
	{
		if(strpbrk(cmd_info->argv[i], ", \t") != NULL)
		{
			ui_sb_errf("Tags can't include comma or whitespace: %s",
					cmd_info->argv[i]);
			return NULL;
		}
	}

	return args_to_csl(cmd_info);
}

/* Makes comma-separated list from command line arguments.  Returns newly
 * allocated string or NULL on absence of arguments. */
static char *
args_to_csl(const cmd_info_t *cmd_info)
{
	int i;
	char *tags = NULL;
	size_t len = 0U;

	if(cmd_info->argc == 0)
	{
		return NULL;
	}

	i = cmd_info->emark ? 1 : 0;
	strappend(&tags, &len, cmd_info->argv[i]);
	for(++i; i < cmd_info->argc; ++i)
	{
		strappendch(&tags, &len, ',');
		strappend(&tags, &len, cmd_info->argv[i]);
	}

	return tags;
}

/* Registers command-line mode abbreviation. */
static int
cabbrev_cmd(const cmd_info_t *cmd_info)
{
	return handle_cabbrevs(cmd_info, 0);
}

/* Displays list of remembered menus. */
static int
chistory_cmd(const cmd_info_t *cmd_info)
{
	return show_chistory_menu(curr_view) != 0;
}

/* Registers command-line mode abbreviation of noremap kind. */
static int
cnoreabbrev_cmd(const cmd_info_t *cmd_info)
{
	return handle_cabbrevs(cmd_info, 1);
}

/* Handles command-line mode abbreviation of both kinds in one place.  Returns
 * value to be returned by command handler. */
static int
handle_cabbrevs(const cmd_info_t *cmd_info, int no_remap)
{
	if(cmd_info->argc == 0)
	{
		return show_cabbrevs_menu(curr_view) != 0;
	}
	if(cmd_info->argc == 1)
	{
		return list_abbrevs(cmd_info->argv[0]);
	}
	return add_cabbrev(cmd_info, no_remap);
}

/* List command-line mode abbreviations that start with specified prefix.
 * Returns value to be returned by command handler. */
static int
list_abbrevs(const char prefix[])
{
	size_t prefix_len;
	void *state;
	const wchar_t *lhs, *rhs;
	const char *descr;
	int no_remap;
	vle_textbuf *msg;
	int seen_match;

	wchar_t *wide_prefix = to_wide(prefix);
	if(wide_prefix == NULL)
	{
		show_error_msgf("Abbrevs Error", "Failed to convert to wide string: %s",
				prefix);
		return 0;
	}

	state = NULL;
	if(!vle_abbr_iter(&lhs, &rhs, &descr, &no_remap, &state))
	{
		free(wide_prefix);
		ui_sb_msg("No abbreviations found");
		return 1;
	}

	msg = vle_tb_create();
	vle_tb_append_line(msg, "Abbreviation -- N -- Expansion/Description");

	prefix_len = wcslen(wide_prefix);

	seen_match = 0;
	state = NULL;
	while(vle_abbr_iter(&lhs, &rhs, &descr, &no_remap, &state))
	{
		if(wcsncmp(lhs, wide_prefix, prefix_len) == 0)
		{
			char *const line = describe_abbrev(lhs, rhs, descr, no_remap,
					/*offset=*/0);
			vle_tb_append_line(msg, line);
			free(line);
			seen_match = 1;
		}
	}

	if(seen_match)
	{
		ui_sb_msg(vle_tb_get_data(msg));
	}
	else
	{
		ui_sb_msg("No abbreviations found");
	}
	vle_tb_free(msg);

	free(wide_prefix);
	return 1;
}

/* Registers command-line mode abbreviation.  Returns value to be returned by
 * command handler. */
static int
add_cabbrev(const cmd_info_t *cmd_info, int no_remap)
{
	wchar_t *subst;
	wchar_t *wargs = to_wide(cmd_info->args);
	wchar_t *rhs = wargs;

	if(wargs == NULL)
	{
		show_error_msgf("Abbrevs Error", "Failed to convert to wide string: %s",
				cmd_info->args);
		return 0;
	}

	while(cfg_is_word_wchar(*rhs))
	{
		++rhs;
	}
	while(iswspace(*rhs))
	{
		*rhs++ = L'\0';
	}

	subst = substitute_specsw(rhs);
	int err = no_remap
	        ? vle_abbr_add_no_remap(wargs, subst)
	        : vle_abbr_add(wargs, subst);
	free(subst);
	free(wargs);

	if(err != 0)
	{
		ui_sb_err("Failed to register abbreviation");
		return CMDS_ERR_CUSTOM;
	}

	return 0;
}

/* Changes location of a view or both views.  Handle multiple configurations of
 * the command (with/without !, none/one/two arguments). */
static int
cd_cmd(const cmd_info_t *cmd_info)
{
	int result;

	char *const curr_dir = strdup(flist_get_dir(curr_view));
	char *const other_dir = strdup(flist_get_dir(other_view));

	if(!cfg.auto_ch_pos)
	{
		flist_hist_clear(curr_view);
		curr_stats.ch_pos = 0;
	}

	if(cmd_info->argc == 0)
	{
		result = cd(curr_view, curr_dir, cfg.home_dir);
		if(result == 0 && cmd_info->emark)
		{
			result += cd(other_view, other_dir, cfg.home_dir);
		}
	}
	else if(cmd_info->argc == 1)
	{
		result = cd(curr_view, curr_dir, cmd_info->argv[0]);
		if(cmd_info->emark)
		{
			if(!is_path_absolute(cmd_info->argv[0]) && cmd_info->argv[0][0] != '~' &&
					strcmp(cmd_info->argv[0], "-") != 0)
			{
				char dir[PATH_MAX + 1];
				snprintf(dir, sizeof(dir), "%s/%s", curr_dir, cmd_info->argv[0]);
				result += cd(other_view, other_dir, dir);
			}
			else if(strcmp(cmd_info->argv[0], "-") == 0)
			{
				result += cd(other_view, other_dir, curr_dir);
			}
			else
			{
				result += cd(other_view, other_dir, cmd_info->argv[0]);
			}
			refresh_view_win(other_view);
		}
	}
	else
	{
		result = cd(curr_view, curr_dir, cmd_info->argv[0]);
		if(!is_path_absolute(cmd_info->argv[1]) && cmd_info->argv[1][0] != '~')
		{
			char dir[PATH_MAX + 1];
			snprintf(dir, sizeof(dir), "%s/%s", curr_dir, cmd_info->argv[1]);
			result += cd(other_view, other_dir, dir);
		}
		else
		{
			result += cd(other_view, other_dir, cmd_info->argv[1]);
		}
		refresh_view_win(other_view);
	}

	if(!cfg.auto_ch_pos)
	{
		curr_stats.ch_pos = 1;
	}

	free(curr_dir);
	free(other_dir);

	return result;
}

/* Performs substitution on current path and navigates to the result. */
static int
cds_cmd(const cmd_info_t *cmd_info)
{
	int case_sensitive = !regexp_should_ignore_case(cmd_info->argv[0]);
	if(cmd_info->argc > 2)
	{
		cmd_info->argv[2][strcspn(cmd_info->argv[2], " \t")] = '\0';
		if(parse_case_flag(cmd_info->argv[2], &case_sensitive) != 0)
		{
			ui_sb_errf("Failed to parse flags: %s", cmd_info->argv[2]);
			return CMDS_ERR_CUSTOM;
		}
	}

	const char *const curr_dir = flist_get_dir(curr_view);
	char *const new_path = strdup(regexp_replace(curr_dir, cmd_info->argv[0],
				cmd_info->argv[1], 0, !case_sensitive));
	if(new_path == NULL)
	{
		return CMDS_ERR_NO_MEM;
	}

	if(strcmp(curr_dir, new_path) == 0)
	{
		free(new_path);
		ui_sb_msgf("No \"%s\" found in CWD", cmd_info->argv[0]);
		return 1;
	}

	int result = cd(curr_view, curr_dir, new_path);
	if(result == 0 && cmd_info->emark)
	{
		result += cd(other_view, curr_dir, new_path);
	}

	free(new_path);

	return result;
}

static int
change_cmd(const cmd_info_t *cmd_info)
{
	enter_change_mode(curr_view);
	cmds_preserve_selection();
	return 0;
}

static int
chmod_cmd(const cmd_info_t *cmd_info)
{
#ifndef _WIN32
	regex_t re;
	int err;
	int i;
#endif

	if(cmd_info->argc == 0)
	{
		enter_attr_mode(curr_view);
		cmds_preserve_selection();
		return 0;
	}

#ifndef _WIN32
	if((err = regcomp(&re, "^([ugoa]*([-+=]([rwxXst]*|[ugo]))+)|([0-7]{3,4})$",
			REG_EXTENDED)) != 0)
	{
		ui_sb_errf("Regexp error: %s", get_regexp_error(err, &re));
		regfree(&re);
		return CMDS_ERR_CUSTOM;
	}

	for(i = 0; i < cmd_info->argc; i++)
	{
		if(regexec(&re, cmd_info->argv[i], 0, NULL, 0) == REG_NOMATCH)
		{
			break;
		}
	}
	regfree(&re);

	if(i < cmd_info->argc)
	{
		ui_sb_errf("Invalid argument: %s", cmd_info->argv[i]);
		return CMDS_ERR_CUSTOM;
	}

	flist_set_marking(curr_view, 0);
	files_chmod(curr_view, cmd_info->args, cmd_info->emark);

	/* Reload metadata because attribute change might not be detected. */
	ui_view_schedule_reload(curr_view);
	ui_view_schedule_reload(other_view);
#endif

	return 0;
}

#ifndef _WIN32
static int
chown_cmd(const cmd_info_t *cmd_info)
{
	char *colon, *user, *group;
	int u, g;
	uid_t uid = (uid_t)-1;
	gid_t gid = (gid_t)-1;

	if(cmd_info->argc == 0)
	{
		fops_chuser();
		return 0;
	}

	colon = strchr(cmd_info->argv[0], ':');
	if(colon == NULL)
	{
		user = cmd_info->argv[0];
		group = "";
	}
	else
	{
		*colon = '\0';
		user = cmd_info->argv[0];
		group = colon + 1;
	}
	u = user[0] != '\0';
	g = group[0] != '\0';

	if(u && get_uid(user, &uid) != 0)
	{
		ui_sb_errf("Invalid user name: \"%s\"", user);
		return CMDS_ERR_CUSTOM;
	}
	if(g && get_gid(group, &gid) != 0)
	{
		ui_sb_errf("Invalid group name: \"%s\"", group);
		return CMDS_ERR_CUSTOM;
	}

	flist_set_marking(curr_view, 0);
	return fops_chown(u, g, uid, gid) != 0;
}
#endif

/* Clones file [count=1] times. */
static int
clone_cmd(const cmd_info_t *cmd_info)
{
	flist_set_marking(curr_view, 0);

	if(cmd_info->qmark)
	{
		if(cmd_info->argc > 0)
		{
			ui_sb_err("No arguments are allowed if you use \"?\"");
			return CMDS_ERR_CUSTOM;
		}
		return fops_clone(curr_view, NULL, -1, 0, 1) != 0;
	}

	return fops_clone(curr_view, cmd_info->argv, cmd_info->argc, cmd_info->emark,
			1) != 0;
}

static int
cmap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "Command Line", CMDLINE_MODE, 0) != 0;
}

static int
cnoremap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "Command Line", CMDLINE_MODE, 1) != 0;
}

/* Displays colorscheme menu, current colorscheme, associates colorscheme with a
 * subtree or sets primary colorscheme. */
static int
colorscheme_cmd(const cmd_info_t *cmd_info)
{
	if(cmd_info->qmark)
	{
		ui_sb_msg(cfg.cs.name);
		return 1;
	}

	if(cmd_info->argc == 0)
	{
		/* Show menu with colorschemes listed. */
		return show_colorschemes_menu(curr_view) != 0;
	}

	const int assoc_form = is_colorscheme_assoc_form(cmd_info);
	if((cmd_info->argc == 1 || assoc_form) && !cs_exists(cmd_info->argv[0]))
	{
		ui_sb_errf("Cannot find colorscheme %s" , cmd_info->argv[0]);
		return CMDS_ERR_CUSTOM;
	}

	if(assoc_form)
	{
		return assoc_colorscheme(cmd_info->argv[0], cmd_info->argv[1]);
	}

	set_colorscheme(cmd_info->argv, cmd_info->argc);
	return 0;
}

/* Checks whether :colorscheme was invoked to associate a color scheme with a
 * directory.  Returns non-zero if so, otherwise zero is returned. */
static int
is_colorscheme_assoc_form(const cmd_info_t *cmd_info)
{
	if(cmd_info->argc != 2 || cs_exists(cmd_info->argv[1]))
	{
		return 0;
	}

	/* The path in :colorscheme command cannot be relative in startup scripts. */
	if(curr_stats.load_stage < 3)
	{
		char *path = expand_tilde(cmd_info->argv[1]);
		int is_abs_path = is_path_absolute(path);
		free(path);
		return is_abs_path;
	}
	return 1;
}

/* Associates colorscheme with a subtree.  Returns value to be returned by
 * command handler. */
static int
assoc_colorscheme(const char name[], const char path[])
{
	char path_buf[PATH_MAX + 1];
	char *directory = expand_tilde(path);
	if(!is_path_absolute(directory))
	{
		snprintf(path_buf, sizeof(path_buf), "%s/%s", flist_get_dir(curr_view),
				directory);
		(void)replace_string(&directory, path_buf);
	}
	canonicalize_path(directory, path_buf, sizeof(path_buf));
	(void)replace_string(&directory, path_buf);

	if(!is_dir(directory))
	{
		ui_sb_errf("%s isn't a directory", directory);
		free(directory);
		return CMDS_ERR_CUSTOM;
	}

	cs_assoc_dir(name, directory);
	free(directory);

	lwin.local_cs = cs_load_local(1, lwin.curr_dir);
	rwin.local_cs = cs_load_local(0, rwin.curr_dir);
	redraw_lists();
	return 0;
}

/* Sets primary colorscheme. */
static void
set_colorscheme(char *names[], int count)
{
	if(count == 1)
	{
		cs_load_primary(names[0]);
	}
	else
	{
		cs_load_primary_list(names, count);
	}

	if(!lwin.local_cs)
	{
		cs_assign(&lwin.cs, &cfg.cs);
	}
	if(!rwin.local_cs)
	{
		cs_assign(&rwin.cs, &cfg.cs);
	}
	redraw_lists();
	update_all_windows();
}

static int
command_cmd(const cmd_info_t *cmd_info)
{
	char *desc;

	if(cmd_info->argc == 0)
	{
		cmds_preserve_selection();
		return show_commands_menu(curr_view) != 0;
	}

	desc = vle_cmds_print_udcs(cmd_info->argv[0]);
	if(desc == NULL)
	{
		ui_sb_msg("No user-defined commands found");
		return 1;
	}

	ui_sb_msg(desc);
	free(desc);
	return 1;
}

/* Copies files. */
static int
copy_cmd(const cmd_info_t *cmd_info)
{
	return cpmv_cmd(cmd_info, 0);
}

/* Same as :quit, but also aborts directory choosing and mandatory returns
 * non-zero exit code. */
static int
cquit_cmd(const cmd_info_t *cmd_info)
{
	vifm_try_leave(!cmd_info->emark, 1, cmd_info->emark);
	return 0;
}

/* Unregisters command-line abbreviation either by its LHS or RHS. */
static int
cunabbrev_cmd(const cmd_info_t *cmd_info)
{
	wchar_t *const wargs = to_wide(cmd_info->args);
	if(wargs == NULL)
	{
		show_error_msgf("Abbrevs Error", "Failed to convert to wide string: %s",
				cmd_info->args);
		return 0;
	}

	const int err = vle_abbr_remove(wargs);
	free(wargs);

	if(err != 0)
	{
		ui_sb_errf("No such abbreviation: %s", cmd_info->args);
		return CMDS_ERR_CUSTOM;
	}
	return 0;
}

static int
cunmap_cmd(const cmd_info_t *cmd_info)
{
	return do_unmap(cmd_info->argv[0], CMDLINE_MODE);
}

/* Processes :[range]delete command followed by "{reg} [{count}]" or
 * "{reg}|{count}" with optional " &". */
static int
delete_cmd(const cmd_info_t *cmd_info)
{
	int reg = DEFAULT_REG_NAME;
	int result;

	result = get_reg_and_count(cmd_info, &reg);
	if(result != 0)
	{
		return result;
	}

	flist_set_marking(curr_view, 0);
	if(cmd_info->bg)
	{
		result = fops_delete_bg(curr_view, !cmd_info->emark) != 0;
	}
	else
	{
		result = fops_delete(curr_view, reg, !cmd_info->emark) != 0;
	}

	return result;
}

static int
delmarks_cmd(const cmd_info_t *cmd_info)
{
	int i;

	if(cmd_info->emark)
	{
		if(cmd_info->argc == 0)
		{
			marks_clear_all();
			return 0;
		}
		else
		{
			ui_sb_err("No arguments are allowed if you use \"!\"");
			return CMDS_ERR_CUSTOM;
		}
	}

	if(cmd_info->argc == 0)
	{
		return CMDS_ERR_TOO_FEW_ARGS;
	}

	for(i = 0; i < cmd_info->argc; i++)
	{
		int j;
		for(j = 0; cmd_info->argv[i][j] != '\0'; j++)
		{
			if(!char_is_one_of(marks_all, cmd_info->argv[i][j]))
			{
				return CMDS_ERR_INVALID_ARG;
			}
		}
	}

	for(i = 0; i < cmd_info->argc; i++)
	{
		int j;
		for(j = 0; cmd_info->argv[i][j] != '\0'; j++)
		{
			marks_clear_one(curr_view, cmd_info->argv[i][j]);
		}
	}
	return 0;
}

/* Removes bookmarks. */
static int
delbmarks_cmd(const cmd_info_t *cmd_info)
{
	if(cmd_info->emark)
	{
		int i;

		/* Remove all bookmarks. */
		if(cmd_info->argc == 0)
		{
			bmarks_clear();
			return 0;
		}

		/* Remove bookmarks from listed paths. */
		for(i = 0; i < cmd_info->argc; ++i)
		{
			char *const path = make_bmark_path(cmd_info->argv[i]);
			bmarks_remove(path);
			free(path);
		}
	}
	else if(cmd_info->argc == 0)
	{
		/* Remove bookmarks from current directory. */
		char *const path = get_bmark_dir(cmd_info);
		bmarks_remove(path);
		free(path);
	}
	else
	{
		/* Remove set of bookmarks that include all of the specified tags. */
		char *const tags = make_tags_list(cmd_info);
		if(tags == NULL)
		{
			return CMDS_ERR_CUSTOM;
		}

		bmarks_find(tags, &remove_bmark, NULL);
		free(tags);
	}
	return 0;
}

/* bmarks_find() callback that removes bookmarks. */
static void
remove_bmark(const char path[], const char tags[], time_t timestamp, void *arg)
{
	/* It's safe to remove bookmark in the callback. */
	bmarks_remove(path);
}

/* Formats path for a bookmark in a unified way for several commands.  Returns
 * newly allocated string with the path. */
static char *
get_bmark_dir(const cmd_info_t *cmd_info)
{
	const char *const cwd = flist_get_dir(curr_view);

	if(cmd_info->emark)
	{
		return make_bmark_path(cmd_info->argv[0]);
	}

	return is_root_dir(cwd) ? strdup(cwd) : format_str("%s/", cwd);
}

/* Prepares path for a bookmark.  Returns newly allocated string. */
static char *
make_bmark_path(const char path[])
{
	char *ret;
	const char *const cwd = flist_get_dir(curr_view);
	char *const expanded = replace_tilde(ma_expand_single(path));

	if(is_path_absolute(expanded))
	{
		return expanded;
	}

	ret = format_str("%s%s%s", cwd, is_root_dir(cwd) ? "" : "/", expanded);
	free(expanded);
	return ret;
}

/* Compares files in one or two panes to produce their diff or lists of
 * duplicates or unique files. */
static int
compare_cmd(const cmd_info_t *cmd_info)
{
	int is_toggling = cmd_info->emark;
	if(is_toggling && !cv_compare(curr_view->custom.type))
	{
		ui_sb_err("Toggling requires active compare view");
		return CMDS_ERR_CUSTOM;
	}

	CompareType ct = CT_CONTENTS;
	ListType lt = LT_ALL;
	int flags = (is_toggling ? CF_NONE : CF_GROUP_PATHS);
	if(parse_compare_properties(cmd_info, &ct, &lt, &flags) != 0)
	{
		return CMDS_ERR_CUSTOM;
	}

	if(is_toggling)
	{
		struct cv_data_t *cv = &curr_view->custom;
		return (compare_two_panes(cv->diff_cmp_type, cv->diff_list_type,
					cv->diff_cmp_flags ^ flags) != 0);
	}

	if((flags & CF_SHOW) == 0)
	{
		flags |= CF_SHOW;
	}

	return (flags & CF_SINGLE_PANE)
	     ? (compare_one_pane(curr_view, ct, lt, flags) != 0)
	     : (compare_two_panes(ct, lt, flags) != 0);
}

/* Opens menu with contents of the last displayed menu with navigation to files
 * by default, if any. */
static int
copen_cmd(const cmd_info_t *cmd_info)
{
	return menus_unstash(curr_view) != 0;
}

/* Parses comparison properties.  Default values for arguments should be set
 * before the call.  Returns zero on success, otherwise non-zero is returned and
 * error message is displayed on the status bar. */
static int
parse_compare_properties(const cmd_info_t *cmd_info, CompareType *ct,
		ListType *lt, int *flags)
{
	int i;
	for(i = 0; i < cmd_info->argc; ++i)
	{
		const char *const property = cmd_info->argv[i];

		if(cmd_info->emark && !starts_with_lit(property, "show"))
		{
			ui_sb_errf("Unexpected property for toggling: %s", property);
			return CMDS_ERR_CUSTOM;
		}

		if     (strcmp(property, "byname") == 0)     *ct = CT_NAME;
		else if(strcmp(property, "bysize") == 0)     *ct = CT_SIZE;
		else if(strcmp(property, "bycontents") == 0) *ct = CT_CONTENTS;

		else if(strcmp(property, "listall") == 0)    *lt = LT_ALL;
		else if(strcmp(property, "listunique") == 0) *lt = LT_UNIQUE;
		else if(strcmp(property, "listdups") == 0)   *lt = LT_DUPS;

		else if(strcmp(property, "ofboth") == 0)     *flags &= ~CF_SINGLE_PANE;
		else if(strcmp(property, "ofone") == 0)      *flags |= CF_SINGLE_PANE;

		else if(strcmp(property, "groupids") == 0)   *flags &= ~CF_GROUP_PATHS;
		else if(strcmp(property, "grouppaths") == 0) *flags |= CF_GROUP_PATHS;

		else if(strcmp(property, "skipempty") == 0)  *flags |= CF_SKIP_EMPTY;

		else if(strcmp(property, "showidentical") == 0)
			*flags |= CF_SHOW_IDENTICAL;
		else if(strcmp(property, "showdifferent") == 0)
			*flags |= CF_SHOW_DIFFERENT;
		else if(strcmp(property, "showuniqueleft") == 0)
			*flags |= CF_SHOW_UNIQUE_LEFT;
		else if(strcmp(property, "showuniqueright") == 0)
			*flags |= CF_SHOW_UNIQUE_RIGHT;

		else if(strcmp(property, "withicase") == 0)
		{
			*flags &= ~CF_RESPECT_CASE;
			*flags |= CF_IGNORE_CASE;
		}
		else if(strcmp(property, "withrcase") == 0)
		{
			*flags &= ~CF_IGNORE_CASE;
			*flags |= CF_RESPECT_CASE;
		}

		else
		{
			ui_sb_errf("Unknown comparison property: %s", property);
			return CMDS_ERR_CUSTOM;
		}
	}

	return 0;
}

/* Deletes a session. */
static int
delsession_cmd(const cmd_info_t *cmd_info)
{
	const char *session_name = cmd_info->argv[0];

	if(!sessions_exists(session_name))
	{
		ui_sb_errf("No stored sessions with such name: %s", session_name);
		return CMDS_ERR_CUSTOM;
	}

	if(sessions_remove(session_name) != 0)
	{
		ui_sb_errf("Failed to delete a session: %s", session_name);
		return CMDS_ERR_CUSTOM;
	}

	return 0;
}

static int
dirs_cmd(const cmd_info_t *cmd_info)
{
	return show_dirstack_menu(curr_view) != 0;
}

/* Maps key sequence in dialogs expanding mappings in RHS. */
static int
dmap_cmd(const cmd_info_t *cmd_info)
{
	return dialog_map(cmd_info, 0);
}

/* Maps key sequence in dialogs not expanding mappings in RHS. */
static int
dnoremap_cmd(const cmd_info_t *cmd_info)
{
	return dialog_map(cmd_info, 1);
}

/* Implementation of :dmap and :dnoremap.  Returns cmds unit friendly code. */
static int
dialog_map(const cmd_info_t *cmd_info, int no_remap)
{
	int result;
	if(cmd_info->argc <= 1)
	{
		result = do_map(cmd_info, "Dialog", SORT_MODE, no_remap);
	}
	else
	{
		result = do_map(cmd_info, "", SORT_MODE, no_remap);
		result = result == 0 ? do_map(cmd_info, "", ATTR_MODE, no_remap) : result;
		result = result == 0 ? do_map(cmd_info, "", CHANGE_MODE, no_remap) : result;
		result = result == 0
		       ? do_map(cmd_info, "", FILE_INFO_MODE, no_remap)
		       : result;
	}
	return result != 0;
}

/* Unmaps key sequence in dialogs. */
static int
dunmap_cmd(const cmd_info_t *cmd_info)
{
	const char *lhs = cmd_info->argv[0];
	int result = do_unmap(lhs, SORT_MODE);
	result = result == 0 ? do_unmap(lhs, ATTR_MODE) : result;
	result = result == 0 ? do_unmap(lhs, CHANGE_MODE) : result;
	result = result == 0 ? do_unmap(lhs, FILE_INFO_MODE) : result;
	return result != 0;
}

/* Evaluates arguments as expression and outputs result to status bar. */
static int
echo_cmd(const cmd_info_t *cmd_info)
{
	char *const eval_result = try_eval_args(cmd_info);
	if(eval_result == NULL)
	{
		return CMDS_ERR_CUSTOM;
	}

	ui_sb_msg(eval_result);
	free(eval_result);
	return 1;
}

/* Edits current/selected/specified file(s) in editor. */
static int
edit_cmd(const cmd_info_t *cmd_info)
{
	flist_set_marking(curr_view, 1);

	if(cmd_info->argc != 0)
	{
		if(stats_file_choose_action_set())
		{
			/* The call below does not return. */
			vifm_choose_files(curr_view, cmd_info->argc, cmd_info->argv);
		}

		(void)vim_edit_files(cmd_info->argc, cmd_info->argv);
		return 0;
	}

	dir_entry_t *entry = NULL;
	while(iter_marked_entries(curr_view, &entry))
	{
		char full_path[PATH_MAX + 1];
		get_full_path_of(entry, sizeof(full_path), full_path);

		if(!path_exists(full_path, DEREF) && path_exists(full_path, NODEREF))
		{
			show_error_msgf("Access error",
					"Can't access destination of link \"%s\". It might be broken.",
					full_path);
			return 0;
		}
	}

	if(stats_file_choose_action_set())
	{
		/* Reuse marking second time. */
		curr_view->pending_marking = 1;

		/* The call below does not return. */
		vifm_choose_files(curr_view, 0, NULL);
	}

	if(vim_edit_marking(curr_view) != 0)
	{
		show_error_msg("Edit error", "Can't edit selection");
	}
	return 0;
}

/* This command designates beginning of the alternative part of if-endif
 * statement. */
static int
else_cmd(const cmd_info_t *cmd_info)
{
	if(cmds_scoped_else() != 0)
	{
		ui_sb_err("Misplaced :else");
		return CMDS_ERR_CUSTOM;
	}
	return 0;
}

/* This command designates beginning of the alternative branch of if-endif
 * statement with its own condition. */
static int
elseif_cmd(const cmd_info_t *cmd_info)
{
	const int x = eval_if_condition(cmd_info);
	if(x < 0)
	{
		return CMDS_ERR_CUSTOM;
	}

	if(cmds_scoped_elseif(x) != 0)
	{
		ui_sb_err("Misplaced :elseif");
		return CMDS_ERR_CUSTOM;
	}

	return 0;
}

/* Starts process of emptying all trashes in background. */
static int
empty_cmd(const cmd_info_t *cmd_info)
{
	trash_empty_all();
	return 0;
}

/* This command ends conditional block. */
static int
endif_cmd(const cmd_info_t *cmd_info)
{
	if(cmds_scoped_endif() != 0)
	{
		ui_sb_err(":endif without :if");
		return CMDS_ERR_CUSTOM;
	}
	return 0;
}

/* This command composes a string from expressions and runs it as a command. */
static int
exe_cmd(const cmd_info_t *cmd_info)
{
	int result = 1;
	char *const eval_result = try_eval_args(cmd_info);
	if(eval_result != NULL)
	{
		result = cmds_dispatch(eval_result, curr_view, CIT_COMMAND);
		free(eval_result);
	}
	return result != 0;
}

/* Tries to evaluate a set of expressions and concatenate results with a space.
 * Returns pointer to newly allocated string, which should be freed by caller,
 * or NULL on error. */
static char *
try_eval_args(const cmd_info_t *cmd_info)
{
	char *eval_result;
	const char *error_pos = NULL;

	if(cmd_info->argc == 0)
	{
		return strdup("");
	}

	vle_tb_clear(vle_err);
	eval_result = cmds_eval_args(cmd_info->raw_args, &error_pos);

	if(eval_result == NULL)
	{
		vle_tb_append_linef(vle_err, "%s: %s", "Invalid expression", error_pos);
		ui_sb_err(vle_tb_get_data(vle_err));
	}

	return eval_result;
}

/* Displays file handler picking menu. */
static int
file_cmd(const cmd_info_t *cmd_info)
{
	if(cmd_info->argc == 0)
	{
		cmds_preserve_selection();
		return show_file_menu(curr_view, cmd_info->bg) != 0;
	}

	if(rn_open_with_match(curr_view, cmd_info->argv[0], cmd_info->bg) != 0)
	{
		ui_sb_err("Can't find associated program with requested beginning");
		return CMDS_ERR_CUSTOM;
	}

	return 0;
}

/* Registers non-x file association handler. */
static int
filetype_cmd(const cmd_info_t *cmd_info)
{
	return add_assoc(cmd_info, 0, 0);
}

/* Registers x file association handler. */
static int
filextype_cmd(const cmd_info_t *cmd_info)
{
	return add_assoc(cmd_info, 0, 1);
}

/* Registers external applications as file viewer scripts for files that match
 * name pattern.  Single argument form lists currently registered patterns that
 * match specified file name in menu mode. */
static int
fileviewer_cmd(const cmd_info_t *cmd_info)
{
	return add_assoc(cmd_info, 1, 0);
}

/* Registers x/non-x or viewer file association handler.  Single argument form
 * lists currently registered patterns that match specified file name in menu
 * mode.  Returns regular *_cmd handler value. */
static int
add_assoc(const cmd_info_t *cmd_info, int viewer, int for_x)
{
	char **matchers;
	int nmatchers;
	int i;
	const int in_x = (curr_stats.exec_env_type == EET_EMULATOR_WITH_X);
	const char *const records = vle_cmds_next_arg(cmd_info->args);

	if(cmd_info->argc == 1)
	{
		return viewer
		     ? (show_fileviewers_menu(curr_view, cmd_info->argv[0]) != 0)
		     : (show_fileprograms_menu(curr_view, cmd_info->argv[0]) != 0);
	}

	matchers = matchers_list(cmd_info->argv[0], &nmatchers);
	if(matchers == NULL)
	{
		return CMDS_ERR_NO_MEM;
	}

	for(i = 0; i < nmatchers; ++i)
	{
		char *error;
		matchers_t *const ms = matchers_alloc(matchers[i], 0, 1, "", &error);
		if(ms == NULL)
		{
			ui_sb_errf("Wrong pattern (%s): %s", matchers[i], error);
			free(error);
			free_string_array(matchers, nmatchers);
			return CMDS_ERR_CUSTOM;
		}

		if(viewer)
		{
			ft_set_viewers(ms, records);
		}
		else
		{
			ft_set_programs(ms, records, for_x, in_x);
		}
	}

	free_string_array(matchers, nmatchers);
	return 0;
}

/* Sets/displays/clears filters. */
static int
filter_cmd(const cmd_info_t *cmd_info)
{
	int ret;

	if(cmd_info->qmark)
	{
		display_filters_info(curr_view);
		return 1;
	}

	ret = update_filter(curr_view, cmd_info);
	if(curr_stats.global_local_settings)
	{
		int i;
		tab_info_t tab_info;
		for(i = 0; tabs_enum_all(i, &tab_info); ++i)
		{
			if(tab_info.view != curr_view)
			{
				ret |= update_filter(tab_info.view, cmd_info);
			}
		}
	}

	return ret;
}

/* Updates filters of the view. */
static int
update_filter(view_t *view, const cmd_info_t *cmd_info)
{
	const char *fallback = hists_search_last();

	if(cmd_info->argc == 0)
	{
		if(cmd_info->emark)
		{
			filters_invert(view);
			return 0;
		}

		/* When no arguments are provided, we don't want to fall back to last
		 * history entry. */
		fallback = "";
	}

	return set_view_filter(view, cmd_info->args, fallback,
			get_filter_inversion_state(cmd_info)) != 0;
}

/* Displays state of all filters on the status bar. */
static void
display_filters_info(const view_t *view)
{
	char *const localf = get_filter_info("Local", &view->local_filter.filter);
	char *const manualf = get_matcher_info("Explicit", view->manual_filter);
	char *const autof = get_filter_info("Implicit", &view->auto_filter);

	ui_sb_msgf("  Filter -- Flags -- Value\n%s\n%s\n%s", localf, manualf, autof);

	free(localf);
	free(manualf);
	free(autof);
}

/* Composes a description string for given filter.  Returns NULL on out of
 * memory error, otherwise a newly allocated string, which should be freed by
 * the caller, is returned. */
static char *
get_filter_info(const char name[], const filter_t *filter)
{
	const char *flags_str;

	if(filter_is_empty(filter))
	{
		flags_str = "";
	}
	else
	{
		flags_str = (filter->cflags & REG_ICASE) ? "i" : "I";
	}

	return format_str("%-8s    %-5s    %s", name, flags_str, filter->raw);
}

/* Composes description string for given matcher.  Returns NULL on out of
 * memory error, otherwise a newly allocated string, which should be freed by
 * the caller, is returned. */
static char *
get_matcher_info(const char name[], const matcher_t *matcher)
{
	const char *const flags = matcher_is_empty(matcher) ? "" : "---->";
	const char *const value = matcher_get_expr(matcher);
	return format_str("%-8s    %-5s    %s", name, flags, value);
}

/* Returns value for filter inversion basing on current configuration and
 * filter command. */
static int
get_filter_inversion_state(const cmd_info_t *cmd_info)
{
	int invert_filter = cfg.filter_inverted_by_default;
	if(cmd_info->emark)
	{
		invert_filter = !invert_filter;
	}
	return invert_filter;
}

/* Tries to update filter of the view rejecting incorrect regular expression.
 * On empty pattern fallback is used.  Returns non-zero if message on the
 * status bar should be saved, otherwise zero is returned. */
static int
set_view_filter(view_t *view, const char filter[], const char fallback[],
		int invert)
{
	char *error;
	matcher_t *const matcher = matcher_alloc(filter, FILTER_DEF_CASE_SENSITIVITY,
			0, fallback, &error);
	if(matcher == NULL)
	{
		ui_sb_errf("Name filter not set: %s", error);
		free(error);
		return CMDS_ERR_CUSTOM;
	}

	view->invert = invert;
	matcher_free(view->manual_filter);
	view->manual_filter = matcher;
	(void)filter_clear(&view->auto_filter);
	ui_view_schedule_reload(view);
	return 0;
}

/* Looks for files matching a pattern. */
static int
find_cmd(const cmd_info_t *cmd_info)
{
	return act_find(cmd_info->args, cmd_info->argc, cmd_info->argv);
}

static int
finish_cmd(const cmd_info_t *cmd_info)
{
	if(curr_stats.sourcing_state != SOURCING_PROCESSING)
	{
		ui_sb_err(":finish used outside of a sourced file");
		return CMDS_ERR_CUSTOM;
	}

	curr_stats.sourcing_state = SOURCING_FINISHING;
	cmds_scope_escape();
	return 0;
}

/* Changes view to have specified file/directory under the cursor. */
static int
goto_path_cmd(const cmd_info_t *cmd_info)
{
	char abs_path[PATH_MAX + 1];
	char *fname;

	char *const expanded = expand_tilde(cmd_info->argv[0]);
	to_canonic_path(expanded, flist_get_dir(curr_view), abs_path,
			sizeof(abs_path));
	free(expanded);

	if(is_root_dir(abs_path))
	{
		ui_sb_errf("Can't navigate to root directory: %s", abs_path);
		return CMDS_ERR_CUSTOM;
	}

	if(!path_exists(abs_path, NODEREF))
	{
		ui_sb_errf("Path doesn't exist: %s", abs_path);
		return CMDS_ERR_CUSTOM;
	}

	fname = strdup(get_last_path_component(abs_path));
	remove_last_path_component(abs_path);
	navigate_to_file(curr_view, abs_path, fname, 0);
	free(fname);
	return 0;
}

/* Handles :grep command with possible inversion of matches. */
static int
grep_cmd(const cmd_info_t *cmd_info)
{
	return act_grep(cmd_info->argc > 0 ? cmd_info->args : NULL, cmd_info->emark);
}

/* Displays documentation. */
static int
help_cmd(const cmd_info_t *cmd_info)
{
	char cmd[PATH_MAX + 1];
	int bg;

	const char *vi_cmd = cfg_get_vicmd(&bg);
	const int use_handler = vlua_handler_cmd(curr_stats.vlua, vi_cmd);

	if(cfg.use_vim_help)
	{
		const char *topic = (cmd_info->argc > 0) ? cmd_info->args : VIFM_VIM_HELP;

		if(use_handler)
		{
			if(vlua_open_help(curr_stats.vlua, vi_cmd, topic) != 0)
			{
				show_error_msgf(":help", "Failed to display help for %s via handler",
						topic);
			}
			return 0;
		}

		bg = vim_format_help_cmd(topic, cmd, sizeof(cmd));
	}
	else
	{
		if(cmd_info->argc != 0)
		{
			ui_sb_err("No arguments are allowed when 'vimhelp' option is off");
			return CMDS_ERR_CUSTOM;
		}

		char help_file[PATH_MAX + 1];
		build_path(help_file, sizeof(help_file), cfg.config_dir, VIFM_HELP);

		if(use_handler)
		{
			if(vlua_edit_one(curr_stats.vlua, vi_cmd, help_file, -1, -1, 0) != 0)
			{
				show_error_msg(":help", "Failed to open help file via handler");
			}
			return 0;
		}

		if(!path_exists(help_file, DEREF))
		{
			show_error_msgf("No help file", "Can't find \"%s\" file", help_file);
			return 0;
		}

		bg = format_help_cmd(cmd, sizeof(cmd));
	}

	if(bg)
	{
		bg_run_external(cmd, 0, SHELL_BY_APP, NULL);
	}
	else
	{
		display_help(cmd);
	}
	return 0;
}

/* Hides interface to show previous commands' output. */
static int
hideui_cmd(const cmd_info_t *cmd_info)
{
	ui_pause();
	return 0;
}

/* Handles :highlight command.  There are several forms:
 *  - highlight clear [file|column:name]
 *  - highlight file
 *  - highlight group
 *  - highlight column:name */
static int
highlight_cmd(const cmd_info_t *cmd_info)
{
	if(cmd_info->argc == 0)
	{
		ui_sb_msg(get_all_highlights());
		return 1;
	}

	if(strcasecmp(cmd_info->argv[0], "clear") == 0)
	{
		return highlight_clear(cmd_info);
	}

	if(starts_with_lit(cmd_info->argv[0], "column:"))
	{
		return highlight_column(cmd_info);
	}
	if(matchers_is_expr(cmd_info->argv[0]))
	{
		return highlight_file(cmd_info);
	}
	return highlight_group(cmd_info);
}

/* Handles clear form of :highlight command.  Returns value to be returned by
 * command handler. */
static int
highlight_clear(const cmd_info_t *cmd_info)
{
	if(cmd_info->argc == 2)
	{
		if(starts_with_lit(cmd_info->argv[1], "column:"))
		{
			int column_idx = find_column_id(cmd_info->argv[1]);
			if(column_idx < 0)
			{
				return CMDS_ERR_CUSTOM;
			}

			if(!cs_del_column_hi(curr_stats.cs, column_idx))
			{
				ui_sb_errf("No such group: %s", cmd_info->argv[1]);
				return CMDS_ERR_CUSTOM;
			}
		}
		else if(!cs_del_file_hi(cmd_info->argv[1]))
		{
			ui_sb_errf("No such group: %s", cmd_info->argv[1]);
			return CMDS_ERR_CUSTOM;
		}

		ui_invalidate_cs(curr_stats.cs);

		/* Redraw to update filename specific highlights. */
		stats_redraw_later();

		return 0;
	}

	if(cmd_info->argc == 1)
	{
		cs_reset(curr_stats.cs);
		ui_invalidate_cs(curr_stats.cs);

		/* Request full update instead of redraw to force recalculation of mixed
		 * colors like cursor line, which otherwise are not updated. */
		stats_refresh_later();
		return 0;
	}

	return CMDS_ERR_TRAILING_CHARS;
}

/* Handles highlight-column form of :highlight command.  Returns value to be
 * returned by command handler. */
static int
highlight_column(const cmd_info_t *cmd_info)
{
	int column_idx = find_column_id(cmd_info->argv[0]);
	if(column_idx < 0)
	{
		return 1;
	}

	/* XXX: start with an existing value, if present? */
	col_attr_t color = { .fg = -1, .bg = -1, .attr = 0, };
	int result = parse_highlight_attrs(cmd_info, &color);
	if(result != 0)
	{
		return result;
	}

	if(cmd_info->argc == 1)
	{
		const col_attr_t *col = cs_get_column_hi(curr_stats.cs, column_idx);
		if(col != NULL)
		{
			ui_sb_msg(get_column_hi_str(column_idx, col));
			return 1;
		}
		return 0;
	}

	if(cs_set_column_hi(curr_stats.cs, column_idx, &color) != 0)
	{
		ui_sb_err("Failed to add/update column color");
		return 1;
	}

	/* Redraw to update columns' look. */
	stats_redraw_later();

	return 0;
}

/* Looks up column id by column:name argument.  Returns the id or -1 on failure
 * and prints error message to the status bar. */
static int
find_column_id(const char arg[])
{
	(void)skip_prefix(&arg, "column:");

	int column_idx = ui_map_column_name(arg);
	if(column_idx < 0)
	{
		ui_sb_errf("No such column: %s", arg);
	}
	return column_idx;
}

/* Handles highlight-file form of :highlight command.  Returns value to be
 * returned by command handler. */
static int
highlight_file(const cmd_info_t *cmd_info)
{
	char pattern[strlen(cmd_info->args) + 1];
	/* XXX: start with an existing value, if present? */
	col_attr_t color = { .fg = -1, .bg = -1, .attr = 0, };
	int result;
	matchers_t *matchers;
	char *error;

	(void)extract_part(cmd_info->args, " \t", pattern);

	matchers = matchers_alloc(pattern, 0, 1, "", &error);
	if(matchers == NULL)
	{
		ui_sb_errf("Pattern error: %s", error);
		free(error);
		return CMDS_ERR_CUSTOM;
	}

	if(cmd_info->argc == 1)
	{
		display_file_highlights(matchers);
		matchers_free(matchers);
		return 1;
	}

	result = parse_highlight_attrs(cmd_info, &color);
	if(result != 0)
	{
		matchers_free(matchers);
		return result;
	}

	cs_add_file_hi(matchers, &color);
	/* We don't need to invalidate anything on startup and while loading a color
	 * scheme. */
	if(curr_stats.load_stage > 1 && curr_stats.cs->state != CSS_LOADING)
	{
		ui_invalidate_cs(curr_stats.cs);
	}

	/* Redraw to update filename specific highlights. */
	stats_redraw_later();

	return result;
}

/* Displays information about filename specific highlight on the status bar. */
static void
display_file_highlights(const matchers_t *matchers)
{
	int i;

	const col_scheme_t *cs = ui_view_get_cs(curr_view);

	for(i = 0; i < cs->file_hi_count; ++i)
	{
		if(matchers_includes(cs->file_hi[i].matchers, matchers))
		{
			break;
		}
	}

	if(i >= cs->file_hi_count)
	{
		ui_sb_errf("Highlight group not found: %s", matchers_get_expr(matchers));
		return;
	}

	ui_sb_msg(get_file_hi_str(cs->file_hi[i].matchers, &cs->file_hi[i].hi));
}

/* Handles highlight-group form of :highlight command.  Returns value to be
 * returned by command handler. */
static int
highlight_group(const cmd_info_t *cmd_info)
{
	int result;
	int group_id;
	col_attr_t tmp_color;
	col_attr_t *color;

	group_id = string_array_pos_case(HI_GROUPS, MAXNUM_COLOR, cmd_info->argv[0]);
	if(group_id < 0)
	{
		ui_sb_errf("Highlight group not found: %s", cmd_info->argv[0]);
		return CMDS_ERR_CUSTOM;
	}

	color = &curr_stats.cs->color[group_id];

	if(cmd_info->argc == 1)
	{
		ui_sb_msg(get_group_str(group_id, color));
		return 1;
	}

	tmp_color = *color;
	result = parse_highlight_attrs(cmd_info, &tmp_color);
	if(result != 0)
	{
		return result;
	}

	*color = tmp_color;
	curr_stats.cs->pair[group_id] = cs_load_color(color);

	/* Other highlight commands might have finished successfully, so update TUI.
	 * Request full update instead of redraw to force recalculation of mixed
	 * colors like cursor line, which otherwise are not updated. */
	stats_refresh_later();

	return result;
}

/* Composes string representation of all highlight group definitions.  Returns
 * pointer to statically allocated buffer. */
static const char *
get_all_highlights(void)
{
	static char msg[256*MAXNUM_COLOR];

	const col_scheme_t *cs = ui_view_get_cs(curr_view);
	size_t msg_len = 0U;
	int i;

	msg[0] = '\0';

	for(i = 0; i < MAXNUM_COLOR; ++i)
	{
		sstrappend(msg, &msg_len, sizeof(msg), get_group_str(i, &cs->color[i]));
		sstrappendch(msg, &msg_len, sizeof(msg), '\n');
	}

	int has_column_hi = 0;
	for(i = 0; i < cs->column_hi_count; ++i)
	{
		const col_attr_t *col = cs_get_column_hi(cs, i);
		if(col != NULL)
		{
			has_column_hi = 1;

			/* Different order is due to the fact that non-zero cs->column_hi_count
			 * doesn't necessarily imply that cs_get_column_hi() will return
			 * non-NULL even once. */
			sstrappendch(msg, &msg_len, sizeof(msg), '\n');
			sstrappend(msg, &msg_len, sizeof(msg), get_column_hi_str(i, col));
		}
	}

	if(cs->file_hi_count > 0)
	{
		sstrappend(msg, &msg_len, sizeof(msg), (has_column_hi ? "\n\n" : "\n"));
	}

	for(i = 0; i < cs->file_hi_count; ++i)
	{
		const file_hi_t *const file_hi = &cs->file_hi[i];
		const char *const line = get_file_hi_str(file_hi->matchers, &file_hi->hi);
		sstrappend(msg, &msg_len, sizeof(msg), line);
		sstrappendch(msg, &msg_len, sizeof(msg), '\n');
	}

	if(msg_len > 0 && msg[msg_len - 1] == '\n')
	{
		msg[msg_len - 1] = '\0';
	}

	return msg;
}

/* Composes string representation of highlight group definition.  Returns
 * pointer to a statically allocated buffer. */
static const char *
get_group_str(int group, const col_attr_t *col)
{
	return get_hi_str(HI_GROUPS[group], col);
}

/* Composes string representation of column highlight definition.  Returns
 * pointer to a statically allocated buffer. */
static const char *
get_column_hi_str(int index, const col_attr_t *col)
{
	char name[16];
	snprintf(name, sizeof(name), "column:%s", sort_enum[index]);
	return get_hi_str(name, col);
}

/* Composes string representation of filename specific highlight definition.
 * Returns pointer to a statically allocated buffer. */
static const char *
get_file_hi_str(const matchers_t *matchers, const col_attr_t *col)
{
	return get_hi_str(matchers_get_expr(matchers), col);
}

/* Composes string representation of highlight definition.  Returns pointer to a
 * statically allocated buffer. */
static const char *
get_hi_str(const char title[], const col_attr_t *col)
{
	static char buf[512];
	static char gui_buf[2*sizeof(buf)];

	char fg_buf[16], bg_buf[16];

	cs_color_to_str(col->fg, sizeof(fg_buf), fg_buf, /*is_gui=*/0);
	cs_color_to_str(col->bg, sizeof(bg_buf), bg_buf, /*is_gui=*/0);

	snprintf(buf, sizeof(buf), "%-10s cterm=%s ctermfg=%-7s ctermbg=%s",
			title, cs_attrs_to_str(col, /*gui_part=*/0), fg_buf, bg_buf);

	if(!col->gui_set)
	{
		return buf;
	}

	cs_color_to_str(col->gui_fg, sizeof(fg_buf), fg_buf, /*is_gui=*/1);
	cs_color_to_str(col->gui_bg, sizeof(bg_buf), bg_buf, /*is_gui=*/1);

	snprintf(gui_buf, sizeof(gui_buf), "%s\n%*s gui=%-6s guifg=%-9s guibg=%s",
			buf, (int)MAX(utf8_strsw(title), 10U), "",
			cs_attrs_to_str(col, /*gui_part=*/1), fg_buf, bg_buf);

	return gui_buf;
}

/* Parses arguments of :highlight command.  Returns non-zero in case of error
 * and prints a message on the status bar, on success zero is returned. */
static int
parse_highlight_attrs(const cmd_info_t *cmd_info, col_attr_t *color)
{
	int i;

	for(i = 1; i < cmd_info->argc; ++i)
	{
		const char *const arg = cmd_info->argv[i];
		const char *const equal = strchr(arg, '=');
		char arg_name[16];

		if(equal == NULL)
		{
			ui_sb_errf("Missing equal sign in \"%s\"", arg);
			return CMDS_ERR_CUSTOM;
		}
		if(equal[1] == '\0')
		{
			ui_sb_errf("Missing argument: %s", arg);
			return CMDS_ERR_CUSTOM;
		}

		copy_str(arg_name, MIN(sizeof(arg_name), (size_t)(equal - arg + 1)), arg);

		if(strcmp(arg_name, "ctermbg") == 0)
		{
			if(try_parse_cterm_color(equal + 1, 0, color) != 0)
			{
				/* Color not supported by the current terminal is not a hard error. */
				return 1;
			}
		}
		else if(strcmp(arg_name, "ctermfg") == 0)
		{
			if(try_parse_cterm_color(equal + 1, 1, color) != 0)
			{
				/* Color not supported by the current terminal is not a hard error. */
				return 1;
			}
		}
		else if(strcmp(arg_name, "guibg") == 0)
		{
			int value;
			if(try_parse_gui_color(equal + 1, &value) != 0)
			{
				/* Color not supported by the current terminal is not a hard error. */
				return 1;
			}

			cs_color_enable_gui(color);
			color->gui_bg = value;
		}
		else if(strcmp(arg_name, "guifg") == 0)
		{
			int value;
			if(try_parse_gui_color(equal + 1, &value) != 0)
			{
				/* Color not supported by the current terminal is not a hard error. */
				return 1;
			}

			cs_color_enable_gui(color);
			color->gui_fg = value;
		}
		else if(strcmp(arg_name, "cterm") == 0 || strcmp(arg_name, "gui") == 0)
		{
			int attrs;
			int combine_attrs;
			if((attrs = get_attrs(equal + 1, &combine_attrs)) == -1)
			{
				ui_sb_errf("Illegal argument: %s", equal + 1);
				return CMDS_ERR_CUSTOM;
			}

			if(strcmp(arg_name, "cterm") == 0)
			{
				color->attr = attrs;
				color->combine_attrs = combine_attrs;

				if(curr_stats.exec_env_type == EET_LINUX_NATIVE &&
						(attrs & (A_BOLD | A_REVERSE)) == (A_BOLD | A_REVERSE))
				{
					color->attr |= A_BLINK;
				}
			}
			else
			{
				cs_color_enable_gui(color);
				color->gui_attr = attrs;
				color->combine_gui_attrs = combine_attrs;
			}
		}
		else
		{
			ui_sb_errf("Illegal argument: %s", arg);
			return CMDS_ERR_CUSTOM;
		}
	}

	return 0;
}

/* Tries to parse color number or color name.  Returns non-zero if status bar
 * message should be preserved, otherwise zero is returned. */
static int
try_parse_cterm_color(const char str[], int is_fg, col_attr_t *color)
{
	col_scheme_t *const cs = curr_stats.cs;
	const int col_num = parse_color_name_value(str, is_fg, &color->attr);

	if(col_num < -1)
	{
		ui_sb_errf("Color name or number not recognized: %s", str);
		if(cs->state == CSS_LOADING)
		{
			cs->state = CSS_BROKEN;
		}

		return 1;
	}

	if(is_fg)
	{
		color->fg = col_num;
	}
	else
	{
		color->bg = col_num;
	}

	return 0;
}

/* Tries to parse a direct color.  Returns non-zero if status bar message should
 * be preserved, otherwise zero is returned. */
static int
try_parse_gui_color(const char str[], int *color)
{
	const char *hex_digits = "0123456789abcdefABCDEF";

	if(is_default_color(str))
	{
		*color = -1;
		return 0;
	}

	*color = string_array_pos_case(XTERM256_COLOR_NAMES,
			ARRAY_LEN(XTERM256_COLOR_NAMES), str);
	if(*color >= 0 && *color < 8)
	{
		return 0;
	}

	if(str[0] != '#' || strlen(str) != 7 || strspn(str + 1, hex_digits) != 6)
	{
		ui_sb_errf("Unrecognized color value format: %s", str);
		if(curr_stats.cs->state == CSS_LOADING)
		{
			curr_stats.cs->state = CSS_BROKEN;
		}
		return 1;
	}

	unsigned int value;
	(void)sscanf(str, "#%x", &value);

	*color = value;
	return 0;
}

/* Parses color string into color number and alters *attr in some cases.
 * Returns value less than -1 to indicate error as -1 is valid return value. */
static int
parse_color_name_value(const char str[], int fg, int *attr)
{
	int col_pos;
	int light_col_pos;
	int col_num;

	if(is_default_color(str))
	{
		return -1;
	}

	light_col_pos = string_array_pos_case(LIGHT_COLOR_NAMES,
			ARRAY_LEN(LIGHT_COLOR_NAMES), str);
	if(light_col_pos >= 0 && COLORS < 16)
	{
		*attr |= (!fg && curr_stats.exec_env_type == EET_LINUX_NATIVE) ?
				A_BLINK : A_BOLD;
		return light_col_pos;
	}

	col_pos = string_array_pos_case(XTERM256_COLOR_NAMES,
			ARRAY_LEN(XTERM256_COLOR_NAMES), str);
	if(col_pos >= 0)
	{
		if(!fg && curr_stats.exec_env_type == EET_LINUX_NATIVE)
		{
			*attr &= ~A_BLINK;
		}
		return col_pos;
	}

	col_num = isdigit(*str) ? atoi(str) : -1;
	if(col_num >= 0 && col_num < COLORS)
	{
		return col_num;
	}

	/* Fail if all possible parsing ways failed. */
	return -2;
}

/* Checks whether a string signifies a default color.  Returns non-zero if so,
 * otherwise zero is returned. */
static int
is_default_color(const char str[])
{
	return (strcmp(str, "-1") == 0)
	    || (strcasecmp(str, "default") == 0)
	    || (strcasecmp(str, "none") == 0);
}

/* Parses comma-separated list of attributes.  Returns parsed result or -1 on
 * error.  *combine_attrs is always assigned to. */
static int
get_attrs(const char text[], int *combine_attrs)
{
#ifdef HAVE_A_ITALIC_DECL
	const int italic_attr = A_ITALIC;
#else
	/* If A_ITALIC is missing (it's an extension), use A_REVERSE instead. */
	const int italic_attr = A_REVERSE;
#endif

	*combine_attrs = 0;

	int result = 0;
	while(*text != '\0')
	{
		const char *const p = until_first(text, ',');
		char buf[64];

		copy_str(buf, p - text + 1, text);
		if(strcasecmp(buf, "bold") == 0)
			result |= A_BOLD;
		else if(strcasecmp(buf, "underline") == 0)
			result |= A_UNDERLINE;
		else if(strcasecmp(buf, "reverse") == 0)
			result |= A_REVERSE;
		else if(strcasecmp(buf, "inverse") == 0)
			result |= A_REVERSE;
		else if(strcasecmp(buf, "standout") == 0)
			result |= A_STANDOUT;
		else if(strcasecmp(buf, "italic") == 0)
			result |= italic_attr;
		else if(strcasecmp(buf, "none") == 0)
			result = 0;
		else if(strcasecmp(buf, "combine") == 0)
			*combine_attrs = 1;
		else
			return -1;

		text = (*p == '\0') ? p : p + 1;
	}
	return result;
}

static int
history_cmd(const cmd_info_t *cmd_info)
{
	const char *const type = (cmd_info->argc == 0) ? "." : cmd_info->argv[0];
	const size_t len = strlen(type);

	if(strcmp(type, ":") == 0 || starts_withn("cmd", type, len))
		return show_cmdhistory_menu(curr_view) != 0;
	else if(strcmp(type, "/") == 0 || starts_withn("search", type, len) ||
			starts_withn("fsearch", type, len))
		return show_fsearchhistory_menu(curr_view) != 0;
	else if(strcmp(type, "?") == 0 || starts_withn("bsearch", type, len))
		return show_bsearchhistory_menu(curr_view) != 0;
	else if(strcmp(type, "@") == 0 || starts_withn("input", type, len))
		return show_prompthistory_menu(curr_view) != 0;
	else if(strcmp(type, "=") == 0 || starts_withn("filter", type, MAX(2U, len)))
		return show_filterhistory_menu(curr_view) != 0;
	else if(starts_withn("exprreg", type, len))
		return show_exprreghistory_menu(curr_view) != 0;
	else if(starts_withn("mcmd", type, len))
		return show_menucmdhistory_menu(curr_view) != 0;
	else if(strcmp(type, ".") == 0 || starts_withn("dir", type, len))
		return show_history_menu(curr_view) != 0;
	else
		return CMDS_ERR_TRAILING_CHARS;
}

/* Goes forward though directory history. */
static int
histnext_cmd(const cmd_info_t *cmd_info)
{
	flist_hist_go_forward(curr_view);
	return 0;
}

/* Goes backward though directory history. */
static int
histprev_cmd(const cmd_info_t *cmd_info)
{
	flist_hist_go_back(curr_view);
	return 0;
}

/* This command starts conditional block. */
static int
if_cmd(const cmd_info_t *cmd_info)
{
	const int x = eval_if_condition(cmd_info);
	if(x < 0)
	{
		return CMDS_ERR_CUSTOM;
	}

	cmds_scoped_if(x);
	return 0;
}

/* Evaluates condition for if-endif statement.  Returns negative number on
 * error, zero for expression that's evaluated to false and positive number for
 * true expressions. */
static int
eval_if_condition(const cmd_info_t *cmd_info)
{
	vle_tb_clear(vle_err);

	int result;

	parsing_result_t parsing_result =
		vle_parser_eval(cmd_info->args, /*interactive=*/1);
	if(parsing_result.error != PE_NO_ERROR)
	{
		vle_tb_append_linef(vle_err, "%s: %s", "Invalid expression",
				cmd_info->args);
		ui_sb_err(vle_tb_get_data(vle_err));
		result = CMDS_ERR_CUSTOM;
	}
	else
	{
		result = var_to_bool(parsing_result.value);
	}

	var_free(parsing_result.value);
	return result;
}

static int
invert_cmd(const cmd_info_t *cmd_info)
{
	const char *const type_str = (cmd_info->argc == 0) ? "f" : cmd_info->argv[0];
	const char state_type = type_str[0];

	if(type_str[1] != '\0' || !char_is_one_of("fso", type_str[0]))
	{
		return CMDS_ERR_INVALID_ARG;
	}

	if(cmd_info->qmark)
	{
		print_inversion_state(state_type);
		return 1;
	}
	else
	{
		invert_state(state_type);
		return 0;
	}
}

/* Prints inversion state of the feature specified by the state_type
 * argument. */
static void
print_inversion_state(char state_type)
{
	if(state_type == 'f')
	{
		ui_sb_msgf("Filter is %sinverted", curr_view->invert ? "" : "not ");
	}
	else if(state_type == 's')
	{
		ui_sb_msg("Selection does not have inversion state");
	}
	else if(state_type == 'o')
	{
		ui_sb_msgf("Primary key is sorted in %s order",
				(curr_view->sort[0] > 0) ? "ascending" : "descending");
	}
	else
	{
		assert(0 && "Unexpected state type.");
	}
}

/* Inverts state of the feature specified by the state_type argument. */
static void
invert_state(char state_type)
{
	if(state_type == 'f')
	{
		filters_invert(curr_view);
	}
	else if(state_type == 's')
	{
		flist_sel_invert(curr_view);
		redraw_view(curr_view);
		cmds_preserve_selection();
	}
	else if(state_type == 'o')
	{
		invert_sorting_order(curr_view);
		resort_dir_list(1, curr_view);
		load_sort_option(curr_view);
		redraw_view(curr_view);
	}
	else
	{
		assert(0 && "Unexpected state type.");
	}
}

static int
jobs_cmd(const cmd_info_t *cmd_info)
{
	return show_jobs_menu(curr_view) != 0;
}

/* Change default from resetting selection at the end of a command to keeping
 * it. */
static int
keepsel_cmd(const cmd_info_t *cmd_info)
{
	return cmds_exec(cmd_info->args, curr_view, /*menu=*/0, /*keep_sel=*/1);
}

static int
let_cmd(const cmd_info_t *cmd_info)
{
	vle_tb_clear(vle_err);
	if(let_variables(cmd_info->args) != 0)
	{
		ui_sb_err(vle_tb_get_data(vle_err));
		return CMDS_ERR_CUSTOM;
	}
	else if(*vle_tb_get_data(vle_err) != '\0')
	{
		ui_sb_msg(vle_tb_get_data(vle_err));
	}
	update_path_env(0);
	return 0;
}

static int
locate_cmd(const cmd_info_t *cmd_info)
{
	static char *last_args;
	if(cmd_info->argc > 0)
	{
		(void)replace_string(&last_args, cmd_info->args);
	}
	else if(last_args == NULL)
	{
		ui_sb_err("Nothing to repeat");
		return CMDS_ERR_CUSTOM;
	}
	return show_locate_menu(curr_view, last_args) != 0;
}

/* Lists active windows of terminal multiplexer in use, if any. */
static int
ls_cmd(const cmd_info_t *cmd_info)
{
	switch(curr_stats.term_multiplexer)
	{
		case TM_NONE:
			ui_sb_msg("No terminal multiplexer is in use");
			return 1;
		case TM_SCREEN:
			(void)vifm_system("screen -X eval windowlist", SHELL_BY_APP);
			return 0;
		case TM_TMUX:
			if(vifm_system("tmux choose-window", SHELL_BY_APP) != EXIT_SUCCESS)
			{
				/* Refresh all windows as failed command outputs message, which can't be
				 * suppressed. */
				update_all_windows();
				/* Fall back to worse way of doing the same for tmux versions < 1.8. */
				(void)vifm_system("tmux command-prompt choose-window", SHELL_BY_APP);
			}
			return 0;

		default:
			assert(0 && "Unknown active terminal multiplexer value");
			ui_sb_msg("Unknown terminal multiplexer is in use");
			return 1;
	}
}

/* Lists files in trash. */
static int
lstrash_cmd(const cmd_info_t *cmd_info)
{
	return show_trash_menu(curr_view) != 0;
}

static int
map_cmd(const cmd_info_t *cmd_info)
{
	return map_or_remap(cmd_info, 0);
}

static int
mark_cmd(const cmd_info_t *cmd_info)
{
	int result;
	char *expanded_path;
	const char *file;
	char mark = cmd_info->argv[0][0];

	if(cmd_info->argv[0][1] != '\0')
		return CMDS_ERR_TRAILING_CHARS;

	if(cmd_info->qmark)
	{
		if(!marks_is_empty(curr_view, mark))
		{
			ui_sb_errf("Mark isn't empty: %c", mark);
			return CMDS_ERR_CUSTOM;
		}
	}

	if(cmd_info->argc == 1)
	{
		const int pos = (cmd_info->end == NOT_DEF)
		              ? curr_view->list_pos
		              : cmd_info->end;
		const dir_entry_t *const entry = &curr_view->dir_entry[pos];
		return marks_set_user(curr_view, mark, entry->origin, entry->name);
	}

	expanded_path = expand_tilde(cmd_info->argv[1]);
	if(!is_path_absolute(expanded_path))
	{
		free(expanded_path);
		ui_sb_err("Expected full path to the directory");
		return CMDS_ERR_CUSTOM;
	}

	if(cmd_info->argc == 2)
	{
		if(cmd_info->end == NOT_DEF || !pane_in_dir(curr_view, expanded_path))
		{
			if(curr_stats.load_stage >= 3 && pane_in_dir(curr_view, expanded_path))
			{
				file = get_current_file_name(curr_view);
			}
			else
			{
				file = NO_MARK_FILE;
			}
		}
		else
		{
			file = curr_view->dir_entry[cmd_info->end].name;
		}
	}
	else
	{
		file = cmd_info->argv[2];
	}
	result = marks_set_user(curr_view, mark, expanded_path, file);
	free(expanded_path);

	return result;
}

/* Displays all or some of marks. */
static int
marks_cmd(const cmd_info_t *cmd_info)
{
	char buf[256];
	int i, j;

	if(cmd_info->argc == 0)
	{
		return show_marks_menu(curr_view, marks_all) != 0;
	}

	j = 0;
	buf[0] = '\0';
	for(i = 0; i < cmd_info->argc; i++)
	{
		const char *p = cmd_info->argv[i];
		while(*p != '\0')
		{
			if(strchr(buf, *p) == NULL)
			{
				buf[j++] = *p;
				buf[j] = '\0';
			}
			p++;
		}
	}
	return show_marks_menu(curr_view, buf) != 0;
}

#ifndef _WIN32

/* Shows a menu for managing media. */
static int
media_cmd(const cmd_info_t *cmd_info)
{
	return (show_media_menu(curr_view) != 0);
}

#endif

static int
messages_cmd(const cmd_info_t *cmd_info)
{
	/* TODO: move this code to ui.c */
	char *lines;
	size_t len;
	int count;
	int t;

	lines = NULL;
	len = 0;
	count = curr_stats.msg_tail - curr_stats.msg_head;
	if(count < 0)
		count += ARRAY_LEN(curr_stats.msgs);
	t = (curr_stats.msg_head + 1) % ARRAY_LEN(curr_stats.msgs);
	while(count-- > 0)
	{
		const char *msg = curr_stats.msgs[t];
		char *new_lines = realloc(lines, len + 1 + strlen(msg) + 1);
		if(new_lines != NULL)
		{
			lines = new_lines;
			len += sprintf(lines + len, "%s%s", (len == 0) ? "": "\n", msg);
			t = (t + 1) % ARRAY_LEN(curr_stats.msgs);
		}
	}

	if(lines == NULL)
		return 0;

	curr_stats.save_msg_in_list = 0;
	curr_stats.allow_sb_msg_truncation = 0;
	ui_sb_msg(lines);
	curr_stats.allow_sb_msg_truncation = 1;
	curr_stats.save_msg_in_list = 1;

	free(lines);
	return 1;
}

/* :mkdir creates sub-directory.  :mkdir! creates chain of directories.  In
 * second case both relative and absolute paths are allowed. */
static int
mkdir_cmd(const cmd_info_t *cmd_info)
{
	const int at = get_at(curr_view, cmd_info);
	return fops_mkdirs(curr_view, at, cmd_info->argv, cmd_info->argc,
			cmd_info->emark) != 0;
}

static int
mmap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "Menu", MENU_MODE, 0) != 0;
}

static int
mnoremap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "Menu", MENU_MODE, 1) != 0;
}

/* Moves files. */
static int
move_cmd(const cmd_info_t *cmd_info)
{
	return cpmv_cmd(cmd_info, 1);
}

/* Common part of copy and move commands interface implementation. */
static int
cpmv_cmd(const cmd_info_t *cmd_info, int move)
{
	const CopyMoveLikeOp op = move ? CMLO_MOVE : CMLO_COPY;

	flist_set_marking(curr_view, 0);

	int argc = cmd_info->argc;
	char **argv = cmd_info->argv;
	int flags = parse_cpmv_flags(&argc, &argv);
	if(flags < 0)
	{
		return CMDS_ERR_CUSTOM;
	}

	flags |= (cmd_info->emark ? CMLF_FORCE : CMLF_NONE);

	if(cmd_info->qmark)
	{
		if(argc > 0)
		{
			ui_sb_err("No positional arguments are allowed if you use \"?\"");
			return CMDS_ERR_CUSTOM;
		}

		if(cmd_info->bg)
		{
			return fops_cpmv_bg(curr_view, NULL, -1, move, flags) != 0;
		}

		return fops_cpmv(curr_view, NULL, -1, op, flags) != 0;
	}

	if(cmd_info->bg)
	{
		return fops_cpmv_bg(curr_view, argv, argc, move, flags) != 0;
	}

	return fops_cpmv(curr_view, argv, argc, op, flags) != 0;
}

static int
munmap_cmd(const cmd_info_t *cmd_info)
{
	return do_unmap(cmd_info->argv[0], MENU_MODE);
}

static int
nmap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "Normal", NORMAL_MODE, 0) != 0;
}

static int
nnoremap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "Normal", NORMAL_MODE, 1) != 0;
}

/* Resets file selection and search highlight. */
static int
nohlsearch_cmd(const cmd_info_t *cmd_info)
{
	ui_view_reset_search_highlight(curr_view);
	flist_sel_stash_if_nonempty(curr_view);
	return 0;
}

static int
noremap_cmd(const cmd_info_t *cmd_info)
{
	return map_or_remap(cmd_info, 1);
}

static int
map_or_remap(const cmd_info_t *cmd_info, int no_remap)
{
	int result;
	if(cmd_info->emark)
	{
		result = do_map(cmd_info, "", CMDLINE_MODE, no_remap);
	}
	else
	{
		result = do_map(cmd_info, "", NORMAL_MODE, no_remap);
		if(result == 0)
			result = do_map(cmd_info, "", VISUAL_MODE, no_remap);
	}
	return result != 0;
}

/* Executes normal mode commands. */
static int
normal_cmd(const cmd_info_t *cmd_info)
{
	wchar_t *const wide = to_wide(cmd_info->args);
	if(wide == NULL)
	{
		show_error_msgf("Command Error", "Failed to convert to wide string: %s",
				cmd_info->args);
		return 0;
	}

	vle_mode_t old_primary_mode = vle_mode_get_primary();
	vle_mode_t old_current_mode = vle_mode_get();
	vle_mode_set(NORMAL_MODE, VMT_PRIMARY);

	if(cmd_info->emark)
	{
		(void)vle_keys_exec_timed_out_no_remap(wide);
	}
	else
	{
		(void)vle_keys_exec_timed_out(wide);
	}
	free(wide);

	/* Force leaving command-line mode if the input contains unfinished ":". */
	if(modes_is_cmdline_like())
	{
		(void)vle_keys_exec_timed_out(WK_C_c);
	}

	/* Don't restore the mode if input sequence led to a mode change as it was
	 * probably the intention of the user. */
	if(vle_mode_is(NORMAL_MODE))
	{
		/* Relative order of the calls is important. */
		vle_mode_set(old_primary_mode, VMT_PRIMARY);
		vle_mode_set(old_current_mode, VMT_SECONDARY);
	}

	cmds_preserve_selection();
	return 0;
}

static int
nunmap_cmd(const cmd_info_t *cmd_info)
{
	return do_unmap(cmd_info->argv[0], NORMAL_MODE);
}

static int
only_cmd(const cmd_info_t *cmd_info)
{
	only();
	return 0;
}

/* Manages plugins. */
static int
plugin_cmd(const cmd_info_t *cmd_info)
{
	if(strcmp(cmd_info->argv[0], "load") == 0)
	{
		if(cmd_info->argc != 1)
		{
			return CMDS_ERR_TRAILING_CHARS;
		}

		plugs_load(curr_stats.plugs, curr_stats.plugins_dirs);
		return 0;
	}

	if(cmd_info->argc != 2)
	{
		return CMDS_ERR_TOO_FEW_ARGS;
	}

	if(strcmp(cmd_info->argv[0], "blacklist") == 0)
	{
		plugs_blacklist(curr_stats.plugs, cmd_info->argv[1]);
		return 0;
	}
	if(strcmp(cmd_info->argv[0], "whitelist") == 0)
	{
		plugs_whitelist(curr_stats.plugs, cmd_info->argv[1]);
		return 0;
	}

	ui_sb_errf("Unknown subcommand: %s", cmd_info->argv[0]);
	return CMDS_ERR_CUSTOM;
}

/* Displays plugins menu. */
static int
plugins_cmd(const cmd_info_t *cmd_info)
{
	return (show_plugins_menu(curr_view) != 0);
}

static int
popd_cmd(const cmd_info_t *cmd_info)
{
	if(dir_stack_pop() != 0)
	{
		ui_sb_msg("Directory stack empty");
		return 1;
	}
	return 0;
}

static int
pushd_cmd(const cmd_info_t *cmd_info)
{
	if(cmd_info->argc == 0)
	{
		if(dir_stack_swap() != 0)
		{
			ui_sb_err("No other directories");
			return CMDS_ERR_CUSTOM;
		}
		return 0;
	}
	if(dir_stack_push_current() != 0)
	{
		show_error_msg("Memory Error", "Unable to allocate enough memory");
		return 0;
	}
	cd_cmd(cmd_info);
	return 0;
}

/* Puts files from the register (default register unless otherwise specified)
 * into current directory.  Can operate in background. */
static int
put_cmd(const cmd_info_t *cmd_info)
{
	int reg = DEFAULT_REG_NAME;
	const int at = get_at(curr_view, cmd_info);

	if(cmd_info->argc == 1)
	{
		const int error = get_reg(cmd_info->argv[0], &reg);
		if(error != 0)
		{
			return error;
		}
	}

	if(cmd_info->bg)
	{
		return fops_put_bg(curr_view, at, reg, cmd_info->emark) != 0;
	}

	return fops_put(curr_view, at, reg, cmd_info->emark) != 0;
}

static int
pwd_cmd(const cmd_info_t *cmd_info)
{
	ui_sb_msg(flist_get_dir(curr_view));
	return 1;
}

static int
qmap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "View", VIEW_MODE, 0) != 0;
}

static int
qnoremap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "View", VIEW_MODE, 1) != 0;
}

static int
qunmap_cmd(const cmd_info_t *cmd_info)
{
	return do_unmap(cmd_info->argv[0], VIEW_MODE);
}

/* Immediately redraws the screen. */
static int
redraw_cmd(const cmd_info_t *cmd_info)
{
	update_screen(UT_FULL);
	return 0;
}

/* Opens external editor to edit specified register contents. */
static int
regedit_cmd(const cmd_info_t *cmd_info)
{
	int reg_name = DEFAULT_REG_NAME;
	if(cmd_info->argc > 0)
	{
		if(strlen(cmd_info->argv[0]) != 1)
		{
			ui_sb_errf("Invalid argument: %s", cmd_info->argv[0]);
			return CMDS_ERR_CUSTOM;
		}
		reg_name = tolower(cmd_info->argv[0][0]);
	}
	if(reg_name == BLACKHOLE_REG_NAME)
	{
		ui_sb_err("Cannot modify blackhole register.");
		return CMDS_ERR_CUSTOM;
	}

	regs_sync_from_shared_memory();
	const reg_t *reg = regs_find(reg_name);
	if(reg == NULL)
	{
		ui_sb_err("Register with given name does not exist.");
		return CMDS_ERR_CUSTOM;
	}

	char tmp_fname[PATH_MAX + 1];
	FILE *file = make_file_in_tmp("vifm.regedit", 0600, /*auto_delete=*/0,
			tmp_fname, sizeof(tmp_fname));
	if(file == NULL)
	{
		ui_sb_err("Couldn't write register content into external file.");
		return CMDS_ERR_CUSTOM;
	}

	write_lines_to_file(file, reg->files, reg->nfiles);
	fclose(file);

	/* Forking is disabled because in some cases process can return value
	 * before any changes will be written and editor will be closed. */
	const int edit_result = vim_view_file(tmp_fname, 1, 1, /*allow_forking=*/0);
	if(edit_result != 0)
	{
		ui_sb_err("Register content edition went unsuccessful.");
		return CMDS_ERR_CUSTOM;
	}

	int read_lines;
	char **edited_content = read_file_of_lines(tmp_fname, &read_lines);
	int error = errno;
	unlink(tmp_fname);
	if(edited_content == NULL)
	{
		ui_sb_errf("Couldn't read edited register's content: %s", strerror(error));
		return CMDS_ERR_CUSTOM;
	}

	/* Normalize paths.  Not done in regs_set() as it doesn't need to know about
	 * current directory. */
	int i;
	const char *base_dir = flist_get_dir(curr_view);
	for(i = 0; i < read_lines; ++i)
	{
		char canonic[PATH_MAX + 1];
		to_canonic_path(edited_content[i], base_dir, canonic, sizeof(canonic));
		replace_string(&edited_content[i], canonic);
	}

	regs_set(reg_name, edited_content, read_lines);
	free_string_array(edited_content, read_lines);

	regs_sync_to_shared_memory();
	return 0;
}

/* Displays menu listing contents of registers (all or just specified ones). */
static int
registers_cmd(const cmd_info_t *cmd_info)
{
	char reg_names[256];
	int i, j;

	if(cmd_info->argc == 0)
	{
		return show_register_menu(curr_view, valid_registers) != 0;
	}

	/* Format list of unique register names. */
	j = 0;
	reg_names[0] = '\0';
	for(i = 0; i < cmd_info->argc; ++i)
	{
		const char *p = cmd_info->argv[i];
		while(*p != '\0')
		{
			if(strchr(reg_names, *p) == NULL)
			{
				reg_names[j++] = *p;
				reg_names[j] = '\0';
			}
			++p;
		}
	}

	return show_register_menu(curr_view, reg_names) != 0;
}

/* Switches to regular view leaving custom view. */
static int
regular_cmd(const cmd_info_t *cmd_info)
{
	if(flist_custom_active(curr_view))
	{
		rn_leave(curr_view, 1);
	}
	return 0;
}

/* Renames selected files of the current view. */
static int
rename_cmd(const cmd_info_t *cmd_info)
{
	flist_set_marking(curr_view, 0);
	return fops_rename(curr_view, cmd_info->argv, cmd_info->argc,
			cmd_info->emark) != 0;
}

/* Resets internal state and reloads configuration files. */
static int
restart_cmd(const cmd_info_t *cmd_info)
{
	int full = 0;
	if(cmd_info->argc == 1)
	{
		if(strcmp(cmd_info->argv[0], "full") != 0)
		{
			ui_sb_errf("Unexpected argument: %s", cmd_info->argv[0]);
			return CMDS_ERR_CUSTOM;
		}
		full = 1;
	}

	(void)restart_into_session(cfg.session, full);
	return 0;
}

static int
restore_cmd(const cmd_info_t *cmd_info)
{
	flist_set_marking(curr_view, 0);
	return fops_restore(curr_view) != 0;
}

/* Creates symbolic links with relative paths to files. */
static int
rlink_cmd(const cmd_info_t *cmd_info)
{
	return link_cmd(cmd_info, 0);
}

/* Common part of alink and rlink commands interface implementation. */
static int
link_cmd(const cmd_info_t *cmd_info, int absolute)
{
	const CopyMoveLikeOp op = absolute ? CMLO_LINK_ABS : CMLO_LINK_REL;

	int argc = cmd_info->argc;
	char **argv = cmd_info->argv;
	int flags = parse_cpmv_flags(&argc, &argv);
	if(flags < 0)
	{
		return CMDS_ERR_CUSTOM;
	}

	flags |= (cmd_info->emark ? CMLF_FORCE : CMLF_NONE);

	flist_set_marking(curr_view, 0);

	if(cmd_info->qmark)
	{
		if(argc > 0)
		{
			ui_sb_err("No positional arguments are allowed if you use \"?\"");
			return CMDS_ERR_CUSTOM;
		}
		return fops_cpmv(curr_view, NULL, -1, op, flags) != 0;
	}

	return fops_cpmv(curr_view, argv, argc, op, flags) != 0;
}

/* Parses leading copy/move options and adjusts argc/argv to exclude them.
 * Returns -1 on parsing error, otherwise combination of CMLF_* values is
 * returned. */
static int
parse_cpmv_flags(int *argc, char ***argv)
{
	int flags = 0;

	int i;
	for(i = 0; i < *argc; ++i)
	{
		if(argv[0][i][0] != '-')
		{
			/* Implicit end of options. */
			break;
		}
		if(strcmp(argv[0][i], "--") == 0)
		{
			++i;
			/* Explicit end of options. */
			break;
		}

		if(strcmp(argv[0][i], "-skip") == 0)
		{
			flags |= CMLF_SKIP;
		}
		else
		{
			ui_sb_errf("Unrecognized :command option: %s", argv[0][i]);
			return -1;
		}
	}

	*argc -= i;
	*argv += i;

	return flags;
}

/* Shows status of terminal multiplexers support, sets it or toggles it. */
static int
screen_cmd(const cmd_info_t *cmd_info)
{
	if(cmd_info->qmark)
	{
		if(cfg.use_term_multiplexer)
		{
			if(curr_stats.term_multiplexer != TM_NONE)
			{
				ui_sb_msgf("Integration with %s is active",
						(curr_stats.term_multiplexer == TM_SCREEN) ? "GNU screen" : "tmux");
			}
			else
			{
				ui_sb_msg("Integration with terminal multiplexers is enabled but "
						"inactive");
			}
		}
		else
		{
			ui_sb_msg("Integration with terminal multiplexers is disabled");
		}
		return 1;
	}

	if(cmd_info->emark)
	{
		cfg_set_use_term_multiplexer(1);
	}
	else
	{
		cfg_set_use_term_multiplexer(!cfg.use_term_multiplexer);
	}

	return 0;
}

/* Selects files that match passed in expression or range. */
static int
select_cmd(const cmd_info_t *cmd_info)
{
	int error;
	cmds_preserve_selection();

	/* If no arguments are passed, select the range. */
	if(cmd_info->argc == 0)
	{
		/* Append to previous selection unless ! is specified. */
		if(cmd_info->emark)
		{
			flist_sel_drop(curr_view);
		}

		flist_sel_by_range(curr_view, cmd_info->begin, cmd_info->end, 1);
		return 0;
	}

	if(cmd_info->begin != NOT_DEF)
	{
		ui_sb_err("Either range or argument should be supplied.");
		error = 1;
	}
	else if(cmd_info->args[0] == '!' && !char_is_one_of("/{<", cmd_info->args[1]))
	{
		error = flist_sel_by_filter(curr_view, cmd_info->args + 1, cmd_info->emark,
				1);
	}
	else
	{
		error = flist_sel_by_pattern(curr_view, cmd_info->args, cmd_info->emark, 1);
	}

	return (error ? CMDS_ERR_CUSTOM : 0);
}

/* Displays current session, detaches from a session or switches to a (possibly
 * new) session. */
static int
session_cmd(const cmd_info_t *cmd_info)
{
	if(cmd_info->qmark)
	{
		if(sessions_active())
		{
			ui_sb_msgf("Active session: %s", sessions_current());
		}
		else
		{
			ui_sb_msg("No active session");
		}
		return 1;
	}

	if(cmd_info->argc == 0)
	{
		char *current = strdup(sessions_current());
		if(sessions_stop() == 0)
		{
			ui_sb_msgf("Detached from session without saving: %s", current);
			put_string(&curr_stats.last_session, current);
			return 1;
		}
		ui_sb_msg("No active session");
		free(current);
		return 1;
	}

	const char *session_name = cmd_info->argv[0];
	if(contains_slash(session_name))
	{
		ui_sb_err("Session name can't include path separators");
		return CMDS_ERR_CUSTOM;
	}

	if(strcmp(session_name, "-") == 0)
	{
		if(is_null_or_empty(curr_stats.last_session))
		{
			ui_sb_err("No previous session");
			return CMDS_ERR_CUSTOM;
		}
		if(!sessions_exists(curr_stats.last_session))
		{
			ui_sb_err("Previous session doesn't exist");
			return CMDS_ERR_CUSTOM;
		}

		session_name = curr_stats.last_session;
	}

	char *old_current_session = NULL;
	update_string(&old_current_session, sessions_current());

	if(switch_to_a_session(session_name) == 0)
	{
		update_string(&curr_stats.last_session, old_current_session);
	}

	free(old_current_session);
	return 1;
}

/* Performs switch to a session by its name.  Always prints status bar message.
 * Returns zero on success, otherwise non-zero is returned. */
static int
switch_to_a_session(const char session_name[])
{
	if(sessions_active())
	{
		if(sessions_current_is(session_name))
		{
			ui_sb_msgf("Already active session: %s", session_name);
			return 1;
		}

		state_store();
	}

	if(sessions_create(session_name) == 0)
	{
		ui_sb_msgf("Switched to a new session: %s", sessions_current());
		return 0;
	}

	if(restart_into_session(session_name, 0) != 0)
	{
		if(sessions_active())
		{
			ui_sb_errf("Session switching has failed, active session: %s",
					sessions_current());
		}
		else
		{
			ui_sb_err("Session switching has failed, no active session");
		}
		return CMDS_ERR_CUSTOM;
	}

	ui_sb_msgf("Loaded session: %s", sessions_current());
	return 0;
}

/* Performs restart and optional (re)loading of a session.  Returns zero on
 * success, otherwise non-zero is returned. */
static int
restart_into_session(const char session[], int full)
{
	instance_start_restart();

	if(full || session != NULL)
	{
		tabs_reinit();
	}

	int result;
	if(session == NULL)
	{
		state_load(!full);
		result = 0;
	}
	else
	{
		result = sessions_load(session);
	}

	instance_finish_restart();
	return result;
}

/* Updates/displays global and local options. */
static int
set_cmd(const cmd_info_t *cmd_info)
{
	const int result = process_set_args(cmd_info->args, 1, 1);
	return (result < 0) ? CMDS_ERR_CUSTOM : (result != 0);
}

/* Updates/displays only global options. */
static int
setglobal_cmd(const cmd_info_t *cmd_info)
{
	const int result = process_set_args(cmd_info->args, 1, 0);
	return (result < 0) ? CMDS_ERR_CUSTOM : (result != 0);
}

/* Updates/displays only local options. */
static int
setlocal_cmd(const cmd_info_t *cmd_info)
{
	const int result = process_set_args(cmd_info->args, 0, 1);
	return (result < 0) ? CMDS_ERR_CUSTOM : (result != 0);
}

static int
shell_cmd(const cmd_info_t *cmd_info)
{
	rn_shell(NULL, PAUSE_NEVER, cmd_info->emark ? 0 : 1, SHELL_BY_APP);
	return 0;
}

/* Navigates to [count]th next sibling directory. */
static int
siblnext_cmd(const cmd_info_t *cmd_info)
{
	const int count = (cmd_info->count == NOT_DEF ? 1 : cmd_info->count);
	return (go_to_sibling_dir(curr_view, count, cmd_info->emark) != 0);
}

/* Navigates to [count]th previous sibling directory. */
static int
siblprev_cmd(const cmd_info_t *cmd_info)
{
	const int count = (cmd_info->count == NOT_DEF ? 1 : cmd_info->count);
	return (go_to_sibling_dir(curr_view, -count, cmd_info->emark) != 0);
}

static int
sort_cmd(const cmd_info_t *cmd_info)
{
	enter_sort_mode(curr_view);
	cmds_preserve_selection();
	return 0;
}

static int
source_cmd(const cmd_info_t *cmd_info)
{
	int ret = 0;
	char *path = expand_tilde(cmd_info->argv[0]);

	if(!path_exists(path, DEREF))
	{
		ui_sb_errf("Sourced file doesn't exist: %s", cmd_info->argv[0]);
		ret = CMDS_ERR_CUSTOM;
	}
	else if(os_access(path, R_OK) != 0)
	{
		ui_sb_errf("Sourced file isn't readable: %s", cmd_info->argv[0]);
		ret = CMDS_ERR_CUSTOM;
	}
	else if(cfg_source_file(path) != 0)
	{
		ui_sb_errf("Error sourcing file: %s", cmd_info->argv[0]);
		ret = CMDS_ERR_CUSTOM;
	}

	free(path);
	return ret;
}

static int
split_cmd(const cmd_info_t *cmd_info)
{
	return do_split(cmd_info, HSPLIT);
}

/* Stops the process by send itself SIGSTOP. */
static int
stop_cmd(const cmd_info_t *cmd_info)
{
	instance_stop();
	return 0;
}

/* :s[ubstitute]/[pat]/[subs]/[flags].  Replaces matches of regular expression
 * in names of files.  Empty pattern is replaced with the latest search pattern.
 * New pattern is saved in search pattern history.  Empty substitution part is
 * replaced with the previously used one. */
static int
substitute_cmd(const cmd_info_t *cmd_info)
{
	/* TODO: Vim preserves these two values across sessions. */
	static char *last_pattern;
	static char *last_sub;

	int ic = 0;
	int glob = cfg.gdefault;

	if(cmd_info->argc == 3)
	{
		/* TODO: maybe extract into a function to generalize code with
		 * parse_case_flag(). */
		const char *flags = cmd_info->argv[2];
		while(*flags != '\0')
		{
			switch(*flags)
			{
				case 'i': ic =  1; break;
				case 'I': ic = -1; break;

				case 'g': glob = !glob; break;

				default:
					return CMDS_ERR_TRAILING_CHARS;
			};

			++flags;
		}
	}

	if(cmd_info->argc >= 1)
	{
		if(cmd_info->argv[0][0] == '\0')
		{
			(void)replace_string(&last_pattern, hists_search_last());
		}
		else
		{
			(void)replace_string(&last_pattern, cmd_info->argv[0]);
			hists_search_save(last_pattern);
		}
	}

	if(cmd_info->argc >= 2)
	{
		(void)replace_string(&last_sub, cmd_info->argv[1]);
	}
	else if(cmd_info->argc == 1)
	{
		(void)replace_string(&last_sub, "");
	}

	if(is_null_or_empty(last_pattern))
	{
		ui_sb_err("No previous pattern");
		return CMDS_ERR_CUSTOM;
	}

	flist_set_marking(curr_view, 0);
	return fops_subst(curr_view, last_pattern, last_sub, ic, glob) != 0;
}

/* Synchronizes path/cursor position of the other pane with corresponding
 * properties of the current one, possibly including some relative path
 * changes. */
static int
sync_cmd(const cmd_info_t *cmd_info)
{
	if(cmd_info->emark && cmd_info->argc != 0)
	{
		return sync_selectively(cmd_info);
	}

	if(cmd_info->argc > 1)
	{
		return CMDS_ERR_TRAILING_CHARS;
	}

	if(cmd_info->argc > 0)
	{
		char dst_path[PATH_MAX + 1];
		to_canonic_path(cmd_info->argv[0], flist_get_dir(curr_view), dst_path,
				sizeof(dst_path));
		sync_location(dst_path, 0, cmd_info->emark, 0, 0);
	}
	else
	{
		sync_location(flist_get_dir(curr_view), 0, cmd_info->emark, 0, 0);
	}

	return 0;
}

/* Mirrors requested properties of current view with the other one.  Returns
 * value to be returned by command handler. */
static int
sync_selectively(const cmd_info_t *cmd_info)
{
	int location = 0, cursor_pos = 0, local_options = 0, filters = 0,
			filelist = 0, tree = 0;
	if(parse_sync_properties(cmd_info, &location, &cursor_pos, &local_options,
				&filters, &filelist, &tree) != 0)
	{
		return CMDS_ERR_CUSTOM;
	}

	if(!cv_tree(curr_view->custom.type))
	{
		tree = 0;
	}

	if(local_options)
	{
		sync_local_opts(location);
	}
	if(location)
	{
		filelist = filelist && flist_custom_active(curr_view);
		sync_location(flist_get_dir(curr_view), filelist, cursor_pos, filters,
				tree);
	}
	if(filters)
	{
		sync_filters();
	}

	return 0;
}

/* Parses selective view synchronization properties.  Default values for
 * arguments should be set before the call.  Returns zero on success, otherwise
 * non-zero is returned and error message is displayed on the status bar. */
static int
parse_sync_properties(const cmd_info_t *cmd_info, int *location,
		int *cursor_pos, int *local_options, int *filters, int *filelist, int *tree)
{
	int i;
	for(i = 0; i < cmd_info->argc; ++i)
	{
		const char *const property = cmd_info->argv[i];
		if(strcmp(property, "location") == 0)
		{
			*location = 1;
		}
		else if(strcmp(property, "cursorpos") == 0)
		{
			*cursor_pos = 1;
		}
		else if(strcmp(property, "localopts") == 0)
		{
			*local_options = 1;
		}
		else if(strcmp(property, "filters") == 0)
		{
			*filters = 1;
		}
		else if(strcmp(property, "filelist") == 0)
		{
			*location = 1;
			*filelist = 1;
		}
		else if(strcmp(property, "tree") == 0)
		{
			*location = 1;
			*tree = 1;
		}
		else if(strcmp(property, "all") == 0)
		{
			*location = 1;
			*cursor_pos = 1;
			*local_options = 1;
			*filters = 1;
			*filelist = 1;
			*tree = 1;
		}
		else
		{
			ui_sb_errf("Unknown selective sync property: %s", property);
			return 1;
		}
	}

	return 0;
}

/* Mirrors location (directory and maybe cursor position plus local filter) of
 * the current view to the other one. */
static void
sync_location(const char path[], int cv, int sync_cursor_pos, int sync_filters,
		int tree)
{
	if(!cd_is_possible(path) || change_directory(other_view, path) < 0)
	{
		return;
	}

	if(tree)
	{
		/* Normally changing location resets local filter.  Prevent this by
		 * synchronizing it here (after directory changing, but before loading list
		 * of files, hence no extra work). */
		if(sync_filters)
		{
			local_filter_apply(other_view, curr_view->local_filter.filter.raw);
		}

		(void)flist_clone_tree(other_view, curr_view);
	}
	else if(cv)
	{
		flist_custom_clone(other_view, curr_view, 0);
		if(sync_filters)
		{
			local_filter_apply(other_view, curr_view->local_filter.filter.raw);
			replace_dir_entries(other_view, &other_view->custom.entries,
					&other_view->custom.entry_count, other_view->dir_entry,
					other_view->list_rows);
		}
	}
	else
	{
		/* Normally changing location resets local filter.  Prevent this by
		 * synchronizing it here (after directory changing, but before loading list
		 * of files, hence no extra work). */
		if(sync_filters)
		{
			local_filter_apply(other_view, curr_view->local_filter.filter.raw);
		}

		(void)populate_dir_list(other_view, 0);
	}

	if(sync_cursor_pos)
	{
		if(flist_custom_active(curr_view) && !flist_custom_active(other_view))
		{
			flist_hist_lookup(other_view, curr_view);
		}
		else
		{
			char curr_file_path[PATH_MAX + 1];

			const int offset = (curr_view->list_pos - curr_view->top_line);
			const int shift = (offset*other_view->window_rows)/curr_view->window_rows;

			get_current_full_path(curr_view, sizeof(curr_file_path), curr_file_path);
			break_atr(curr_file_path, '/');
			navigate_to_file(other_view, curr_file_path,
					get_current_file_name(curr_view), 1);

			other_view->top_line = MAX(0, curr_view->list_pos - shift);
			(void)fview_enforce_scroll_offset(other_view);
		}

		flist_hist_save(other_view);
	}

	ui_view_schedule_redraw(other_view);
}

/* Sets local options of the other view to be equal to options of the current
 * one. */
static void
sync_local_opts(int defer_slow)
{
	clone_local_options(curr_view, other_view, defer_slow);
	ui_view_schedule_redraw(other_view);
}

/* Sets filters of the other view to be equal to options of the current one. */
static void
sync_filters(void)
{
	other_view->prev_invert = curr_view->prev_invert;
	other_view->invert = curr_view->invert;

	(void)filter_assign(&other_view->local_filter.filter,
			&curr_view->local_filter.filter);
	matcher_free(other_view->manual_filter);
	other_view->manual_filter = matcher_clone(curr_view->manual_filter);
	(void)filter_assign(&other_view->auto_filter, &curr_view->auto_filter);
	ui_view_schedule_reload(other_view);
}

/* Closes current tab unless it's the last one. */
static int
tabclose_cmd(const cmd_info_t *cmd_info)
{
	tabs_close();
	return 0;
}

/* Moves current tab to a different position. */
static int
tabmove_cmd(const cmd_info_t *cmd_info)
{
	int where_to;

	if(cmd_info->argc == 0 || strcmp(cmd_info->argv[0], "$") == 0)
	{
		where_to = tabs_count(curr_view);
	}
	else if(!read_int(cmd_info->argv[0], &where_to))
	{
		return CMDS_ERR_INVALID_ARG;
	}

	tabs_move(curr_view, where_to);
	ui_views_update_titles();
	return 0;
}

/* Sets, changes or resets name of current tab. */
static int
tabname_cmd(const cmd_info_t *cmd_info)
{
	tabs_rename(curr_view, cmd_info->argc == 0 ? NULL : cmd_info->argv[0]);
	ui_views_update_titles();
	return 0;
}

/* Creates a new tab.  Takes optional path for the new tab. */
static int
tabnew_cmd(const cmd_info_t *cmd_info)
{
	if(cfg.pane_tabs && curr_view->custom.type == CV_DIFF)
	{
		ui_sb_err("Switching tab of single pane would drop comparison");
		return CMDS_ERR_CUSTOM;
	}

	const char *path = NULL;
	char canonic_dir[PATH_MAX + 1];

	if(cmd_info->argc > 0)
	{
		char dir[PATH_MAX + 1];

		int updir;
		flist_pick_cd_path(curr_view, flist_get_dir(curr_view), cmd_info->argv[0],
				&updir, dir, sizeof(dir));
		if(updir)
		{
			copy_str(dir, sizeof(dir), "..");
		}

		to_canonic_path(dir, flist_get_dir(curr_view), canonic_dir,
				sizeof(canonic_dir));

		if(!cd_is_possible(canonic_dir))
		{
			return 0;
		}

		path = canonic_dir;
	}

	if(tabs_new(NULL, path) != 0)
	{
		ui_sb_err("Failed to open a new tab");
		return CMDS_ERR_CUSTOM;
	}
	return 0;
}

/* Switches either to the next tab or to tab specified by its number in the only
 * optional parameter. */
static int
tabnext_cmd(const cmd_info_t *cmd_info)
{
	if(cmd_info->argc == 0)
	{
		tabs_next(1);
		return 0;
	}

	int n;
	if(!read_int(cmd_info->argv[0], &n) || n <= 0 || n > tabs_count(curr_view))
	{
		return CMDS_ERR_INVALID_ARG;
	}

	tabs_goto(n - 1);
	return 0;
}

/* Closes all tabs but the current one. */
static int
tabonly_cmd(const cmd_info_t *cmd_info)
{
	tabs_only(curr_view);
	stats_redraw_later();
	return 0;
}

/* Switches either to the previous tab or to n-th previous tab, where n is
 * specified by the only optional parameter. */
static int
tabprevious_cmd(const cmd_info_t *cmd_info)
{
	if(cmd_info->argc == 0)
	{
		tabs_previous(1);
		return 0;
	}

	int n;
	if(!read_int(cmd_info->argv[0], &n) || n <= 0)
	{
		return CMDS_ERR_INVALID_ARG;
	}

	tabs_previous(n);
	return 0;
}

/* Creates files. */
static int
touch_cmd(const cmd_info_t *cmd_info)
{
	const int at = get_at(curr_view, cmd_info);
	return fops_mkfiles(curr_view, at, cmd_info->argv, cmd_info->argc) != 0;
}

/* Gets destination position based on range.  Returns the position. */
static int
get_at(const view_t *view, const cmd_info_t *cmd_info)
{
	return (cmd_info->end == NOT_DEF) ? view->list_pos : cmd_info->end;
}

/* Replaces letters in names of files according to character mapping. */
static int
tr_cmd(const cmd_info_t *cmd_info)
{
	char buf[strlen(cmd_info->argv[0]) + 1];
	size_t pl, sl;

	if(cmd_info->argv[0][0] == '\0' || cmd_info->argv[1][0] == '\0')
	{
		ui_sb_err("Empty argument");
		return CMDS_ERR_CUSTOM;
	}

	pl = strlen(cmd_info->argv[0]);
	sl = strlen(cmd_info->argv[1]);
	strcpy(buf, cmd_info->argv[1]);
	if(pl < sl)
	{
		ui_sb_err("Second argument cannot be longer");
		return CMDS_ERR_CUSTOM;
	}
	else if(pl > sl)
	{
		while(sl < pl)
		{
			buf[sl] = buf[sl - 1];
			++sl;
		}
		buf[sl] = '\0';
	}

	flist_set_marking(curr_view, 0);
	return fops_tr(curr_view, cmd_info->argv[0], buf) != 0;
}

/* Lists all valid non-empty trash directories in a menu with optional size of
 * each one. */
static int
trashes_cmd(const cmd_info_t *cmd_info)
{
	return show_trashes_menu(curr_view, cmd_info->qmark) != 0;
}

/* Convert view into a tree with root at current location. */
static int
tree_cmd(const cmd_info_t *cmd_info)
{
	int in_tree = flist_custom_active(curr_view)
	           && cv_tree(curr_view->custom.type);

	if(cmd_info->emark && in_tree)
	{
		rn_leave(curr_view, 1);
		return 0;
	}

	int depth;
	if(parse_tree_properties(cmd_info, &depth) != 0)
	{
			return CMDS_ERR_CUSTOM;
	}

	(void)flist_load_tree(curr_view, flist_get_dir(curr_view), depth);
	return 0;
}

/* Parses properties of atree.  Default values are set by the function.  Returns
 * zero on success, otherwise non-zero is returned and error message is
 * displayed on the status bar. */
static int
parse_tree_properties(const cmd_info_t *cmd_info, int *depth)
{
	*depth = INT_MAX;

	int i;
	for(i = 0; i < cmd_info->argc; ++i)
	{
		const char *arg = cmd_info->argv[i];
		if(skip_prefix(&arg, "depth="))
		{
			char *endptr;
			const long value = strtol(arg, &endptr, 10);
			if(*endptr != '\0' || value < 1)
			{
				ui_sb_errf("Invalid depth: %s", arg);
				return CMDS_ERR_CUSTOM;
			}

			*depth = value - 1;
		}
		else
		{
			ui_sb_errf("Invalid argument: %s", arg);
			return CMDS_ERR_CUSTOM;
		}
	}

	return 0;
}

static int
undolist_cmd(const cmd_info_t *cmd_info)
{
	return show_undolist_menu(curr_view, cmd_info->emark) != 0;
}

static int
unlet_cmd(const cmd_info_t *cmd_info)
{
	vle_tb_clear(vle_err);
	if(unlet_variables(cmd_info->args) != 0 && !cmd_info->emark)
	{
		ui_sb_err(vle_tb_get_data(vle_err));
		return CMDS_ERR_CUSTOM;
	}
	return 0;
}

/* Unmaps keys in normal and visual or in command-line mode. */
static int
unmap_cmd(const cmd_info_t *cmd_info)
{
	wchar_t *subst = substitute_specs(cmd_info->argv[0]);
	if(subst == NULL)
	{
		show_error_msgf("Unmapping Error", "Failed to convert to wide string: %s",
				cmd_info->argv[0]);
		return 0;
	}

	int result;
	if(cmd_info->emark)
	{
		result = (vle_keys_user_remove(subst, CMDLINE_MODE) != 0);
	}
	else if(!vle_keys_user_exists(subst, NORMAL_MODE))
	{
		ui_sb_err("No such mapping in normal mode");
		result = -1;
	}
	else if(!vle_keys_user_exists(subst, VISUAL_MODE))
	{
		ui_sb_err("No such mapping in visual mode");
		result = -2;
	}
	else
	{
		result = (vle_keys_user_remove(subst, NORMAL_MODE) != 0);
		result += (vle_keys_user_remove(subst, VISUAL_MODE) != 0);
	}
	free(subst);

	if(result > 0)
	{
		ui_sb_err("Error while unmapping keys");
	}
	return (result != 0 ? CMDS_ERR_CUSTOM : 0);
}

/* Unselects files that match passed in expression or range. */
static int
unselect_cmd(const cmd_info_t *cmd_info)
{
	int error;
	cmds_preserve_selection();

	/* If no arguments are passed, unselect the range. */
	if(cmd_info->argc == 0)
	{
		flist_sel_by_range(curr_view, cmd_info->begin, cmd_info->end, 0);
		return 0;
	}

	if(cmd_info->begin != NOT_DEF)
	{
		ui_sb_err("Either range or argument should be supplied.");
		error = 1;
	}
	else if(cmd_info->args[0] == '!' && !char_is_one_of("/{<", cmd_info->args[1]))
	{
		error = flist_sel_by_filter(curr_view, cmd_info->args + 1, cmd_info->emark,
				0);
	}
	else
	{
		error = flist_sel_by_pattern(curr_view, cmd_info->args, 0, 0);
	}

	return (error ? CMDS_ERR_CUSTOM : 0);
}

static int
view_cmd(const cmd_info_t *cmd_info)
{
	cmds_preserve_selection();

	if((!curr_stats.preview.on || cmd_info->emark) && !qv_can_show())
	{
		return CMDS_ERR_CUSTOM;
	}
	if(curr_stats.preview.on && cmd_info->emark)
	{
		return 0;
	}
	qv_toggle();
	return 0;
}

static int
vifm_cmd(const cmd_info_t *cmd_info)
{
	return show_vifm_menu(curr_view) != 0;
}

static int
vmap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "Visual", VISUAL_MODE, 0) != 0;
}

static int
vnoremap_cmd(const cmd_info_t *cmd_info)
{
	return do_map(cmd_info, "Visual", VISUAL_MODE, 1) != 0;
}

/* Maps keys in the specified mode. */
static int
do_map(const cmd_info_t *cmd_info, const char map_type[], int mode,
		int no_remap)
{
	wchar_t *keys, *mapping;
	char *raw_rhs, *rhs;
	char t;

	if(cmd_info->argc <= 1)
	{
		keys = substitute_specs(cmd_info->args);
		if(keys != NULL)
		{
			int save_msg = show_map_menu(curr_view, map_type, mode, keys);
			free(keys);
			return save_msg != 0;
		}
		show_error_msgf("Mapping Error", "Failed to convert to wide string: %s",
				cmd_info->args);
		return 0;
	}

	const char *args = cmd_info->args;
	const int flags = (no_remap ? KEYS_FLAG_NOREMAP : KEYS_FLAG_NONE)
	                | parse_map_args(&args);

	raw_rhs = vle_cmds_past_arg(args);
	t = *raw_rhs;
	*raw_rhs = '\0';

	int error = 0;
	rhs = vle_cmds_at_arg(raw_rhs + 1);
	keys = substitute_specs(args);
	mapping = substitute_specs(rhs);
	if(keys != NULL && mapping != NULL)
	{
		error = vle_keys_user_add(keys, mapping, mode, flags);
	}
	else
	{
		show_error_msgf("Mapping Error", "Failed to convert to wide string: %s",
				cmd_info->args);
	}
	free(mapping);
	free(keys);

	*raw_rhs = t;

	if(error)
		show_error_msg("Mapping Error", "Unable to allocate enough memory");

	return 0;
}

/* Parses <*> :*map arguments removing them from the line.  Returns flags
 * collected. */
static int
parse_map_args(const char **args)
{
	int flags = 0;
	do
	{
		if(skip_prefix(args, "<silent>"))
		{
			flags |= KEYS_FLAG_SILENT;
		}
		else if(skip_prefix(args, "<wait>"))
		{
			flags |= KEYS_FLAG_WAIT;
		}
		else
		{
			break;
		}
		*args = skip_whitespace(*args);
	}
	while(1);
	return flags;
}

#ifdef _WIN32
static int
volumes_cmd(const cmd_info_t *cmd_info)
{
	return show_volumes_menu(curr_view) != 0;
}
#endif

static int
vsplit_cmd(const cmd_info_t *cmd_info)
{
	return do_split(cmd_info, VSPLIT);
}

static int
do_split(const cmd_info_t *cmd_info, SPLIT orientation)
{
	if(cmd_info->emark && cmd_info->argc != 0)
	{
		ui_sb_err("No arguments are allowed if you use \"!\"");
		return CMDS_ERR_CUSTOM;
	}

	if(cmd_info->emark)
	{
		if(curr_stats.number_of_windows == 1)
			split_view(orientation);
		else
			only();
	}
	else
	{
		if(cmd_info->argc == 1)
			cd(other_view, flist_get_dir(curr_view), cmd_info->argv[0]);
		split_view(orientation);
	}
	return 0;
}

static int
vunmap_cmd(const cmd_info_t *cmd_info)
{
	return do_unmap(cmd_info->argv[0], VISUAL_MODE);
}

/* Unmaps keys for the specified mode.  Returns zero on success, otherwise
 * non-zero is returned and message is printed on the status bar. */
static int
do_unmap(const char keys[], int mode)
{
	wchar_t *subst = substitute_specs(keys);
	if(subst == NULL)
	{
		show_error_msgf("Unmapping Error", "Failed to convert to wide string: %s",
				keys);
		return 0;
	}

	int result = vle_keys_user_remove(subst, mode);
	free(subst);

	if(result != 0)
	{
		ui_sb_err("No such mapping");
		return CMDS_ERR_CUSTOM;
	}
	return 0;
}

/* Executes Ctrl-W [count] {arg} normal mode command. */
static int
wincmd_cmd(const cmd_info_t *cmd_info)
{
	int count;
	char *cmd;
	wchar_t *wcmd;

	if(cmd_info->count != NOT_DEF && cmd_info->count < 0)
	{
		return CMDS_ERR_INVALID_RANGE;
	}
	if(cmd_info->args[0] == '\0' || cmd_info->args[1] != '\0')
	{
		return CMDS_ERR_INVALID_ARG;
	}

	count = (cmd_info->count <= 1) ? 1 : cmd_info->count;
	cmd = format_str("%c%d%s", NC_C_w, count, cmd_info->args);

	wcmd = to_wide(cmd);
	if(wcmd == NULL)
	{
		show_error_msgf("Command Error", "Failed to convert to wide string: %s",
				cmd);
		free(cmd);
		return 0;
	}
	free(cmd);

	(void)vle_keys_exec_timed_out_no_remap(wcmd);
	free(wcmd);
	return 0;
}

/* Prefix that execute same command-line command for both panes. */
static int
windo_cmd(const cmd_info_t *cmd_info)
{
	int result = 0;

	if(cmd_info->argc == 0)
		return 0;

	result += winrun(&lwin, cmd_info->args) != 0;
	result += winrun(&rwin, cmd_info->args) != 0;

	update_screen(UT_FULL);

	return result;
}

static int
winrun_cmd(const cmd_info_t *cmd_info)
{
	int result = 0;
	const char *cmd;

	if(cmd_info->argc == 0)
		return 0;

	if(cmd_info->argv[0][1] != '\0' ||
			!char_is_one_of("^$%.,", cmd_info->argv[0][0]))
		return CMDS_ERR_INVALID_ARG;

	if(cmd_info->argc == 1)
		return 0;

	cmd = cmd_info->args + 2;
	switch(cmd_info->argv[0][0])
	{
		case '^':
			result += winrun(&lwin, cmd) != 0;
			break;
		case '$':
			result += winrun(&rwin, cmd) != 0;
			break;
		case '%':
			result += winrun(&lwin, cmd) != 0;
			result += winrun(&rwin, cmd) != 0;
			break;
		case '.':
			result += winrun(curr_view, cmd) != 0;
			break;
		case ',':
			result += winrun(other_view, cmd) != 0;
			break;
	}

	stats_refresh_later();

	return result;
}

/* Executes cmd command-line command for a specific view. */
static int
winrun(view_t *view, const char cmd[])
{
	const int prev_global_local_settings = curr_stats.global_local_settings;
	int result;
	view_t *tmp_curr, *tmp_other;

	ui_view_pick(view, &tmp_curr, &tmp_other);

	/* :winrun and :windo should be able to set settings separately for each
	 * window. */
	curr_stats.global_local_settings = 0;
	result = cmds_dispatch(cmd, curr_view, CIT_COMMAND);
	curr_stats.global_local_settings = prev_global_local_settings;

	ui_view_unpick(view, tmp_curr, tmp_other);

	return result;
}

static int
write_cmd(const cmd_info_t *cmd_info)
{
	state_store();
	return 0;
}

/* Possibly exits vifm normally with or without saving state to vifminfo
 * file. */
static int
qall_cmd(const cmd_info_t *cmd_info)
{
	vifm_try_leave(!cmd_info->emark, 0, cmd_info->emark);
	return 0;
}

/* Possibly exits vifm normally with or without saving state to vifminfo file or
 * closes a tab. */
static int
quit_cmd(const cmd_info_t *cmd_info)
{
	ui_quit(!cmd_info->emark, cmd_info->emark);
	return 0;
}

/* Possibly exits the application saving vifminfo file or closes a tab. */
static int
wq_cmd(const cmd_info_t *cmd_info)
{
	ui_quit(1, cmd_info->emark);
	return 0;
}

/* Possibly exits the application saving vifminfo file. */
static int
wqall_cmd(const cmd_info_t *cmd_info)
{
	vifm_try_leave(1, 0, cmd_info->emark);
	return 0;
}

/* Processes :[range]yank command followed by "{reg} [{count}]" or
 * "{reg}|{count}". */
static int
yank_cmd(const cmd_info_t *cmd_info)
{
	int reg = DEFAULT_REG_NAME;
	int result;

	result = get_reg_and_count(cmd_info, &reg);
	if(result == 0)
	{
		flist_set_marking(curr_view, 0);
		result = fops_yank(curr_view, reg) != 0;
	}

	return result;
}

/* Processes arguments of form "{reg} [{count}]" or "{reg}|{count}".  On success
 * *reg is set (so it should be initialized before the call) and zero is
 * returned, otherwise cmds unit error code is returned. */
static int
get_reg_and_count(const cmd_info_t *cmd_info, int *reg)
{
	if(cmd_info->argc == 2)
	{
		int count;
		int error;

		error = get_reg(cmd_info->argv[0], reg);
		if(error != 0)
		{
			return error;
		}

		if(!isdigit(cmd_info->argv[1][0]))
			return CMDS_ERR_TRAILING_CHARS;

		count = atoi(cmd_info->argv[1]);
		if(count == 0)
		{
			ui_sb_err("Count argument can't be zero");
			return CMDS_ERR_CUSTOM;
		}
		flist_sel_count(curr_view, cmd_info->end, count);
	}
	else if(cmd_info->argc == 1)
	{
		if(isdigit(cmd_info->argv[0][0]))
		{
			int count = atoi(cmd_info->argv[0]);
			if(count == 0)
			{
				ui_sb_err("Count argument can't be zero");
				return CMDS_ERR_CUSTOM;
			}
			flist_sel_count(curr_view, cmd_info->end, count);
		}
		else
		{
			return get_reg(cmd_info->argv[0], reg);
		}
	}
	return 0;
}

/* Processes argument as register name.  On success *reg is set (so it should be
 * initialized before the call) and zero is returned, otherwise cmds unit error
 * code is returned. */
static int
get_reg(const char arg[], int *reg)
{
	if(arg[1] != '\0')
	{
		return CMDS_ERR_TRAILING_CHARS;
	}
	if(!regs_exists(arg[0]))
	{
		return CMDS_ERR_TRAILING_CHARS;
	}

	*reg = arg[0];
	return 0;
}

/* Special handler for user defined commands, which are defined using
 * :command. */
static int
usercmd_cmd(const cmd_info_t *cmd_info)
{
	char *expanded_com;
	MacroFlags flags;
	int external = 1;
	int save_msg = 0;

	MacroExpandReason mer = MER_OP;
	if(vle_cmds_identify(cmd_info->user_action) == COM_EXECUTE)
	{
		mer = MER_SHELL_OP;
	}

	/* Expand macros in a bound command. */
	expanded_com = ma_expand(cmd_info->user_action, cmd_info->args, &flags, mer);

	if(expanded_com[0] == ':')
	{
		cmds_scope_start();

		int sm = cmds_dispatch(expanded_com, curr_view, CIT_COMMAND);
		free(expanded_com);

		if(cmds_scope_finish() != 0)
		{
			ui_sb_err("Unmatched if-else-endif");
			return CMDS_ERR_CUSTOM;
		}

		return sm != 0;
	}

	int bg = parse_bg_mark(expanded_com);
	int pause = 0;
	const char *ext_cmd = expanded_com;
	if(*ext_cmd == '!')
	{
		++ext_cmd;
		if(*ext_cmd == '!')
		{
			pause = 1;
			++ext_cmd;
		}
		ext_cmd = skip_whitespace(ext_cmd);
	}

	flist_sel_stash(curr_view);

	char *title = format_str(":%s%s%s", cmd_info->user_cmd,
			(cmd_info->raw_args[0] == '\0' ? "" : " "), cmd_info->raw_args);
	int handled = rn_ext(curr_view, ext_cmd, title, flags, pause, bg,
			&save_msg);
	free(title);

	const int use_term_multiplexer = ma_flags_missing(flags, MF_NO_TERM_MUX);

	if(handled > 0)
	{
		/* Do nothing. */
	}
	else if(handled < 0)
	{
		/* XXX: is it intentional to skip adding such commands to undo list? */
		free(expanded_com);
		return save_msg;
	}
	else if(starts_with_lit(expanded_com, "filter") &&
			char_is_one_of(" !/", expanded_com[6]))
	{
		save_msg = cmds_dispatch1(expanded_com, curr_view, CIT_COMMAND);
		external = 0;
	}
	else if(expanded_com[0] == '!')
	{
		if(*ext_cmd != '\0')
		{
			if(bg)
			{
				bg_run_external(ext_cmd, 0, SHELL_BY_USER, NULL);
			}
			else
			{
				rn_shell(ext_cmd, pause ? PAUSE_ALWAYS : PAUSE_ON_ERROR,
						use_term_multiplexer, SHELL_BY_USER);
			}
		}
	}
	else if(expanded_com[0] == '/')
	{
		modnorm_set_search_attrs(/*count=*/1, /*last_search_backward=*/0);
		cmds_dispatch1(expanded_com + 1, curr_view, CIT_FSEARCH_PATTERN);
		cmds_preserve_selection();
		external = 0;
	}
	else if(expanded_com[0] == '=')
	{
		cmds_dispatch1(expanded_com + 1, curr_view, CIT_FILTER_PATTERN);
		ui_view_schedule_reload(curr_view);
		cmds_preserve_selection();
		external = 0;
	}
	else if(bg)
	{
		rn_start_bg_command(curr_view, expanded_com, flags);
	}
	else if(ma_flags_present(flags, MF_PIPE_FILE_LIST) ||
			ma_flags_present(flags, MF_PIPE_FILE_LIST_Z))
	{
		(void)rn_pipe(expanded_com, curr_view, flags, PAUSE_ON_ERROR);
	}
	else
	{
		(void)rn_shell(expanded_com, PAUSE_ON_ERROR, use_term_multiplexer,
				SHELL_BY_USER);
	}

	if(external)
	{
		un_group_reopen_last();
		un_group_add_op(OP_USR, strdup(expanded_com), NULL, "", "");
		un_group_close();
	}

	free(expanded_com);

	return save_msg;
}

/* Checks for background mark and trims it from the command.  Returns non-zero
 * if mark is found, and zero otherwise. */
static int
parse_bg_mark(char cmd[])
{
	/* Mark is: space, ampersand, any number of trailing separators. */

	char *const amp = strrchr(cmd, '&');
	if(amp == NULL || amp - 1 < cmd || *vle_cmds_at_arg(amp + 1) != '\0')
	{
		return 0;
	}

	amp[-1] = '\0';
	return 1;
}

/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
/* vim: set cinoptions+=t0 filetype=c : */
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master