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 / ui / ui.c (2a5ebbb50cff3c5f97732e5ab76f0b51fc65546e) (66KiB) (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
/* 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
 */

/* Save wrefresh identifier behind the macro before it's poisoned in the
 * header. */
#define use_wrefresh wrefresh

#include "ui.h"

#include <curses.h> /* mvwin() werase() */

#ifndef _WIN32
#include <sys/ioctl.h>
#include <termios.h> /* struct winsize */
#endif
#include <unistd.h>

#include <assert.h> /* assert() */
#include <ctype.h> /* isdigit() */
#include <errno.h> /* errno */
#include <stddef.h> /* NULL size_t wchar_t */
#include <stdint.h> /* uint64_t */
#include <stdlib.h> /* abs() free() */
#include <stdio.h> /* snprintf() vsnprintf() */
#include <string.h> /* memset() strcat() strcmp() strcpy() strdup() strlen() */
#include <wchar.h> /* wint_t wcslen() */

#include "../cfg/config.h"
#include "../cfg/info.h"
#include "../compat/curses.h"
#include "../compat/fs_limits.h"
#include "../compat/pthread.h"
#include "../engine/mode.h"
#include "../int/term_title.h"
#include "../lua/vlua.h"
#include "../modes/dialogs/msg_dialog.h"
#include "../modes/modes.h"
#include "../modes/view.h"
#include "../modes/wk.h"
#include "../utils/darray.h"
#include "../utils/log.h"
#include "../utils/macros.h"
#include "../utils/matchers.h"
#include "../utils/path.h"
#include "../utils/str.h"
#include "../utils/string_array.h"
#include "../utils/utf8.h"
#include "../utils/utils.h"
#include "../compare.h"
#include "../event_loop.h"
#include "../filelist.h"
#include "../flist_sel.h"
#include "../macros.h"
#include "../opt_handlers.h"
#include "../sort.h"
#include "../status.h"
#include "../vifm.h"
#include "private/statusline.h"
#include "cancellation.h"
#include "color_manager.h"
#include "color_scheme.h"
#include "colored_line.h"
#include "colors.h"
#include "fileview.h"
#include "quickview.h"
#include "statusbar.h"
#include "statusline.h"
#include "tabs.h"

/* List of formatted tab labels with some extra information. */
typedef struct
{
	cline_t *labels; /* List of labels (might miss some tabs). */
	int count;       /* Number of labels. */
	int skipped;     /* Number of leading tabs skipped due to lack of space. */
	int current;     /* Index of the current tab (does not take skipped count into
	                    account). */
}
tab_line_info_t;

/* Information for formatting tab title. */
typedef struct
{
	char *name;               /* Tab name. */
	char *num;                /* Tab number. */
	char *escaped_view_title; /* Auto-formatted title. */
	char *escaped_path;       /* Current path (could be a file path). */
	char *cv_title;           /* Prefix of custom view. */
	int tree;                 /* Whether in tree mode. */
	int current;              /* Whether it's a current tab. */
}
tab_title_info_t;

/* Type of path transformation function for format_view_title(). */
typedef char * (*path_func)(const char[]);

/* Window configured to do not wait for input.  Never appears on the screen,
 * sometimes used for reading input. */
static WINDOW *no_delay_window;
/* Window configured to wait for input indefinitely.  Never appears on the
 * screen, sometimes used for reading input. */
static WINDOW *inf_delay_window;

/* Pieces of top line for the left/top view. */
static WINDOW *ltop_line1; /* On top of left border. */
static WINDOW *ltop_line2; /* Middle of right border in horizontal view. */
/* On top of middle border. */
static WINDOW *mtop_line;
/* Pieces of top line for the right/bottom view. */
static WINDOW *rtop_line1; /* On top of right border. */
static WINDOW *rtop_line2; /* Middle of right border in horizontal view. */

unsigned int ui_next_view_id = 3;

view_t lwin = { .id = 1 };
view_t rwin = { .id = 2 };

view_t *other_view;
view_t *curr_view;

WINDOW *status_bar;
WINDOW *stat_win;
WINDOW *job_bar;
WINDOW *ruler_win;
WINDOW *input_win;
WINDOW *menu_win;
WINDOW *sort_win;
WINDOW *change_win;
WINDOW *error_win;
WINDOW *tab_line;

static WINDOW *lborder;
static WINDOW *mborder;
static WINDOW *rborder;

static int init_pair_wrapper(int pair, int fg, int bg);
static int pair_content_wrapper(int pair, int *fg, int *bg);
static int pair_in_use(int pair);
static void pair_moved(int from, int to);
static void create_windows(void);
static void update_geometry(void);
static int update_start(UpdateType update_kind);
static void update_finish(void);
static void adjust_splitter(int screen_w, int screen_h);
static int get_working_area_height(void);
static void clear_border(WINDOW *border);
static int middle_border_is_visible(void);
static void update_views(int reload);
static void reload_lists(void);
static void reload_list(view_t *view);
static void update_view(view_t *view);
static void update_window_lazy(WINDOW *win);
static void update_term_size(void);
static void update_statusbar_layout(void);
static int are_statusbar_widgets_visible(void);
static int get_ruler_width(view_t *view);
static char * expand_ruler_macros(view_t *view, const char format[]);
static void switch_panes_content(void);
static void set_splitter(int pos);
static FileType ui_view_entry_target_type(const dir_entry_t *entry);
static void refresh_bottom_lines(void);
static char * path_identity(const char path[]);
static int view_shows_tabline(const view_t *view);
static int get_tabline_height(void);
static void print_tabline(WINDOW *win, view_t *view, col_attr_t base_col,
		path_func pf);
static tab_line_info_t format_tab_labels(view_t *view, int max_width,
		path_func pf);
static void compute_avg_width(int *avg_width, int *spare_width,
		int min_widths[], int max_width, view_t *view, path_func pf);
TSTATIC cline_t make_tab_title(const tab_title_info_t *title_info);
TSTATIC tab_title_info_t make_tab_title_info(const tab_info_t *tab_info,
		path_func pf, int tab_num, int current_tab);
TSTATIC void dispose_tab_title_info(tab_title_info_t *title_info);
static cline_t format_tab_part(const char fmt[],
		const tab_title_info_t *title_info);
static char * format_view_title(const view_t *view, path_func pf);
static void print_view_title(const view_t *view, int active_view, char title[]);
static col_attr_t fixup_titles_attributes(const view_t *view, int active_view);
static int is_in_miller_view(const view_t *view);
static int is_forced_list_mode(const view_t *view);

/* List of macros that are expanded in the ruler. */
static const char RULER_MACROS[] = "-xlLPS%[]";

void
ui_ruler_update(view_t *view, int lazy_redraw)
{
	char *expanded;

	if(!are_statusbar_widgets_visible())
	{
		/* Do nothing, especially don't update layout because it might be a custom
		 * layout at the moment. */
		return;
	}

	update_statusbar_layout();

	expanded = expand_ruler_macros(view, cfg.ruler_format);
	expanded = break_in_two(expanded, getmaxx(ruler_win), "%=");

	ui_ruler_set(expanded);
	if(!lazy_redraw)
	{
		ui_refresh_win(ruler_win);
	}

	free(expanded);
}

void
ui_ruler_set(const char val[])
{
	const int x = getmaxx(ruler_win) - strlen(val);

	werase(ruler_win);
	mvwaddstr(ruler_win, 0, MAX(x, 0), val);
	wnoutrefresh(ruler_win);
}

int
setup_ncurses_interface(void)
{
	int screen_x, screen_y;

	initscr();
	noecho();
	nonl();
	raw();

	ui_set_cursor(/*visibility=*/0);

	getmaxyx(stdscr, screen_y, screen_x);
	/* Screen is too small to be useful. */
	if(screen_y < MIN_TERM_HEIGHT || screen_x < MIN_TERM_WIDTH)
	{
		vifm_finish("Terminal is too small to run vifm.");
	}

	if(!has_colors())
	{
		vifm_finish("Vifm requires a console that can support color.");
	}

	start_color();
	use_default_colors();

	const colmgr_conf_t colmgr_conf = {
		.max_color_pairs = COLOR_PAIRS,
		.max_colors = COLORS,
		.init_pair = &init_pair_wrapper,
		.pair_content = &pair_content_wrapper,
		.pair_in_use = &pair_in_use,
		.pair_moved = &pair_moved,
	};
	colmgr_init(&colmgr_conf);

	cs_load_defaults();

	create_windows();

	cfg.tab_stop = TABSIZE;

#ifdef ENABLE_EXTENDED_KEYS
	keypad(status_bar, TRUE);
#endif /* ENABLE_EXTENDED_KEYS */

#if defined(NCURSES_EXT_FUNCS) && NCURSES_EXT_FUNCS >= 20081102
#ifdef HAVE_SET_ESCDELAY_FUNC
	/* Use ncurses specific function to make delay after pressing escape key
	 * unnoticeable.  Used to be zero, but in some corner cases multiple bytes
	 * composing a functional key code might be handled to the application with a
	 * delay. */
	set_escdelay(5);
#endif
#endif

#if defined(NCURSES_VERSION) && defined(ENABLE_EXTENDED_KEYS)
	/* Disable 'unsupported' extended keys so that esc-codes will be
	 * received instead of unnamed extended keycode values (> KEY_MAX).
	 * These keys can then be mapped with esc-codes in vifmrc.
	 * An example could be <c-down>:
	 *     qnoremap <esc>[1;5B j
	 * as without this change <c-down> can return a keycode value of 531
	 * (terminfo name kDN5), which is larger than KEY_MAX and has no
	 * pre-defined curses key name. */
	int i;
	for(i = KEY_MAX; i < 2000; ++i)
	{
		keyok(i, FALSE);
	}
#endif

#ifdef HAVE_EXTENDED_COLORS
	curr_stats.direct_color = (COLORS == 0x1000000);
#endif

	ui_resize_all();

	return 1;
}

/* Calls init_pair() from libcurses. */
static int
init_pair_wrapper(int pair, int fg, int bg)
{
#ifdef HAVE_EXTENDED_COLORS
	return init_extended_pair(pair, fg, bg);
#else
	return init_pair(pair, fg, bg);
#endif
}

/* Calls pair_content() from libcurses. */
static int
pair_content_wrapper(int pair, int *fg, int *bg)
{
#ifdef HAVE_EXTENDED_COLORS
	return extended_pair_content(pair, fg, bg);
#else
	short fg_short, bg_short;

	const int result = pair_content(pair, &fg_short, &bg_short);

	*fg = fg_short;
	*bg = bg_short;

	return result;
#endif
}

/* Checks whether pair is being used at the moment.  Returns non-zero if so and
 * zero otherwise. */
static int
pair_in_use(int pair)
{
	int i;

	for(i = 0; i < MAXNUM_COLOR; ++i)
	{
		if(cfg.cs.pair[i] == pair || lwin.cs.pair[i] == pair ||
				rwin.cs.pair[i] == pair)
		{
			return 1;
		}
	}

	return 0;
}

/* Replaces uses of an old pair number with the new one. */
static void
pair_moved(int from, int to)
{
	int i;
	for(i = 0; i < MAXNUM_COLOR; ++i)
	{
		if(cfg.cs.pair[i] == from)
		{
			cfg.cs.pair[i] = to;
		}
		if(lwin.cs.pair[i] == from)
		{
			lwin.cs.pair[i] = to;
		}
		if(rwin.cs.pair[i] == from)
		{
			rwin.cs.pair[i] = to;
		}
	}
}

void
ui_set_mouse_active(int active)
{
	if(vifm_testing())
	{
		return;
	}

	if(active)
	{
		mousemask(ALL_MOUSE_EVENTS, /*oldmask=*/NULL);
		mouseinterval(0);
	}
	else
	{
		mousemask(/*newmask=*/0, /*oldmask=*/NULL);
	}
}

/* Initializes all WINDOW variables by calling newwin() to create ncurses
 * windows and configures hardware cursor. */
static void
create_windows(void)
{
	no_delay_window = newwin(1, 1, 0, 0);
	wtimeout(no_delay_window, 0);

	inf_delay_window = newwin(1, 1, 0, 0);
	wtimeout(inf_delay_window, -1);

	/* These refreshes prevent curses from drawing these windows on first read
	 * from them.  It seems that this way they get updated early and this keeps
	 * them from being drawn again. */
	wnoutrefresh(no_delay_window);
	wnoutrefresh(inf_delay_window);

	menu_win = newwin(1, 1, 0, 0);
	sort_win = newwin(1, 1, 0, 0);
	change_win = newwin(1, 1, 0, 0);
	error_win = newwin(1, 1, 0, 0);

	lborder = newwin(1, 1, 0, 0);

	lwin.title = newwin(1, 1, 0, 0);
	lwin.win = newwin(1, 1, 0, 0);

	mborder = newwin(1, 1, 0, 0);

	ltop_line1 = newwin(1, 1, 0, 0);
	ltop_line2 = newwin(1, 1, 0, 0);

	mtop_line = newwin(1, 1, 0, 0);
	tab_line = newwin(1, 1, 0, 0);

	rtop_line1 = newwin(1, 1, 0, 0);
	rtop_line2 = newwin(1, 1, 0, 0);

	rwin.title = newwin(1, 1, 0, 0);
	rwin.win = newwin(1, 1, 0, 0);

	rborder = newwin(1, 1, 0, 0);

	stat_win = newwin(1, 1, 0, 0);
	job_bar = newwin(1, 1, 0, 0);
	status_bar = newwin(1, 1, 0, 0);
	ruler_win = newwin(1, 1, 0, 0);
	input_win = newwin(1, 1, 0, 0);

	leaveok(menu_win, FALSE);
	leaveok(sort_win, FALSE);
	leaveok(change_win, FALSE);
	leaveok(error_win, FALSE);
	leaveok(lwin.win, FALSE);
	leaveok(rwin.win, FALSE);
	leaveok(status_bar, FALSE);

	leaveok(lborder, TRUE);
	leaveok(lwin.title, TRUE);
	leaveok(mborder, TRUE);
	leaveok(ltop_line1, TRUE);
	leaveok(ltop_line2, TRUE);
	leaveok(mtop_line, TRUE);
	leaveok(tab_line, TRUE);
	leaveok(rtop_line1, TRUE);
	leaveok(rtop_line2, TRUE);
	leaveok(rwin.title, TRUE);
	leaveok(rborder, TRUE);
	leaveok(stat_win, TRUE);
	leaveok(job_bar, TRUE);
	leaveok(ruler_win, TRUE);
	leaveok(input_win, TRUE);
}

void
ui_update_term_state(void)
{
	int screen_x, screen_y;

	update_term_size();
	getmaxyx(stdscr, screen_y, screen_x);
	(void)stats_update_term_state(screen_x, screen_y);
}

int
ui_char_pressed(wint_t c)
{
	if(curr_stats.load_stage < 2)
	{
		return 0;
	}

	ui_cancellation_push_off();
	wint_t pressed;

	while(1)
	{
		pressed = L'\0';
		/* Query single character in non-blocking mode. */
		if(compat_wget_wch(no_delay_window, &pressed) == ERR)
		{
			break;
		}

		if(c != NC_C_c && pressed == NC_C_c)
		{
			ui_cancellation_request();
		}

		if(pressed == c)
		{
			break;
		}
	}

	ui_cancellation_pop();

	return (pressed == c);
}

void
ui_drain_input(void)
{
	wint_t c;
	while(compat_wget_wch(no_delay_window, &c) != ERR)
	{
		/* Discard input. */
	}
}

static void
correct_size(view_t *view)
{
	getmaxyx(view->win, view->window_rows, view->window_cols);
	fview_update_geometry(view);
}

/* Updates TUI elements sizes and coordinates for single window
 * configuration. */
static void
only_layout(view_t *view, int screen_x)
{
	const int pos_correction = cfg.side_borders_visible ? 1 : 0;
	const int size_correction = cfg.side_borders_visible ? -2 : 0;
	const int y = get_tabline_height();

	wresize(view->title, 1, screen_x + size_correction);
	mvwin(view->title, y, pos_correction);

	mvwin(ltop_line1, y, 0);
	mvwin(ltop_line2, y, 0);

	mvwin(rtop_line1, y, screen_x - 1);
	mvwin(rtop_line2, y, screen_x - 1);

	wresize(tab_line, 1, screen_x);
	mvwin(tab_line, 0, 0);

	wresize(view->win, get_working_area_height(), screen_x + size_correction);
	mvwin(view->win, y + 1, pos_correction);
}

/* Updates TUI elements sizes and coordinates for vertical configuration of
 * panes: left one and right one. */
static void
vertical_layout(int screen_x)
{
	const int pos_correction = cfg.side_borders_visible ? 1 : 0;
	const int size_correction = cfg.side_borders_visible ? -1 : 0;
	const int border_height = get_working_area_height();
	const int y = get_tabline_height();

	int splitter_pos;
	if(curr_stats.splitter_pos < 0)
		splitter_pos = screen_x/2 - 1 + screen_x%2;
	else
		splitter_pos = curr_stats.splitter_pos;

	int splitter_width = (cfg.flexible_splitter ? 2 - screen_x%2 : 1);
	if(splitter_pos < 4)
		splitter_pos = 4;
	if(splitter_pos > screen_x - 4 - splitter_width)
		splitter_pos = screen_x - 4 - splitter_width;
	if(curr_stats.splitter_pos >= 0)
		stats_set_splitter_pos(splitter_pos);

	wresize(lwin.title, 1, splitter_pos + size_correction);
	mvwin(lwin.title, y, pos_correction);

	wresize(lwin.win, border_height, splitter_pos + size_correction);
	mvwin(lwin.win, y + 1, pos_correction);

	wresize(mborder, border_height, splitter_width);
	mvwin(mborder, y + 1, splitter_pos);

	mvwin(ltop_line1, y, 0);
	mvwin(ltop_line2, y, 0);

	wresize(tab_line, 1, screen_x);
	mvwin(tab_line, 0, 0);

	wresize(mtop_line, 1, splitter_width);
	mvwin(mtop_line, y, splitter_pos);

	mvwin(rtop_line1, y, screen_x - 1);
	mvwin(rtop_line2, y, screen_x - 1);

	wresize(rwin.title, 1,
			screen_x - (splitter_pos + splitter_width) + size_correction);
	mvwin(rwin.title, y, splitter_pos + splitter_width);

	wresize(rwin.win, border_height,
			screen_x - (splitter_pos + splitter_width) + size_correction);
	mvwin(rwin.win, y + 1, splitter_pos + splitter_width);
}

/* Updates TUI elements sizes and coordinates for horizontal configuration of
 * panes: top one and bottom one. */
static void
horizontal_layout(int screen_x, int screen_y)
{
	const int pos_correction = cfg.side_borders_visible ? 1 : 0;
	const int size_correction = cfg.side_borders_visible ? -2 : 0;
	const int y = get_tabline_height();

	int splitter_pos;
	if(curr_stats.splitter_pos < 0)
		splitter_pos = screen_y/2 - 1;
	else
		splitter_pos = curr_stats.splitter_pos;
	int splitter_height = (middle_border_is_visible() ? 1 : 0);
	if(splitter_pos < 2)
		splitter_pos = 2;
	if(splitter_pos > get_working_area_height() - 1 - splitter_height)
		splitter_pos = get_working_area_height() - 1 - splitter_height;
	if(curr_stats.splitter_pos >= 0)
		stats_set_splitter_pos(splitter_pos);

	wresize(lwin.title, 1, screen_x + size_correction);
	mvwin(lwin.title, y, pos_correction);

	wresize(lwin.win, splitter_pos - (y + 1), screen_x + size_correction);
	mvwin(lwin.win, y + 1, pos_correction);

	wresize(mborder, splitter_height, screen_x);
	mvwin(mborder, splitter_pos, 0);

	wresize(rwin.title, 1, screen_x + size_correction);
	mvwin(rwin.title, splitter_pos + splitter_height, pos_correction);

	wresize(rwin.win,
			get_working_area_height() - splitter_pos - splitter_height + y,
			screen_x + size_correction);
	mvwin(rwin.win, splitter_pos + splitter_height + 1, pos_correction);

	mvwin(ltop_line1, y, 0);
	mvwin(ltop_line2, splitter_pos, 0);

	wresize(tab_line, 1, screen_x);
	mvwin(tab_line, 0, 0);

	mvwin(rtop_line1, y, screen_x - 1);
	mvwin(rtop_line2, splitter_pos, screen_x - 1);

	wresize(lborder, screen_y - 1, 1);
	mvwin(lborder, y, 0);

	wresize(rborder, screen_y - 1, 1);
	mvwin(rborder, y, screen_x - 1);

	/* Unused in this layout. */
	wresize(mtop_line, 1, 1);
	mvwin(mtop_line, 0, 0);
}

/* Calculates height available for main area that contains file lists.  Returns
 * the height. */
static int
get_working_area_height(void)
{
	return getmaxy(stdscr)          /* Total available height. */
	     - 1                        /* Top line. */
	     - ui_stat_height()         /* Status line. */
	     - ui_stat_job_bar_height() /* Job bar. */
	     - 1                        /* Status bar line. */
	     - get_tabline_height();    /* Tab line. */
}

/* Updates internal data structures to reflect actual terminal geometry. */
static void
update_geometry(void)
{
	int screen_x, screen_y;

	update_term_size();

	getmaxyx(stdscr, screen_y, screen_x);
	cfg.lines = screen_y;
	cfg.columns = screen_x;

	LOG_INFO_MSG("New geometry: %dx%d", screen_x, screen_y);

	if(curr_stats.initial_lines == INT_MIN)
	{
		curr_stats.initial_lines = screen_y;
		curr_stats.initial_columns = screen_x;
	}

	load_geometry();
}

void
ui_quit(int write_info, int force)
{
	if(tabs_quit_on_close())
	{
		vifm_try_leave(write_info, 0, force);
	}
	else
	{
		tabs_close();
	}
}

int
cv_unsorted(CVType type)
{
	return type == CV_VERY || cv_compare(type);
}

int
cv_compare(CVType type)
{
	return type == CV_COMPARE || type == CV_DIFF;
}

int
cv_tree(CVType type)
{
	return type == CV_TREE || type == CV_CUSTOM_TREE;
}

const char *
cv_describe(CVType type)
{
	switch(type)
	{
		case CV_REGULAR:
			return "custom";

		case CV_VERY:
			return "very-custom";

		case CV_CUSTOM_TREE:
		case CV_TREE:
			return "tree";

		case CV_DIFF:
		case CV_COMPARE:
			return "compare";
	}

	return "UNKNOWN";
}

void
update_screen(UpdateType update_kind)
{
	if(update_start(update_kind))
	{
		update_all_windows();
		update_finish();
	}
}

void
ui_redraw_as_background(void)
{
	if(update_start(UT_REDRAW))
	{
		update_finish();
	}
}

/* Most of the update logic.  Everything that's done before updating windows.
 * Returns non-zero if update was carried out until the end, otherwise zero is
 * returned. */
static int
update_start(UpdateType update_kind)
{
	if(curr_stats.load_stage < 2 || update_kind == UT_NONE)
	{
		return 0;
	}

	ui_resize_all();

	if(curr_stats.restart_in_progress)
	{
		return 0;
	}

	update_attributes();

	if(curr_stats.term_state != TS_NORMAL)
	{
		return 0;
	}

	qv_ui_updated();

	update_views(update_kind == UT_FULL);
	/* Redraw message dialog over updated panes.  It's not very nice to do it
	 * here, but for sure better then blocking pane updates by checking for
	 * message mode. */
	if(vle_mode_is(MSG_MODE))
	{
		redraw_msg_dialog(0);
	}

	ui_stat_update(curr_view, 0);

	if(curr_stats.save_msg == 0 && !ui_sb_multiline())
	{
		modes_statusbar_update();

		if(vle_mode_is(VIEW_MODE))
		{
			modview_ruler_update();
		}
		else
		{
			ui_ruler_update(curr_view, 1);
		}
	}

	if(curr_stats.save_msg == 0)
	{
		ui_sb_clear();
	}
	else
	{
		ui_sb_msg(NULL);
	}

	if(vle_mode_is(VIEW_MODE) ||
			(curr_stats.number_of_windows == 2 && other_view->explore_mode))
	{
		modview_redraw();
	}

	return 1;
}

/* Post-windows-update part of an update. */
static void
update_finish(void)
{
	if(!curr_view->explore_mode)
	{
		fview_cursor_redraw(curr_view);
	}

	update_input_buf();
	ui_stat_job_bar_redraw();
}

void
ui_resize_all(void)
{
	update_geometry();

	int screen_w, screen_h;
	getmaxyx(stdscr, screen_h, screen_w);
	LOG_INFO_MSG("screen_h = %d; screen_w = %d", screen_h, screen_w);

	if(stats_update_term_state(screen_w, screen_h) != TS_NORMAL)
	{
		return;
	}

	adjust_splitter(screen_w, screen_h);

	wresize(stdscr, screen_h, screen_w);
	wresize(menu_win, screen_h - 1, screen_w);

	int border_h = get_working_area_height();
	int border_y = 1 + get_tabline_height();

	wresize(lborder, border_h, 1);
	mvwin(lborder, border_y, 0);

	wresize(rborder, border_h, 1);
	mvwin(rborder, border_y, screen_w - 1);

	/* These need a resize at least after terminal size was zero or they grow and
	 * produce bad looking effect. */
	wresize(ltop_line1, 1, 1);
	wresize(ltop_line2, 1, 1);
	wresize(rtop_line1, 1, 1);
	wresize(rtop_line2, 1, 1);

	if(curr_stats.number_of_windows == 1)
	{
		only_layout(&lwin, screen_w);
		only_layout(&rwin, screen_w);
	}
	else
	{
		if(curr_stats.split == HSPLIT)
			horizontal_layout(screen_w, screen_h);
		else
			vertical_layout(screen_w);
	}

	correct_size(&lwin);
	correct_size(&rwin);

	wresize(stat_win, ui_stat_height(), screen_w);
	(void)ui_stat_reposition(1, 0);

	wresize(job_bar, 1, screen_w);

	update_statusbar_layout();

	ui_set_cursor(/*visibility=*/0);
}

/* Adjusts splitter position after screen resize. */
static void
adjust_splitter(int screen_w, int screen_h)
{
	static int prev_w = -1, prev_h = -1;

	if(curr_stats.splitter_pos < 0)
	{
		prev_w = -1;
		prev_h = -1;
		return;
	}

	if(prev_w != screen_w || prev_h != screen_h)
	{
		stats_set_splitter_ratio(curr_stats.splitter_ratio);
		prev_w = screen_w;
		prev_h = screen_h;
	}
}

/* Clears border, possibly by filling it with a pattern (depends on
 * configuration). */
static void
clear_border(WINDOW *border)
{
	int i;
	int height, width;

	werase(border);

	getmaxyx(border, height, width);
	if(height > width)
	{
		/* Vertical split */
		if(strcmp(cfg.vborder_filler, " ") == 0 || *cfg.vborder_filler == '\0')
		{
			return;
		}
		for(i = 0; i < height; ++i)
		{
			mvwaddstr(border, i, 0, cfg.vborder_filler);
		}
	}
	else
	{
		/* Horizontal split */
		if(strcmp(cfg.hborder_filler, " ") == 0 || *cfg.hborder_filler == '\0')
		{
			return;
		}
		for(i = 0; i < width; ++i)
		{
			mvwaddstr(border, 0, i, cfg.hborder_filler);
		}
	}

	wnoutrefresh(border);
}

/* Checks whether mborder should be displayed/updated.  Returns non-zero if so,
 * otherwise zero is returned. */
static int
middle_border_is_visible(void)
{
	if(curr_stats.number_of_windows != 2)
	{
		return 0;
	}

	if(curr_stats.split == VSPLIT)
	{
		/* Always visible for vertical split. */
		return 1;
	}

	/* Optionally visible for horizontal split. */
	return (strlen(cfg.hborder_filler) != 0);
}

/* Updates (redraws or reloads) views. */
static void
update_views(int reload)
{
	if(reload)
		reload_lists();
	else
		redraw_lists();
}

/* Reloads file lists for both views. */
static void
reload_lists(void)
{
	reload_list(curr_view);

	if(curr_stats.number_of_windows == 2)
	{
		ui_view_title_update(other_view);
		if(curr_stats.preview.on)
		{
			qv_draw(curr_view);
		}
		else if(!other_view->explore_mode)
		{
			reload_list(other_view);
		}
	}
}

/* Reloads view handling special case of loading it for the first time during
 * startup. */
static void
reload_list(view_t *view)
{
	if(curr_stats.load_stage < 3)
	{
		const int keep_position = cfg_ch_pos_on(CHPOS_STARTUP)
		                        ? 0
		                        : !is_dir_list_loaded(view);
		load_dir_list(view, keep_position);
		return;
	}

	load_saving_pos(view);
}

void
change_window(void)
{
	swap_view_roles();

	load_view_options(curr_view);

	if(window_shows_dirlist(other_view))
	{
		const col_scheme_t *cs = ui_view_get_cs(other_view);
		if(cs_is_color_set(&cs->color[OTHER_WIN_COLOR]))
		{
			draw_dir_list(other_view);
		}
		else
		{
			fview_draw_inactive_cursor(other_view);
		}
	}

	if(curr_stats.preview.on && !is_dir_list_loaded(curr_view))
	{
		/* This view hasn't been loaded since startup yet, do it now. */
		navigate_to(curr_view, curr_view->curr_dir);
	}
	else
	{
		/* Change working directory, so that %c macro and other cwd-sensitive things
		 * work as expected. */
		(void)vifm_chdir(flist_get_dir(curr_view));
	}

	if(window_shows_dirlist(&lwin) && window_shows_dirlist(&rwin))
	{
		fview_cursor_redraw(curr_view);
		ui_views_update_titles();
	}
	else
	{
		stats_redraw_later();
	}
}

void
swap_view_roles(void)
{
	view_t *const tmp = curr_view;
	curr_view = other_view;
	other_view = tmp;
}

void
update_all_windows(void)
{
	if(curr_stats.load_stage >= 2)
	{
		touch_all_windows();
		doupdate();
	}
}

void
touch_all_windows(void)
{
	if(curr_stats.load_stage < 2)
	{
		return;
	}

	if(!modes_is_menu_like())
	{
		update_window_lazy(tab_line);

		if(middle_border_is_visible())
		{
			update_window_lazy(mborder);
			update_window_lazy(mtop_line);
		}

		if(curr_stats.number_of_windows == 1)
		{
			/* In one window view. */
			update_view(curr_view);
		}
		else
		{
			/* Two pane View. */
			update_view(&lwin);
			update_view(&rwin);
		}

		if(cfg.side_borders_visible)
		{
			/* This needs to be updated before things like status bar and status line
			 * to account for cases when they hide the top line. */
			update_window_lazy(ltop_line1);
			update_window_lazy(ltop_line2);
			update_window_lazy(rtop_line1);
			update_window_lazy(rtop_line2);

			update_window_lazy(lborder);
			update_window_lazy(rborder);
		}

		if(cfg.display_statusline)
		{
			update_window_lazy(stat_win);
		}

		if(ui_stat_job_bar_height() != 0)
		{
			update_window_lazy(job_bar);
		}
	}

	update_window_lazy(ruler_win);
	update_window_lazy(input_win);
	update_window_lazy(status_bar);

	if(vle_mode_is(MSG_MODE))
	{
		/* Location of message dialog can change, so it's better to let it position
		 * itself or multiple copies appear on the screen. */
		redraw_msg_dialog(1);
	}
}

/* Updates all parts of file view. */
static void
update_view(view_t *view)
{
	update_window_lazy(view->title);

	/* If view displays graphics, we don't want to update it or the image will be
	 * lost. */
	if(!view->explore_mode && !view->displays_graphics &&
			!(curr_stats.preview.on && view == other_view))
	{
		update_window_lazy(view->win);
	}
}

/* Tell curses to internally mark window as changed. */
static void
update_window_lazy(WINDOW *win)
{
	touchwin(win);
	/* Tell curses that it shouldn't assume that screen isn't messed up in any
	 * way. */
	redrawwin(win);
	wnoutrefresh(win);
}

void
update_input_bar(const wchar_t *str)
{
	if(!curr_stats.use_input_bar)
		return;

	if(wcslen(str) > (size_t)getmaxx(input_win))
	{
		str += wcslen(str) - getmaxx(input_win);
	}

	werase(input_win);
	compat_waddwstr(input_win, str);
	ui_refresh_win(input_win);
}

void
clear_num_window(void)
{
	if(curr_stats.use_input_bar)
	{
		werase(input_win);
		ui_refresh_win(input_win);
	}
}

void
show_progress(const char msg[], int period)
{
	static char marks[] = { '|', '/', '-', '\\' };
	static int mark = 0;
	static int count = 1;
	static int total = 0;

	/* Do nothing if UI is not functional. */
	if(curr_stats.load_stage < 1)
	{
		return;
	}

	/* Reset state. */
	if(period == 0)
	{
		count = 1;
		total = 0;
		return;
	}

	/* Advance state. */
	++count;
	++total;

	/* Skip intermediate updates to do not hammer UI with refreshes. */
	if(abs(period) != 1 && count%abs(period) != 1)
	{
		return;
	}
	count = 1;

	/* Assume that period equal to or less than one means that message already
	 * contains counter (maybe along with total number) or doesn't need one. */
	if(period <= 1)
	{
		ui_sb_quick_msgf("%s %c", msg, marks[mark]);
	}
	else
	{
		ui_sb_quick_msgf("%s %c %d", msg, marks[mark], total);
	}

	/* Pick next mark character. */
	mark = (mark + 1) % sizeof(marks);
}

void
redraw_lists(void)
{
	if(curr_stats.load_stage == 0)
	{
		return;
	}

	redraw_current_view();
	if(curr_stats.number_of_windows == 2)
	{
		if(curr_stats.preview.on)
		{
			qv_draw(curr_view);
			refresh_view_win(other_view);
		}
		else if(!other_view->explore_mode)
		{
			fview_cursor_redraw(other_view);
			draw_dir_list(other_view);
			refresh_view_win(other_view);
		}
	}
}

void
update_attributes(void)
{
	if(curr_stats.load_stage < 2)
		return;

	if(cfg.side_borders_visible)
	{
		ui_set_bg(lborder, &cfg.cs.color[BORDER_COLOR], cfg.cs.pair[BORDER_COLOR]);
		clear_border(lborder);
		ui_set_bg(rborder, &cfg.cs.color[BORDER_COLOR], cfg.cs.pair[BORDER_COLOR]);
		clear_border(rborder);

		ui_set_bg(ltop_line1, &cfg.cs.color[TOP_LINE_COLOR],
				cfg.cs.pair[TOP_LINE_COLOR]);
		werase(ltop_line1);

		ui_set_bg(ltop_line2, &cfg.cs.color[TOP_LINE_COLOR],
				cfg.cs.pair[TOP_LINE_COLOR]);
		werase(ltop_line2);

		ui_set_bg(rtop_line1, &cfg.cs.color[TOP_LINE_COLOR],
				cfg.cs.pair[TOP_LINE_COLOR]);
		werase(rtop_line1);

		ui_set_bg(rtop_line2, &cfg.cs.color[TOP_LINE_COLOR],
				cfg.cs.pair[TOP_LINE_COLOR]);
		werase(rtop_line2);
	}

	if(middle_border_is_visible())
	{
		ui_set_bg(mtop_line, &cfg.cs.color[TOP_LINE_COLOR],
				cfg.cs.pair[TOP_LINE_COLOR]);
		werase(mtop_line);
		/* For some reason this wnoutrefresh is needed to make this window appear
		 * at the same time along with others during startup.  Might be an
		 * indication that all windows need a refresh, but that need is masked. */
		wnoutrefresh(mtop_line);

		ui_set_bg(mborder, &cfg.cs.color[BORDER_COLOR], cfg.cs.pair[BORDER_COLOR]);
		clear_border(mborder);
	}

	ui_set_bg(tab_line, &cfg.cs.color[TAB_LINE_COLOR],
			cfg.cs.pair[TAB_LINE_COLOR]);
	werase(tab_line);

	ui_set_bg(stat_win, &cfg.cs.color[STATUS_LINE_COLOR],
			cfg.cs.pair[STATUS_LINE_COLOR]);

	ui_set_bg(job_bar, &cfg.cs.color[JOB_LINE_COLOR],
			cfg.cs.pair[JOB_LINE_COLOR]);

	ui_set_bg(menu_win, &cfg.cs.color[WIN_COLOR], cfg.cs.pair[WIN_COLOR]);
	ui_set_bg(sort_win, &cfg.cs.color[WIN_COLOR], cfg.cs.pair[WIN_COLOR]);
	ui_set_bg(change_win, &cfg.cs.color[WIN_COLOR], cfg.cs.pair[WIN_COLOR]);
	ui_set_bg(error_win, &cfg.cs.color[WIN_COLOR], cfg.cs.pair[WIN_COLOR]);

	ui_set_bg(status_bar, &cfg.cs.color[CMD_LINE_COLOR],
			cfg.cs.pair[CMD_LINE_COLOR]);
	ui_set_bg(ruler_win, &cfg.cs.color[CMD_LINE_COLOR],
			cfg.cs.pair[CMD_LINE_COLOR]);
	ui_set_bg(input_win, &cfg.cs.color[CMD_LINE_COLOR],
			cfg.cs.pair[CMD_LINE_COLOR]);
}

void
ui_refresh_win(WINDOW *win)
{
	if(!stats_silenced_ui())
	{
		use_wrefresh(win);
	}
}

void
wprint(WINDOW *win, const char str[])
{
#ifndef _WIN32
	waddstr(win, str);
#else
	wchar_t *t = to_wide(str);
	if(t == NULL)
	{
		show_error_msg("Memory Error", "Unable to allocate enough memory");
		return;
	}

	compat_waddwstr(win, t);
	free(t);
#endif
}

void
wprinta(WINDOW *win, const char str[], const cchar_t *line_attrs,
		int attrs_xors)
{
	attr_t attrs;
	short color_pair;
	wchar_t wch[getcchar(line_attrs, NULL, &attrs, &color_pair, NULL)];
	getcchar(line_attrs, wch, &attrs, &color_pair, NULL);

	col_attr_t col = {
		.attr = attrs ^ attrs_xors,
		.gui_attr = attrs ^ attrs_xors
	};
	ui_set_attr(win, &col, color_pair);
	wprint(win, str);
	wnoutrefresh(win);
}

int
resize_for_menu_like(void)
{
	int screen_x, screen_y;

	/* Resizing everything to avoid a situation when windows that are not visible
	 * are of wrong size and using redrawwin() on them causes a memory corruption.
	 * I'm pretty sure it's a bug in ncurses. */
	ui_resize_all();
	if(curr_stats.term_state == TS_TOO_SMALL)
	{
		return 1;
	}

	update_term_size();
	flushinp(); /* without it we will get strange character on input */
	getmaxyx(stdscr, screen_y, screen_x);

	werase(stdscr);
	werase(status_bar);
	werase(ruler_win);

	wresize(menu_win, screen_y - 1, screen_x);

	update_statusbar_layout();

	ui_refresh_win(status_bar);
	ui_refresh_win(ruler_win);
	ui_refresh_win(input_win);

	return 0;
}

void
ui_setup_for_menu_like(void)
{
	if(curr_stats.load_stage > 0)
	{
		scrollok(menu_win, FALSE);
		ui_set_cursor(/*visibility=*/0);
		werase(menu_win);
		werase(status_bar);
		werase(ruler_win);
		ui_refresh_win(status_bar);
		ui_refresh_win(ruler_win);
	}
}

/* Query terminal size from the "device" and pass it to curses library. */
static void
update_term_size(void)
{
	if(curr_stats.load_stage < 1)
	{
		/* No terminal in tests. */
		return;
	}

#ifndef _WIN32
	struct winsize ws = { .ws_col = -1, .ws_row = -1 };

	if(ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
	{
		LOG_SERROR_MSG(errno, "Failed to query terminal size.");
		vifm_finish("Terminal error.");
	}
	/* Allow 0 as GNU Hurd returns this, is_term_resized() returns false then, but
	 * everything works. */
	if(ws.ws_row == (typeof(ws.ws_row))-1 || ws.ws_col == (typeof(ws.ws_col))-1)
	{
		LOG_INFO_MSG("ws.ws_row = %d; ws.ws_col = %d", ws.ws_row, ws.ws_col);
		vifm_finish("Terminal is unable to run vifm.");
	}

	if(is_term_resized(ws.ws_row, ws.ws_col))
	{
		resizeterm(ws.ws_row, ws.ws_col);
	}
#elif defined(__PDCURSES__)
	if(is_termresized())
	{
		/* This resizes vifm to window size. */
		resize_term(0, 0);
	}
#endif
}

/* Re-layouts windows located on status bar (status bar itself, input and the
 * ruler). */
static void
update_statusbar_layout(void)
{
	int screen_x, screen_y;

	int ruler_width;
	int fields_pos;

	getmaxyx(stdscr, screen_y, screen_x);

	ruler_width = get_ruler_width(curr_view);
	fields_pos = screen_x - (INPUT_WIN_WIDTH + ruler_width);

	wresize(ruler_win, 1, ruler_width);
	mvwin(ruler_win, screen_y - 1, fields_pos + INPUT_WIN_WIDTH);

	wresize(input_win, 1, INPUT_WIN_WIDTH);
	mvwin(input_win, screen_y - 1, fields_pos);

	/* We might be in command-line mode in which case we shouldn't change visible
	 * parts of the layout. */
	if(are_statusbar_widgets_visible())
	{
		wresize(status_bar, 1, fields_pos);
		mvwin(status_bar, screen_y - 1, 0);

		wnoutrefresh(ruler_win);
		wnoutrefresh(input_win);
	}
}

/* Checks whether ruler and input bar are visible.  Returns non-zero if so, zero
 * is returned otherwise. */
static int
are_statusbar_widgets_visible(void)
{
	return !ui_sb_multiline() && !ui_sb_locked();
}

/* Gets "recommended" width for the ruler.  Returns the width. */
static int
get_ruler_width(view_t *view)
{
	char *expanded;
	int len;
	int list_pos;

	/* Size must correspond to the "worst case" of the last list item. */
	list_pos = view->list_pos;
	view->list_pos = (view->list_rows == 0) ? 0 : (view->list_rows - 1);

	expanded = expand_ruler_macros(view, cfg.ruler_format);
	len = strlen(expanded);
	free(expanded);

	view->list_pos = list_pos;

	return MAX(POS_WIN_MIN_WIDTH, len);
}

/* Expands view macros to be displayed on the ruler line according to the format
 * string.  Returns newly allocated string, which should be freed by the caller,
 * or NULL if there is not enough memory. */
static char *
expand_ruler_macros(view_t *view, const char format[])
{
	return expand_view_macros(view, format, RULER_MACROS);
}

void
refresh_view_win(view_t *view)
{
	if(curr_stats.restart_in_progress)
	{
		return;
	}

	ui_refresh_win(view->win);
	refresh_bottom_lines();
}

void
move_window(view_t *view, int horizontally, int first)
{
	const SPLIT split_type = horizontally ? HSPLIT : VSPLIT;
	const view_t *const desired_view = first ? &lwin : &rwin;
	split_view(split_type);
	if(view != desired_view)
	{
		/* Switch two panes saving current windows as the active one (left/top or
		 * right/bottom). */
		switch_panes_content();
		go_to_other_pane();
		tabs_switch_panes();
	}
}

void
switch_panes(void)
{
	switch_panes_content();
	load_view_options(curr_view);
	modview_try_activate();
}

void
ui_view_pick(view_t *view, view_t **old_curr, view_t **old_other)
{
	*old_curr = curr_view;
	*old_other = other_view;

	curr_view = view;
	other_view = (view == *old_curr) ? *old_other : *old_curr;
	if(curr_view != *old_curr)
	{
		load_view_options(curr_view);
	}
}

void
ui_view_unpick(view_t *view, view_t *old_curr, view_t *old_other)
{
	if(curr_view != view)
	{
		/* Do nothing if view roles were switched from outside. */
		return;
	}

	curr_view = old_curr;
	other_view = old_other;
	if(curr_view != view)
	{
		load_view_options(curr_view);
	}
}

/* Switches panes content. */
static void
switch_panes_content(void)
{
	ui_swap_view_data(&lwin, &rwin);

	flist_update_origins(&lwin);
	flist_update_origins(&rwin);

	modview_panes_swapped();

	stats_redraw_later();
}

void
ui_swap_view_data(view_t *left, view_t *right)
{
	view_t tmp_view;
	WINDOW *tmp;
	int t;

	/* Data in some fields doesn't need to be swapped.  Swap it beforehand so that
	 * swapping structures will put it back. */

	tmp = left->win;
	left->win = right->win;
	right->win = tmp;

	t = left->window_rows;
	left->window_rows = right->window_rows;
	right->window_rows = t;

	t = left->window_cols;
	left->window_cols = right->window_cols;
	right->window_cols = t;

	tmp = left->title;
	left->title = right->title;
	right->title = tmp;

	/* Swap these fields so they reflect updated layout. */

	t = left->custom.diff_stats.unique_left;
	left->custom.diff_stats.unique_left = left->custom.diff_stats.unique_right;
	left->custom.diff_stats.unique_right = t;

	t = right->custom.diff_stats.unique_left;
	right->custom.diff_stats.unique_left = right->custom.diff_stats.unique_right;
	right->custom.diff_stats.unique_right = t;

	const int unique_lr = (CF_SHOW_UNIQUE_LEFT | CF_SHOW_UNIQUE_RIGHT);
	if((left->custom.diff_cmp_flags & unique_lr) == CF_SHOW_UNIQUE_LEFT ||
			(left->custom.diff_cmp_flags & unique_lr) == CF_SHOW_UNIQUE_RIGHT)
	{
		left->custom.diff_cmp_flags ^= unique_lr;
		right->custom.diff_cmp_flags ^= unique_lr;
	}

	tmp_view = *left;
	*left = *right;
	*right = tmp_view;
}

void
go_to_other_pane(void)
{
	change_window();
	modview_try_activate();
}

void
split_view(SPLIT orientation)
{
	if(curr_stats.number_of_windows == 2 && curr_stats.split == orientation)
		return;

	curr_stats.split = orientation;
	curr_stats.number_of_windows = 2;

	if(curr_stats.splitter_pos > 0)
	{
		stats_set_splitter_ratio(curr_stats.splitter_ratio);
	}

	stats_redraw_later();
}

void
only(void)
{
	if(curr_stats.number_of_windows != 1)
	{
		ui_hide_graphics();
		curr_stats.number_of_windows = 1;
		update_screen(UT_REDRAW);
	}
}

void
move_splitter(int by, int fact)
{
	int pos = curr_stats.splitter_pos;

	/* Determine exact splitter position if it's centered at the moment. */
	if(curr_stats.splitter_pos < 0)
	{
		if(curr_stats.split == HSPLIT)
		{
			pos = getmaxy(stdscr)/2 - 1;
		}
		else
		{
			pos = getmaxx(stdscr)/2 - 1 + getmaxx(stdscr)%2;
		}
	}

	set_splitter(pos + fact*by);
}

void
ui_remember_scroll_offset(void)
{
	const int rwin_pos = rwin.top_line/rwin.column_count;
	const int lwin_pos = lwin.top_line/lwin.column_count;
	curr_stats.scroll_bind_off = rwin_pos - lwin_pos;
}

void
ui_view_resize(view_t *view, int to)
{
	int pos;

	if(curr_stats.split == HSPLIT)
	{
		const int height = get_working_area_height();
		pos = (view == &lwin) ? (1 + to) : (height - to);
	}
	else
	{
		const int width = getmaxx(stdscr) - 1;
		pos = (view == &lwin) ? to : (width - to);
	}

	set_splitter(pos);
}

/* Sets splitter position making sure it isn't negative value which has special
 * meaning. */
static void
set_splitter(int pos)
{
	stats_set_splitter_pos(pos < 0 ? 0 : pos);
}

void
format_entry_name(const dir_entry_t *entry, NameFormat fmt, size_t buf_len,
		char buf[])
{
	if(fmt == NF_NONE)
	{
		char *escaped = escape_unreadable(entry->name);
		copy_str(buf, buf_len, escaped);
		free(escaped);
		return;
	}

	char tmp_buf[strlen(entry->name) + 1];
	const char *name = entry->name;

	if(fmt == NF_ROOT)
	{
		int root_len;
		const char *ext_pos;

		copy_str(tmp_buf, sizeof(tmp_buf), entry->name);
		split_ext(tmp_buf, &root_len, &ext_pos);
		name = tmp_buf;
	}

	char *escaped = escape_unreadable(name);

	const char *prefix, *suffix;
	ui_get_decors(entry, &prefix, &suffix);
	snprintf(buf, buf_len, "%s%s%s", prefix,
			(is_root_dir(escaped) && suffix[0] == '/') ? "" : escaped, suffix);

	free(escaped);
}

void
ui_get_decors(const dir_entry_t *entry, const char **prefix,
		const char **suffix)
{
	/* The check of actual file type can be relatively slow in some cases, so make
	 * sure we do it only when needed and at most once. */
	FileType type = FT_UNK;

	if(entry->name_dec_num == -1)
	{
		/* Find a match and cache the result. */

		((dir_entry_t *)entry)->name_dec_num = 0;
		if(cfg.name_dec_count != 0)
		{
			char full_path[PATH_MAX + 1];
			int i;

			get_full_path_of(entry, sizeof(full_path) - 1U, full_path);
			type = ui_view_entry_target_type(entry);
			if(type == FT_DIR)
			{
				strcat(full_path, "/");
			}

			for(i = 0; i < cfg.name_dec_count; ++i)
			{
				const file_dec_t *const file_dec = &cfg.name_decs[i];
				if(matchers_match(file_dec->matchers, full_path))
				{
					((dir_entry_t *)entry)->name_dec_num = i + 1;
					break;
				}
			}
		}
	}

	if(entry->name_dec_num == 0)
	{
		type = (type == FT_UNK) ? ui_view_entry_target_type(entry) : type;
		*prefix = cfg.type_decs[type][DECORATION_PREFIX];
		*suffix = cfg.type_decs[type][DECORATION_SUFFIX];
	}
	else
	{
		assert(entry->name_dec_num - 1 >= 0 && "Wrong index.");
		assert(entry->name_dec_num - 1 < cfg.name_dec_count && "Wrong index.");

		*prefix = cfg.name_decs[entry->name_dec_num - 1].prefix;
		*suffix = cfg.name_decs[entry->name_dec_num - 1].suffix;
	}
}

void
ui_view_reset_decor_cache(const view_t *view)
{
	int i;

	for(i = 0; i < view->list_rows; ++i)
	{
		view->dir_entry[i].name_dec_num = -1;
	}

	for(i = 0; i < view->left_column.entries.nentries; ++i)
	{
		view->left_column.entries.entries[i].name_dec_num = -1;
	}

	for(i = 0; i < view->right_column.entries.nentries; ++i)
	{
		view->right_column.entries.entries[i].name_dec_num = -1;
	}
}

/* Gets real type of file view entry.  Returns type of entry, resolving symbolic
 * link if needed. */
static FileType
ui_view_entry_target_type(const dir_entry_t *entry)
{
	return (fentry_is_dir(entry) ? FT_DIR : entry->type);
}

void
checked_wmove(WINDOW *win, int y, int x)
{
	if(wmove(win, y, x) == ERR)
	{
		LOG_INFO_MSG("Error moving cursor on a window to (x=%d, y=%d).", x, y);
	}
}

void
ui_set_cursor(int visibility)
{
	/* PDCurses crashes if curs_set() is called for uninitialized library. */
	if(!vifm_testing())
	{
		(void)curs_set(visibility);
	}
}

int
ui_get_mouse(MEVENT *event)
{
	int ret = getmouse(event);
	if(ret != OK)
	{
		return ret;
	}

	/* Positions after 222 can become negative due to a combination of protocol
	 * limitations and implementation.  This workaround can extend the range at
	 * least a bit when SGR 1006 isn't available. */
	if(event->x < 0)
	{
		event->x &= 0xff;
	}
	if(event->y < 0)
	{
		event->y &= 0xff;
	}

	int mask = M_ALL_MODES;
	switch(vle_mode_get())
	{
		case NORMAL_MODE:  mask |= M_NORMAL_MODE; break;
		case NAV_MODE:
		case CMDLINE_MODE: mask |= M_CMDLINE_MODE; break;
		case VISUAL_MODE:  mask |= M_VISUAL_MODE; break;
		case MENU_MODE:    mask |= M_MENU_MODE; break;
		case VIEW_MODE:    mask |= M_VIEW_MODE; break;

		default:           mask = 0; break;
	}

	return (cfg.mouse & mask ? OK : ERR);
}

WINDOW *
ui_get_tab_line_win(const view_t *view)
{
	return (view_shows_tabline(view) ? view->title : tab_line);
}

int
ui_map_tab_line(view_t *view, int x)
{
	if(!is_null_or_empty(cfg.tab_line))
	{
		/* No mouse mapping for custom tabline. */
		return -1;
	}

	path_func pf = cfg.shorten_title_paths ? &replace_home_part : &path_identity;

	const int max_width = getmaxx(ui_get_tab_line_win(view));
	tab_line_info_t info = format_tab_labels(view, max_width, pf);

	int tab_idx = -1;

	int i;
	for(i = 0; i < info.count; ++i)
	{
		if(tab_idx == -1 && x < (int)info.labels[i].attrs_len)
		{
			tab_idx = info.skipped + i;
		}
		x -= info.labels[i].attrs_len;

		cline_dispose(&info.labels[i]);
	}
	free(info.labels);

	return tab_idx;
}

int
ui_wenclose(const view_t *view, WINDOW *win, int x, int y)
{
	return (ui_view_is_visible(view) && wenclose(win, y, x));
}

void
ui_display_too_small_term_msg(void)
{
	touchwin(stdscr);
	ui_refresh_win(stdscr);

	mvwin(status_bar, 0, 0);
	wresize(status_bar, getmaxy(stdscr), getmaxx(stdscr));
	werase(status_bar);
	waddstr(status_bar, "Terminal is too small for vifm");
	touchwin(status_bar);
	ui_refresh_win(status_bar);
}

void
ui_view_win_changed(view_t *view)
{
	wnoutrefresh(view->win);
	refresh_bottom_lines();
}

/* Makes sure that status line and status bar are drawn over view window after
 * view refresh. */
static void
refresh_bottom_lines(void)
{
	/* Use getmaxy(...) instead of multiline_status_bar to handle command line
	 * mode, which doesn't use this module to show multiline messages. */
	if(cfg.display_statusline && getmaxy(status_bar) > 1)
	{
		touchwin(stat_win);
		wnoutrefresh(stat_win);
		touchwin(status_bar);
		wnoutrefresh(status_bar);
	}
}

void
ui_view_reset_selection_and_reload(view_t *view)
{
	flist_sel_stash(view);
	load_saving_pos(view);
}

void
ui_view_reset_search_highlight(view_t *view)
{
	if(view->matches != 0)
	{
		view->matches = 0;
		ui_view_schedule_redraw(view);
	}
}

void
ui_views_reload_visible_filelists(void)
{
	if(curr_stats.preview.on)
	{
		load_saving_pos(curr_view);
	}
	else
	{
		ui_views_reload_filelists();
	}
}

void
ui_views_reload_filelists(void)
{
	load_saving_pos(curr_view);
	load_saving_pos(other_view);
}

void
ui_views_update_titles(void)
{
	ui_view_title_update(&lwin);
	ui_view_title_update(&rwin);
}

void
ui_view_title_update(view_t *view)
{
	const int gen_view = vle_mode_is(VIEW_MODE) && !curr_view->explore_mode;
	view_t *selected = gen_view ? other_view : curr_view;
	path_func pf = cfg.shorten_title_paths ? &replace_home_part : &path_identity;
	col_attr_t title_col;

	if(curr_stats.load_stage < 2 || !ui_view_is_visible(view))
	{
		return;
	}

	if(view == selected && !stats_silenced_ui())
	{
		char *const term_title = format_view_title(view, pf);
		term_title_update(term_title);
		free(term_title);
	}

	title_col = fixup_titles_attributes(view, view == selected);

	if(view_shows_tabline(view))
	{
		print_tabline(view->title, view, title_col, pf);
	}
	else
	{
		char *const title = format_view_title(view, pf);
		print_view_title(view, view == selected, title);
		wnoutrefresh(view->title);
		free(title);
	}

	if(view == curr_view && get_tabline_height() > 0)
	{
		print_tabline(tab_line, view, cfg.cs.color[TAB_LINE_COLOR], pf);
	}
}

/* Identity path function.  Returns its argument. */
static char *
path_identity(const char path[])
{
	return (char *)path;
}

/* Checks whether view displays list of pane tabs at the moment.  Returns
 * non-zero if so, otherwise zero is returned. */
static int
view_shows_tabline(const view_t *view)
{
	return cfg.pane_tabs
	    && !(curr_stats.preview.on && view == other_view)
	    && cfg.show_tab_line != STL_NEVER
	    && !(cfg.show_tab_line == STL_MULTIPLE && tabs_count(view) == 1);
}

/* Retrieves height of the tab line in lines.  Returns the height. */
static int
get_tabline_height(void)
{
	if(cfg.pane_tabs || cfg.show_tab_line == STL_NEVER)
	{
		return 0;
	}

	return (cfg.show_tab_line == STL_MULTIPLE && tabs_count(curr_view) == 1)
	     ? 0
	     : 1;
}

/* Prints title of the tab on specified curses window. */
static void
print_tabline(WINDOW *win, view_t *view, col_attr_t base_col, path_func pf)
{
	const int max_width = (vifm_testing() ? cfg.columns : getmaxx(win));

	ui_set_bg(win, &base_col, -1);
	werase(win);
	checked_wmove(win, 0, 0);

	if(is_null_or_empty(cfg.tab_line))
	{
		tab_line_info_t info = format_tab_labels(view, max_width, pf);

		int i;
		for(i = 0; i < info.count; ++i)
		{
			col_attr_t col = base_col;
			if(i == info.current - info.skipped)
			{
				cs_mix_colors(&col, &cfg.cs.color[TAB_LINE_SEL_COLOR]);
			}

			cline_print(&info.labels[i], win, &col);
			cline_dispose(&info.labels[i]);
		}
		free(info.labels);
	}
	else
	{
		char *fmt;
		if(vlua_handler_cmd(curr_stats.vlua, cfg.tab_line))
		{
			int other = (view == other_view);
			fmt = vlua_make_tab_line(curr_stats.vlua, cfg.tab_line, other, max_width);
		}
		else
		{
			fmt = strdup(cfg.tab_line);
		}

		if(fmt != NULL)
		{
			cline_t title = ma_expand_colored_custom(fmt, /*nmacros=*/0,
					/*macros=*/NULL, MA_OPT);
			free(fmt);

			cline_print(&title, win, &base_col);
			cline_dispose(&title);
		}
	}

	wnoutrefresh(win);
}

/* Computes layout of the tab line and formats tab labels.  Returns list of tab
 * labels along with supplementary information. */
static tab_line_info_t
format_tab_labels(view_t *view, int max_width, path_func pf)
{
	int i;
	int tab_count = tabs_count(view);
	int width_used = 0;

	int avg_width, spare_width;
	int min_widths[tab_count];
	compute_avg_width(&avg_width, &spare_width, min_widths, max_width, view, pf);

	int min_width = 0;
	for(i = 0; i < tab_count; ++i)
	{
		min_width += min_widths[i];
	}

	cline_t *tab_labels = NULL;
	DA_INSTANCE(tab_labels);

	int current_tab = -1;
	int skipped_tabs = 0;
	tab_info_t tab_info;
	for(i = 0; tabs_get(view, i, &tab_info) && width_used < max_width; ++i)
	{
		int current = (tab_info.view == view);
		if(current)
		{
			current_tab = i;
		}
		else if(current_tab == -1 && min_width > max_width)
		{
			/* Skip a tab that precedes the current one because it doesn't fit in. */
			min_width -= min_widths[i];
			spare_width = max_width - min_width;
			++skipped_tabs;
			continue;
		}

		tab_title_info_t title_info = make_tab_title_info(&tab_info, pf, i,
				current);
		cline_t prefix = format_tab_part(cfg.tab_prefix, &title_info);
		cline_t title = make_tab_title(&title_info);
		cline_t suffix = format_tab_part(cfg.tab_suffix, &title_info);
		dispose_tab_title_info(&title_info);

		const int width_needed = title.attrs_len;
		const int extra_width = prefix.attrs_len + suffix.attrs_len;
		int width = max_width;

		if(!current)
		{
			width = tab_info.last ? (max_width - width_used)
			                      : MIN(avg_width, extra_width + width_needed);
		}

		if(width < width_needed + extra_width)
		{
			if(spare_width > 0)
			{
				width += 1;
				--spare_width;
			}

			cline_left_ellipsis(&title, MAX(0, width - extra_width),
					curr_stats.ellipsis);
		}

		int real_width = prefix.attrs_len + title.attrs_len + suffix.attrs_len;

		if(width < real_width && max_width - width_used >= real_width)
		{
			width = real_width;
		}
		if(width >= real_width)
		{
			cline_t *tab_label = DA_EXTEND(tab_labels);
			if(tab_label != NULL)
			{
				*tab_label = prefix;
				cline_append(tab_label, &title);
				cline_append(tab_label, &suffix);
				DA_COMMIT(tab_labels);
			}
		}

		width_used += real_width;
	}

	tab_line_info_t result = {
		.labels = tab_labels,
		.count = DA_SIZE(tab_labels),
		.skipped = skipped_tabs,
		.current = current_tab,
	};
	return result;
}

/* Computes average width of tab tips as well as number of spare character
 * positions. */
static void
compute_avg_width(int *avg_width, int *spare_width, int min_widths[],
		int max_width, view_t *view, path_func pf)
{
	int left = max_width;
	int widths[tabs_count(view)];
	int i;
	tab_info_t tab_info;

	*avg_width = 0;
	*spare_width = 0;

	for(i = 0; tabs_get(view, i, &tab_info); ++i)
	{
		int current = (tab_info.view == view);

		tab_title_info_t title_info = make_tab_title_info(&tab_info, pf, i,
				current);
		cline_t prefix = format_tab_part(cfg.tab_prefix, &title_info);
		cline_t title = make_tab_title(&title_info);
		cline_t suffix = format_tab_part(cfg.tab_suffix, &title_info);
		dispose_tab_title_info(&title_info);

		widths[i] = prefix.attrs_len + title.attrs_len + suffix.attrs_len;
		min_widths[i] = prefix.attrs_len + suffix.attrs_len;

		cline_dispose(&prefix);
		cline_dispose(&title);
		cline_dispose(&suffix);

		if(current)
		{
			min_widths[i] = MIN(max_width, widths[i]);

			if(tabs_count(view) != 1)
			{
				int tab_count = tabs_count(view);

				left = MAX(max_width - widths[i], 0);
				*avg_width = left/(tab_count - 1);
				*spare_width = left%(tab_count - 1);
			}
		}
	}

	int new_avg_width = *avg_width;
	do
	{
		int well_used_width = 0;
		int truncated_count = 0;
		*avg_width = new_avg_width;
		for(i = 0; tabs_get(view, i, &tab_info); ++i)
		{
			if(tab_info.view != view)
			{
				if(widths[i] <= *avg_width)
				{
					well_used_width += widths[i];
				}
				else
				{
					++truncated_count;
				}
			}
		}
		if(truncated_count == 0)
		{
			break;
		}
		new_avg_width = (left - well_used_width)/truncated_count;
		*spare_width = (left - well_used_width)%truncated_count;
	}
	while(new_avg_width != *avg_width);
}

/* Gets title of the tab.  Returns colored line. */
TSTATIC cline_t
make_tab_title(const tab_title_info_t *title_info)
{
	if(!is_null_or_empty(cfg.tab_label))
	{
		return format_tab_part(cfg.tab_label, title_info);
	}

	cline_t result = cline_make();

	if(is_null_or_empty(title_info->name))
	{
		replace_string(&result.line, title_info->escaped_view_title);
	}
	else
	{
		replace_string(&result.line, title_info->name);
	}

	result.line_len = strlen(result.line);
	cline_finish(&result);
	return result;
}

/* Produces and stores information needed to format tab title.  Returns the
 * information. */
TSTATIC tab_title_info_t
make_tab_title_info(const tab_info_t *tab_info, path_func pf, int tab_num,
		int current_tab)
{
	view_t *view = tab_info->view;

	if(cfg.tail_tab_line_paths)
	{
		/* We can just do the replacement, because shortening home part doesn't make
		 * sense for a single path entry. */
		pf = &get_last_path_component;
	}

	char path[PATH_MAX + 1];
	if(view->explore_mode)
	{
		get_current_full_path(view, sizeof(path), path);
	}
	else
	{
		copy_str(path, sizeof(path), flist_get_dir(view));
	}

	tab_title_info_t result = {
		.current = current_tab,
	};

	const char *custom_title = "";
	if(flist_custom_active(view))
	{
		custom_title = view->custom.title;
		result.tree = cv_tree(view->custom.type);
	}

	result.name = escape_unreadable(tab_info->name == NULL ? "" : tab_info->name);
	result.escaped_path = escape_unreadable(path);
	result.cv_title = escape_unreadable(custom_title);

	char *view_title = format_view_title(view, pf);
	result.escaped_view_title = escape_unreadable(view_title);
	free(view_title);

	result.num = format_str("%d", tab_num + 1);

	return result;
}

/* Frees information used to format tab title. */
TSTATIC void
dispose_tab_title_info(tab_title_info_t *title_info)
{
	free(title_info->num);
	free(title_info->escaped_view_title);
	free(title_info->cv_title);
	free(title_info->escaped_path);
	free(title_info->name);
}

/* Formats part of a tab label.  Returns colored line. */
static cline_t
format_tab_part(const char fmt[], const tab_title_info_t *title_info)
{
	custom_macro_t macros[] = {
		{ .letter = 'n', .value = title_info->name },
		{ .letter = 'N', .value = title_info->num },
		{ .letter = 't', .value = title_info->escaped_view_title },
		{ .letter = 'p', .value = title_info->escaped_path,
			.expand_mods = 1, .parent = "/" },
		{ .letter = 'c', .value = title_info->cv_title },

		{ .letter = 'C', .value = (title_info->current ? "*" : ""),
		  .flag = 1},
		{ .letter = 'T', .value = (title_info->tree ? "*" : ""),
			.flag = 1 },
	};

	cline_t title = ma_expand_colored_custom(fmt, ARRAY_LEN(macros), macros,
			MA_OPT);

	return title;
}

/* Formats title for the view.  The pf function will be applied to full paths.
 * Returns newly allocated string, which should be freed by the caller, or NULL
 * if there is not enough memory. */
static char *
format_view_title(const view_t *view, path_func pf)
{
	char *unescaped_title;

	if(view->explore_mode)
	{
		char full_path[PATH_MAX + 1];
		get_current_full_path(view, sizeof(full_path), full_path);
		unescaped_title = strdup(pf(full_path));
	}
	else if(curr_stats.preview.on && view == other_view)
	{
		const char *const viewer = modview_detached_get_viewer();
		if(viewer != NULL)
		{
			unescaped_title = format_str("Command: %s", viewer);
		}
		else
		{
			unescaped_title = format_str("File: %s",
					get_current_file_name(curr_view));
		}
	}
	else if(flist_custom_active(view))
	{
		const char *tree_mark = (cv_tree(view->custom.type) ? "[tree]" : "");
		const char *path = pf(view->custom.orig_dir);
		if(view->custom.title[0] == '\0')
		{
			unescaped_title = format_str("%s @ %s", tree_mark, path);
		}
		else
		{
			unescaped_title = format_str("%s[%s] @ %s", tree_mark, view->custom.title,
					path);
		}
	}
	else
	{
		unescaped_title = strdup(pf(view->curr_dir));
	}

	char *escaped_title = escape_unreadable(unescaped_title);
	free(unescaped_title);
	return escaped_title;
}

/* Prints view title (which can be changed for printing).  Takes care of setting
 * correct attributes. */
static void
print_view_title(const view_t *view, int active_view, char title[])
{
	char *ellipsis;

	const size_t title_width = getmaxx(view->title);
	if(title_width == (size_t)-1)
	{
		return;
	}

	werase(view->title);

	if (cfg.ellipsis_position != 0)
	{
		ellipsis = cfg.ellipsis_position < 0
		         ? left_ellipsis(title, title_width, curr_stats.ellipsis)
		         : right_ellipsis(title, title_width, curr_stats.ellipsis);
	}
	else
	{
		ellipsis = active_view
		         ? left_ellipsis(title, title_width, curr_stats.ellipsis)
		         : right_ellipsis(title, title_width, curr_stats.ellipsis);
	}

	wprint(view->title, ellipsis);
	free(ellipsis);
}

/* Updates attributes for view titles and top line.  Returns base color used for
 * the title. */
static col_attr_t
fixup_titles_attributes(const view_t *view, int active_view)
{
	col_attr_t col = cfg.cs.color[TOP_LINE_COLOR];

	if(view->title == NULL)
	{
		/* Workaround for tests. */
		return col;
	}

	if(active_view)
	{
		cs_mix_colors(&col, &cfg.cs.color[TOP_LINE_SEL_COLOR]);

		ui_set_bg(view->title, &col, -1);
		ui_set_attr(view->title, &col, -1);
	}
	else
	{
		ui_set_bg(view->title, &col, cfg.cs.pair[TOP_LINE_COLOR]);
		ui_set_attr(view->title, &col, cfg.cs.pair[TOP_LINE_COLOR]);

		ui_set_bg(mtop_line, &col, cfg.cs.pair[TOP_LINE_COLOR]);
		werase(mtop_line);
	}

	return col;
}

int
ui_view_sort_list_contains(const signed char sort[SK_COUNT], char key)
{
	int i = -1;
	while(++i < SK_COUNT)
	{
		const int sort_key = abs(sort[i]);
		if(sort_key > SK_LAST)
		{
			return 0;
		}
		else if(sort_key == key)
		{
			return 1;
		}
	}
	return 0;
}

void
ui_view_sort_list_ensure_well_formed(view_t *view, signed char sort_keys[])
{
	int found_name_key = 0;
	int i = -1;

	while(++i < SK_COUNT)
	{
		const int sort_key = abs(sort_keys[i]);
		if(sort_key > SK_LAST)
		{
			break;
		}
		else if(sort_key == SK_BY_NAME || sort_key == SK_BY_INAME)
		{
			found_name_key = 1;
		}
	}

	if(!found_name_key && i < SK_COUNT &&
			(!flist_custom_active(view) ||
			 (sort_keys == view->sort && !ui_view_unsorted(view))))
	{
		sort_keys[i++] = SK_DEFAULT;
	}

	if(i < SK_COUNT)
	{
		memset(&sort_keys[i], SK_NONE, SK_COUNT - i);
	}
}

signed char *
ui_view_sort_list_get(const view_t *view, const signed char sort[])
{
	return (flist_custom_active(view) && ui_view_unsorted(view))
	     ? (signed char *)view->custom.sort
	     : (signed char *)sort;
}

int
ui_view_displays_numbers(const view_t *view)
{
	return view->num_type != NT_NONE && ui_view_displays_columns(view);
}

int
ui_view_is_visible(const view_t *view)
{
	return curr_stats.number_of_windows == 2 || curr_view == view;
}

int
ui_view_displays_columns(const view_t *view)
{
	return !view->ls_view
	    || is_in_miller_view(view)
	    || is_forced_list_mode(view);
}

int
ui_view_main_area(const view_t *view)
{
	return ui_view_main_padded(view) - (cfg.extra_padding ? 2 : 0);
}

int
ui_view_main_padded(const view_t *view)
{
	return view->window_cols
	     - ui_view_left_reserved(view)
	     - ui_view_right_reserved(view);
}

int
ui_view_left_reserved(const view_t *view)
{
	const int total = view->miller_ratios[0] + view->miller_ratios[1]
	                + view->miller_ratios[2];
	return is_in_miller_view(view)
	     ? (view->window_cols*view->miller_ratios[0])/total : 0;
}

int
ui_view_right_reserved(const view_t *view)
{
	dir_entry_t *const entry = get_current_entry(view);

	if(!is_in_miller_view(view) || is_parent_dir(entry->name))
	{
		return 0;
	}

	if(view->miller_preview != MP_ALL &&
			fentry_is_dir(entry) != (view->miller_preview == MP_DIRS))
	{
		return 0;
	}

	const int total = view->miller_ratios[0] + view->miller_ratios[1]
	                + view->miller_ratios[2];
	return (view->window_cols*view->miller_ratios[2])/total;
}

/* Whether miller columns should be displayed.  Returns non-zero if so,
 * otherwise zero is returned. */
static int
is_in_miller_view(const view_t *view)
{
	return view->miller_view
	    && !flist_custom_active(view);
}

/* Whether view must display straight single-column file list.  Returns non-zero
 * if so, otherwise zero is returned. */
static int
is_forced_list_mode(const view_t *view)
{
	return flist_custom_active(view)
	    && (cv_tree(view->custom.type) || cv_compare(view->custom.type));
}

int
ui_qv_left(const view_t *view)
{
	const int with_margin = (!curr_stats.preview.clearing && cfg.extra_padding);
	return with_margin ? 1 : 0;
}

int
ui_qv_top(const view_t *view)
{
	const int with_margin = (!curr_stats.preview.clearing && cfg.extra_padding);
	return with_margin ? 1 : 0;
}

int
ui_qv_height(const view_t *view)
{
	const int with_margin = (!curr_stats.preview.clearing && cfg.extra_padding);
	return with_margin ? view->window_rows - 2 : view->window_rows;
}

int
ui_qv_width(const view_t *view)
{
	const int with_margin = (!curr_stats.preview.clearing && cfg.extra_padding);
	return with_margin ? view->window_cols - 2 : view->window_cols;
}

void
ui_qv_cleanup_if_needed(void)
{
	if(curr_stats.preview.on && curr_stats.preview.kind != VK_TEXTUAL)
	{
		qv_cleanup(other_view, curr_stats.preview.cleanup_cmd);
	}

	if(ui_view_is_visible(&lwin))
	{
		fview_clear_miller_preview(&lwin);
	}
	if(ui_view_is_visible(&rwin))
	{
		fview_clear_miller_preview(&rwin);
	}
}

void
ui_hide_graphics(void)
{
	if(!modes_is_menu_like())
	{
		ui_qv_cleanup_if_needed();
		modview_hide_graphics();
	}
}

void
ui_invalidate_cs(const col_scheme_t *cs)
{
	int i;
	tab_info_t tab_info;
	for(i = 0; tabs_enum_all(i, &tab_info); ++i)
	{
		if(ui_view_get_cs(tab_info.view) == cs)
		{
			fview_reset_cs(tab_info.view);
		}
	}
}

const col_scheme_t *
ui_view_get_cs(const view_t *view)
{
	return view->local_cs ? &view->cs : &cfg.cs;
}

void
ui_view_erase(view_t *view, int use_global_cs)
{
	const col_scheme_t *cs = (use_global_cs ? &cfg.cs : ui_view_get_cs(view));
	col_attr_t col = ui_get_win_color(view, cs);
	ui_set_bg(view->win, &col, -1);
	werase(view->win);
}

col_attr_t
ui_get_win_color(const view_t *view, const col_scheme_t *cs)
{
	col_attr_t col = cs->color[WIN_COLOR];
	if(view == other_view && window_shows_dirlist(view))
	{
		cs_mix_colors(&col, &cs->color[OTHER_WIN_COLOR]);
	}
	return col;
}

int
ui_view_unsorted(const view_t *view)
{
	return cv_unsorted(view->custom.type);
}

void
ui_shutdown(void)
{
	if(curr_stats.load_stage >= 0 && !vifm_testing() && !isendwin())
	{
		ui_hide_graphics();
		def_prog_mode();
		endwin();
	}
}

void
ui_pause(void)
{
#ifdef _WIN32
	/* Refresh the window, because otherwise curses redraws the screen on call to
	 * `compat_wget_wch()` (why does it do this?). */
	use_wrefresh(inf_delay_window);
#endif

	/* Show previous screen state. */
	ui_shutdown();

#ifndef _WIN32
	/* Yet restore program mode to read input without waiting for Enter. */
	reset_prog_mode();
	/* Refresh the window, because otherwise curses redraws the screen on call to
	 * `compat_wget_wch()` (why does it do this?). */
	wnoutrefresh(inf_delay_window);
#endif

	/* Ignore window resize. */
	wint_t pressed;
	do
	{
		/* Nothing. */
	}
	while(compat_wget_wch(inf_delay_window, &pressed) != ERR &&
			pressed == KEY_RESIZE);

	/* Redraw UI to account for all things including graphical preview. */
	stats_redraw_later();
}

void
ui_set_bg(WINDOW *win, const col_attr_t *col, int pair)
{
	if(curr_stats.load_stage >= 1)
	{
		const cchar_t bg = cs_color_to_cchar(col, pair);
		wbkgrndset(win, &bg);
	}
}

void
ui_set_attr(WINDOW *win, const col_attr_t *col, int pair)
{
	/* win can be NULL in tests. */
	if(curr_stats.load_stage < 1 || win == NULL)
	{
		return;
	}

	if(pair < 0)
	{
		pair = cs_load_color(col);
	}

	/*
	 * Compiler complains about unused result of comma operator, because
	 * wattr_set() is a macro and it uses comma to evaluate multiple expressions.
	 * So cast result to void.
	 *
	 * Color pair is specified twice to support both older and newer ncurses
	 * versions in one call.  MIN() might not be necessary for older versions with
	 * at most 32768 color pairs, but it won't hurt and makes the API constraints
	 * clearer.
	 */
	(void)wattr_set(win, cs_color_get_attr(col), MIN(pair, SHRT_MAX), &pair);
}

void
ui_drop_attr(WINDOW *win)
{
	if(curr_stats.load_stage >= 1)
	{
		wattrset(win, 0);
	}
}

void
ui_pass_through(const strlist_t *lines, WINDOW *win, int x, int y)
{
	/* Position hardware cursor on the screen. */
	checked_wmove(win, y, x);
	use_wrefresh(win);

	int i;
	for(i = 0; i < lines->nitems; ++i)
	{
		puts(lines->items[i]);
	}

	/* Make curses synchronize its idea about where cursor is with the terminal
	 * state. */
	touchwin(status_bar);
	checked_wmove(status_bar, 0, 0);
	use_wrefresh(status_bar);
}

int
ui_map_column_name(const char name[])
{
	int pos =
		string_array_pos((char **)&sort_enum[1], ARRAY_LEN(sort_enum) - 1, name);
	if(pos >= 0)
	{
		return 1 + pos;
	}

	return vlua_viewcolumn_map(curr_stats.vlua, name);
}

void
ui_view_schedule_redraw(view_t *view)
{
	pthread_mutex_lock(view->timestamps_mutex);
	view->need_redraw = 1;
	pthread_mutex_unlock(view->timestamps_mutex);
}

void
ui_view_schedule_reload(view_t *view)
{
	pthread_mutex_lock(view->timestamps_mutex);
	view->need_reload = 1;
	pthread_mutex_unlock(view->timestamps_mutex);
}

void
ui_view_redrawn(view_t *view)
{
	pthread_mutex_lock(view->timestamps_mutex);
	view->need_redraw = 0;
	pthread_mutex_unlock(view->timestamps_mutex);
}

UiUpdateEvent
ui_view_query_scheduled_event(view_t *view)
{
	UiUpdateEvent event;

	pthread_mutex_lock(view->timestamps_mutex);

	if(view->need_reload)
	{
		event = UUE_RELOAD;
	}
	else if(view->need_redraw)
	{
		event = UUE_REDRAW;
	}
	else
	{
		event = UUE_NONE;
	}

	view->need_redraw = 0;
	view->need_reload = 0;

	pthread_mutex_unlock(view->timestamps_mutex);

	return event;
}

/* 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