Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 3498 lines (3075 sloc) 82.142 kb
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
/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/

/*
* os_mswin.c
*
* Routines common to both Win16 and Win32.
*/

#ifdef WIN16
# ifdef __BORLANDC__
# pragma warn -par
# pragma warn -ucp
# pragma warn -use
# pragma warn -aus
# endif
#endif

#include "vim.h"

#ifdef WIN16
# define SHORT_FNAME /* always 8.3 file name */
# include <dos.h>
# include <string.h>
#endif
#include <sys/types.h>
#include <signal.h>
#include <limits.h>
#include <process.h>

#undef chdir
#ifdef __GNUC__
# ifndef __MINGW32__
# include <dirent.h>
# endif
#else
# include <direct.h>
#endif

#if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32)
# include <shellapi.h>
#endif

#if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
# include <dlgs.h>
# ifdef WIN3264
# include <winspool.h>
# else
# include <print.h>
# endif
# include <commdlg.h>
#endif

#ifdef __MINGW32__
# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
# endif
# ifndef RIGHTMOST_BUTTON_PRESSED
# define RIGHTMOST_BUTTON_PRESSED 0x0002
# endif
# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
# endif
# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
# endif
# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
# endif

/*
* EventFlags
*/
# ifndef MOUSE_MOVED
# define MOUSE_MOVED 0x0001
# endif
# ifndef DOUBLE_CLICK
# define DOUBLE_CLICK 0x0002
# endif
#endif

/*
* When generating prototypes for Win32 on Unix, these lines make the syntax
* errors disappear. They do not need to be correct.
*/
#ifdef PROTO
#define WINAPI
#define WINBASEAPI
typedef int BOOL;
typedef int CALLBACK;
typedef int COLORREF;
typedef int CONSOLE_CURSOR_INFO;
typedef int COORD;
typedef int DWORD;
typedef int ENUMLOGFONT;
typedef int HANDLE;
typedef int HDC;
typedef int HFONT;
typedef int HICON;
typedef int HWND;
typedef int INPUT_RECORD;
typedef int KEY_EVENT_RECORD;
typedef int LOGFONT;
typedef int LPARAM;
typedef int LPBOOL;
typedef int LPCSTR;
typedef int LPCWSTR;
typedef int LPSTR;
typedef int LPTSTR;
typedef int LPWSTR;
typedef int LRESULT;
typedef int MOUSE_EVENT_RECORD;
typedef int NEWTEXTMETRIC;
typedef int PACL;
typedef int PRINTDLG;
typedef int PSECURITY_DESCRIPTOR;
typedef int PSID;
typedef int SECURITY_INFORMATION;
typedef int SHORT;
typedef int SMALL_RECT;
typedef int TEXTMETRIC;
typedef int UINT;
typedef int WCHAR;
typedef int WORD;
typedef int WPARAM;
typedef void VOID;
#endif

/* Record all output and all keyboard & mouse input */
/* #define MCH_WRITE_DUMP */

#ifdef MCH_WRITE_DUMP
FILE* fdDump = NULL;
#endif

#ifdef WIN3264
extern DWORD g_PlatformId;
#endif

#ifndef FEAT_GUI_MSWIN
extern char g_szOrigTitle[];
#endif

#ifdef FEAT_GUI
extern HWND s_hwnd;
#else
static HWND s_hwnd = 0; /* console window handle, set by GetConsoleHwnd() */
#endif

extern int WSInitialized;

/* Don't generate prototypes here, because some systems do have these
* functions. */
#if defined(__GNUC__) && !defined(PROTO)
# ifndef __MINGW32__
int _stricoll(char *a, char *b)
{
    // the ANSI-ish correct way is to use strxfrm():
    char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
    strxfrm(a_buff, a, 512);
    strxfrm(b_buff, b, 512);
    return strcoll(a_buff, b_buff);
}

char * _fullpath(char *buf, char *fname, int len)
{
    LPTSTR toss;

    return (char *)GetFullPathName(fname, len, buf, &toss);
}
# endif

int _chdrive(int drive)
{
    char temp [3] = "-:";
    temp[0] = drive + 'A' - 1;
    return !SetCurrentDirectory(temp);
}
#else
# ifdef __BORLANDC__
/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
* but it does in BC 5.02! */
# if __BORLANDC__ < 0x502
int _stricoll(char *a, char *b)
{
# if 1
    // this is fast but not correct:
    return stricmp(a, b);
# else
    // the ANSI-ish correct way is to use strxfrm():
    char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
    strxfrm(a_buff, a, 512);
    strxfrm(b_buff, b, 512);
    return strcoll(a_buff, b_buff);
# endif
}
# endif
# endif
#endif


#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
/*
* GUI version of mch_exit().
* Shut down and exit with status `r'
* Careful: mch_exit() may be called before mch_init()!
*/
    void
mch_exit(int r)
{
    display_errors();

    ml_close_all(TRUE); /* remove all memfiles */

# ifdef FEAT_OLE
    UninitOLE();
# endif
# ifdef FEAT_NETBEANS_INTG
    if (WSInitialized)
    {
WSInitialized = FALSE;
WSACleanup();
    }
# endif
#ifdef DYNAMIC_GETTEXT
    dyn_libintl_end();
#endif

    if (gui.in_use)
gui_exit(r);

#ifdef EXITFREE
    free_all_mem();
#endif

    exit(r);
}

#endif /* FEAT_GUI_MSWIN */


/*
* Init the tables for toupper() and tolower().
*/
    void
mch_early_init(void)
{
    int i;

#ifdef WIN3264
    PlatformId();
#endif

    /* Init the tables for toupper() and tolower() */
    for (i = 0; i < 256; ++i)
toupper_tab[i] = tolower_tab[i] = i;
#ifdef WIN3264
    CharUpperBuff(toupper_tab, 256);
    CharLowerBuff(tolower_tab, 256);
#else
    AnsiUpperBuff(toupper_tab, 256);
    AnsiLowerBuff(tolower_tab, 256);
#endif

#if defined(FEAT_MBYTE) && !defined(FEAT_GUI)
    (void)get_cmd_argsW(NULL);
#endif
}


/*
* Return TRUE if the input comes from a terminal, FALSE otherwise.
*/
    int
mch_input_isatty()
{
#ifdef FEAT_GUI_MSWIN
    return OK; /* GUI always has a tty */
#else
    if (isatty(read_cmd_fd))
return TRUE;
    return FALSE;
#endif
}

#ifdef FEAT_TITLE
/*
* mch_settitle(): set titlebar of our window
*/
    void
mch_settitle(
    char_u *title,
    char_u *icon)
{
# ifdef FEAT_GUI_MSWIN
    gui_mch_settitle(title, icon);
# else
    if (title != NULL)
    {
# ifdef FEAT_MBYTE
if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
{
/* Convert the title from 'encoding' to the active codepage. */
WCHAR *wp = enc_to_utf16(title, NULL);
int n;

if (wp != NULL)
{
n = SetConsoleTitleW(wp);
vim_free(wp);
if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
return;
}
}
# endif
SetConsoleTitle(title);
    }
# endif
}


/*
* Restore the window/icon title.
* which is one of:
* 1: Just restore title
* 2: Just restore icon (which we don't have)
* 3: Restore title and icon (which we don't have)
*/
/*ARGSUSED*/
    void
mch_restore_title(
    int which)
{
#ifndef FEAT_GUI_MSWIN
    mch_settitle((which & 1) ? g_szOrigTitle : NULL, NULL);
#endif
}


/*
* Return TRUE if we can restore the title (we can)
*/
    int
mch_can_restore_title()
{
    return TRUE;
}


/*
* Return TRUE if we can restore the icon title (we can't)
*/
    int
mch_can_restore_icon()
{
    return FALSE;
}
#endif /* FEAT_TITLE */


/*
* Get absolute file name into buffer "buf" of length "len" bytes,
* turning all '/'s into '\\'s and getting the correct case of each component
* of the file name. Append a (back)slash to a directory name.
* When 'shellslash' set do it the other way around.
* Return OK or FAIL.
*/
/*ARGSUSED*/
    int
mch_FullName(
    char_u *fname,
    char_u *buf,
    int len,
    int force)
{
    int nResult = FAIL;

#ifdef __BORLANDC__
    if (*fname == NUL) /* Borland behaves badly here - make it consistent */
nResult = mch_dirname(buf, len);
    else
#endif
    {
#ifdef FEAT_MBYTE
if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
# ifdef __BORLANDC__
/* Wide functions of Borland C 5.5 do not work on Windows 98. */
&& g_PlatformId == VER_PLATFORM_WIN32_NT
# endif
)
{
WCHAR *wname;
WCHAR wbuf[MAX_PATH];
char_u *cname = NULL;

/* Use the wide function:
* - convert the fname from 'encoding' to UCS2.
* - invoke _wfullpath()
* - convert the result from UCS2 to 'encoding'.
*/
wname = enc_to_utf16(fname, NULL);
if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH - 1) != NULL)
{
cname = utf16_to_enc((short_u *)wbuf, NULL);
if (cname != NULL)
{
vim_strncpy(buf, cname, len - 1);
nResult = OK;
}
}
vim_free(wname);
vim_free(cname);
}
if (nResult == FAIL) /* fall back to non-wide function */
#endif
{
if (_fullpath(buf, fname, len - 1) == NULL)
{
/* failed, use relative path name */
vim_strncpy(buf, fname, len - 1);
}
else
nResult = OK;
}
    }

#ifdef USE_FNAME_CASE
    fname_case(buf, len);
#else
    slash_adjust(buf);
#endif

    return nResult;
}


/*
* Return TRUE if "fname" does not depend on the current directory.
*/
    int
mch_isFullName(char_u *fname)
{
    char szName[_MAX_PATH + 1];

    /* A name like "d:/foo" and "//server/share" is absolute */
    if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
|| (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
return TRUE;

    /* A name that can't be made absolute probably isn't absolute. */
    if (mch_FullName(fname, szName, _MAX_PATH, FALSE) == FAIL)
return FALSE;

    return pathcmp(fname, szName, -1) == 0;
}

/*
* Replace all slashes by backslashes.
* This used to be the other way around, but MS-DOS sometimes has problems
* with slashes (e.g. in a command name). We can't have mixed slashes and
* backslashes, because comparing file names will not work correctly. The
* commands that use a file name should try to avoid the need to type a
* backslash twice.
* When 'shellslash' set do it the other way around.
*/
    void
slash_adjust(p)
    char_u *p;
{
    while (*p)
    {
if (*p == psepcN)
*p = psepc;
mb_ptr_adv(p);
    }
}


/*
* stat() can't handle a trailing '/' or '\', remove it first.
*/
    int
vim_stat(const char *name, struct stat *stp)
{
    char buf[_MAX_PATH + 1];
    char *p;

    vim_strncpy((char_u *)buf, (char_u *)name, _MAX_PATH);
    p = buf + strlen(buf);
    if (p > buf)
mb_ptr_back(buf, p);
    if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
*p = NUL;
#ifdef FEAT_MBYTE
    if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
# ifdef __BORLANDC__
/* Wide functions of Borland C 5.5 do not work on Windows 98. */
&& g_PlatformId == VER_PLATFORM_WIN32_NT
# endif
       )
    {
WCHAR *wp = enc_to_utf16(buf, NULL);
int n;

if (wp != NULL)
{
n = _wstat(wp, (struct _stat *)stp);
vim_free(wp);
if (n >= 0)
return n;
/* Retry with non-wide function (for Windows 98). Can't use
* GetLastError() here and it's unclear what errno gets set to if
* the _wstat() fails for missing wide functions. */
}
    }
#endif
    return stat(buf, stp);
}

#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
/*ARGSUSED*/
    void
mch_settmode(int tmode)
{
    /* nothing to do */
}

    int
mch_get_shellsize(void)
{
    /* never used */
    return OK;
}

    void
mch_set_shellsize(void)
{
    /* never used */
}

/*
* Rows and/or Columns has changed.
*/
    void
mch_new_shellsize(void)
{
    /* never used */
}

#endif

/*
* We have no job control, so fake it by starting a new shell.
*/
    void
mch_suspend()
{
    suspend_shell();
}

#if defined(USE_MCH_ERRMSG) || defined(PROTO)

#ifdef display_errors
# undef display_errors
#endif

/*
* Display the saved error message(s).
*/
    void
display_errors()
{
    char *p;

    if (error_ga.ga_data != NULL)
    {
/* avoid putting up a message box with blanks only */
for (p = (char *)error_ga.ga_data; *p; ++p)
if (!isspace(*p))
{
(void)gui_mch_dialog(
#ifdef FEAT_GUI
gui.starting ? VIM_INFO :
#endif
VIM_ERROR,
#ifdef FEAT_GUI
gui.starting ? (char_u *)_("Message") :
#endif
(char_u *)_("Error"),
p, (char_u *)_("&Ok"), 1, NULL, FALSE);
break;
}
ga_clear(&error_ga);
    }
}
#endif


/*
* Return TRUE if "p" contain a wildcard that can be expanded by
* dos_expandpath().
*/
    int
mch_has_exp_wildcard(char_u *p)
{
    for ( ; *p; mb_ptr_adv(p))
    {
if (vim_strchr((char_u *)"?*[", *p) != NULL
|| (*p == '~' && p[1] != NUL))
return TRUE;
    }
    return FALSE;
}

/*
* Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
* shortened file name).
*/
    int
mch_has_wildcard(char_u *p)
{
    for ( ; *p; mb_ptr_adv(p))
    {
if (vim_strchr((char_u *)
# ifdef VIM_BACKTICK
"?*$[`"
# else
"?*$["
# endif
, *p) != NULL
|| (*p == '~' && p[1] != NUL))
return TRUE;
    }
    return FALSE;
}


/*
* The normal _chdir() does not change the default drive. This one does.
* Returning 0 implies success; -1 implies failure.
*/
    int
mch_chdir(char *path)
{
    if (path[0] == NUL) /* just checking... */
return -1;

    if (p_verbose >= 5)
    {
verbose_enter();
smsg((char_u *)"chdir(%s)", path);
verbose_leave();
    }
    if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
    {
/* If we can change to the drive, skip that part of the path. If we
* can't then the current directory may be invalid, try using chdir()
* with the whole path. */
if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
path += 2;
    }

    if (*path == NUL) /* drive name only */
return 0;

#ifdef FEAT_MBYTE
    if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
    {
WCHAR *p = enc_to_utf16(path, NULL);
int n;

if (p != NULL)
{
n = _wchdir(p);
vim_free(p);
if (n == 0)
return 0;
/* Retry with non-wide function (for Windows 98). */
}
    }
#endif

    return chdir(path); /* let the normal chdir() do the rest */
}


/*
* Switching off termcap mode is only allowed when Columns is 80, otherwise a
* crash may result. It's always allowed on NT or when running the GUI.
*/
/*ARGSUSED*/
    int
can_end_termcap_mode(
    int give_msg)
{
#ifdef FEAT_GUI_MSWIN
    return TRUE; /* GUI starts a new console anyway */
#else
    if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
return TRUE;
    if (give_msg)
msg(_("'columns' is not 80, cannot execute external commands"));
    return FALSE;
#endif
}

#ifdef FEAT_GUI_MSWIN
/*
* return non-zero if a character is available
*/
    int
mch_char_avail()
{
    /* never used */
    return TRUE;
}
#endif


/*
* set screen mode, always fails.
*/
/*ARGSUSED*/
    int
mch_screenmode(
    char_u *arg)
{
    EMSG(_(e_screenmode));
    return FAIL;
}


#if defined(FEAT_LIBCALL) || defined(PROTO)
/*
* Call a DLL routine which takes either a string or int param
* and returns an allocated string.
* Return OK if it worked, FAIL if not.
*/
# ifdef WIN3264
typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
typedef LPTSTR (*MYINTPROCSTR)(int);
typedef int (*MYSTRPROCINT)(LPTSTR);
typedef int (*MYINTPROCINT)(int);
# else
typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
typedef LPSTR (*MYINTPROCSTR)(int);
typedef int (*MYSTRPROCINT)(LPSTR);
typedef int (*MYINTPROCINT)(int);
# endif

# ifndef WIN16
/*
* Check if a pointer points to a valid NUL terminated string.
* Return the length of the string, including terminating NUL.
* Returns 0 for an invalid pointer, 1 for an empty string.
*/
    static size_t
check_str_len(char_u *str)
{
    SYSTEM_INFO si;
    MEMORY_BASIC_INFORMATION mbi;
    size_t length = 0;
    size_t i;
    const char *p;

    /* get page size */
    GetSystemInfo(&si);

    /* get memory information */
    if (VirtualQuery(str, &mbi, sizeof(mbi)))
    {
/* pre cast these (typing savers) */
long_u dwStr = (long_u)str;
long_u dwBaseAddress = (long_u)mbi.BaseAddress;

/* get start address of page that str is on */
long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;

/* get length from str to end of page */
long_u pageLength = si.dwPageSize - (dwStr - strPage);

for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
p += pageLength, pageLength = si.dwPageSize)
for (i = 0; i < pageLength; ++i, ++length)
if (p[i] == NUL)
return length + 1;
    }

    return 0;
}
# endif

    int
mch_libcall(
    char_u *libname,
    char_u *funcname,
    char_u *argstring, /* NULL when using a argint */
    int argint,
    char_u **string_result,/* NULL when using number_result */
    int *number_result)
{
    HINSTANCE hinstLib;
    MYSTRPROCSTR ProcAdd;
    MYINTPROCSTR ProcAddI;
    char_u *retval_str = NULL;
    int retval_int = 0;
    size_t len;

    BOOL fRunTimeLinkSuccess = FALSE;

    // Get a handle to the DLL module.
# ifdef WIN16
    hinstLib = LoadLibrary(libname);
# else
    hinstLib = vimLoadLib(libname);
# endif

    // If the handle is valid, try to get the function address.
    if (hinstLib != NULL)
    {
#ifdef HAVE_TRY_EXCEPT
__try
{
#endif
if (argstring != NULL)
{
/* Call with string argument */
ProcAdd = (MYSTRPROCSTR) GetProcAddress(hinstLib, funcname);
if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
{
if (string_result == NULL)
retval_int = ((MYSTRPROCINT)ProcAdd)(argstring);
else
retval_str = (ProcAdd)(argstring);
}
}
else
{
/* Call with number argument */
ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, funcname);
if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
{
if (string_result == NULL)
retval_int = ((MYINTPROCINT)ProcAddI)(argint);
else
retval_str = (ProcAddI)(argint);
}
}

// Save the string before we free the library.
// Assume that a "1" result is an illegal pointer.
if (string_result == NULL)
*number_result = retval_int;
else if (retval_str != NULL
# ifdef WIN16
&& retval_str != (char_u *)1
&& retval_str != (char_u *)-1
&& !IsBadStringPtr(retval_str, INT_MAX)
&& (len = strlen(retval_str) + 1) > 0
# else
&& (len = check_str_len(retval_str)) > 0
# endif
)
{
*string_result = lalloc((long_u)len, TRUE);
if (*string_result != NULL)
mch_memmove(*string_result, retval_str, len);
}

#ifdef HAVE_TRY_EXCEPT
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
RESETSTKOFLW();
fRunTimeLinkSuccess = 0;
}
#endif

// Free the DLL module.
(void)FreeLibrary(hinstLib);
    }

    if (!fRunTimeLinkSuccess)
    {
EMSG2(_(e_libcall), funcname);
return FAIL;
    }

    return OK;
}
#endif

#if defined(FEAT_MBYTE) || defined(PROTO)
/*
* Convert an UTF-8 string to UTF-16.
* "instr[inlen]" is the input. "inlen" is in bytes.
* When "outstr" is NULL only return the number of UTF-16 words produced.
* Otherwise "outstr" must be a buffer of sufficient size.
* Returns the number of UTF-16 words produced.
*/
    int
utf8_to_utf16(char_u *instr, int inlen, short_u *outstr, int *unconvlenp)
{
    int outlen = 0;
    char_u *p = instr;
    int todo = inlen;
    int l;
    int ch;

    while (todo > 0)
    {
/* Only convert if we have a complete sequence. */
l = utf_ptr2len_len(p, todo);
if (l > todo)
{
/* Return length of incomplete sequence. */
if (unconvlenp != NULL)
*unconvlenp = todo;
break;
}

ch = utf_ptr2char(p);
if (ch >= 0x10000)
{
/* non-BMP character, encoding with surrogate pairs */
++outlen;
if (outstr != NULL)
{
*outstr++ = (0xD800 - (0x10000 >> 10)) + (ch >> 10);
*outstr++ = 0xDC00 | (ch & 0x3FF);
}
}
else if (outstr != NULL)
*outstr++ = ch;
++outlen;
p += l;
todo -= l;
    }

    return outlen;
}

/*
* Convert an UTF-16 string to UTF-8.
* The input is "instr[inlen]" with "inlen" in number of UTF-16 words.
* When "outstr" is NULL only return the required number of bytes.
* Otherwise "outstr" must be a buffer of sufficient size.
* Return the number of bytes produced.
*/
    int
utf16_to_utf8(short_u *instr, int inlen, char_u *outstr)
{
    int outlen = 0;
    int todo = inlen;
    short_u *p = instr;
    int l;
    int ch, ch2;

    while (todo > 0)
    {
ch = *p;
if (ch >= 0xD800 && ch <= 0xDBFF && todo > 1)
{
/* surrogate pairs handling */
ch2 = p[1];
if (ch2 >= 0xDC00 && ch2 <= 0xDFFF)
{
ch = ((ch - 0xD800) << 10) + (ch2 & 0x3FF) + 0x10000;
++p;
--todo;
}
}
if (outstr != NULL)
{
l = utf_char2bytes(ch, outstr);
outstr += l;
}
else
l = utf_char2len(ch);
++p;
outlen += l;
--todo;
    }

    return outlen;
}

/*
* Call MultiByteToWideChar() and allocate memory for the result.
* Returns the result in "*out[*outlen]" with an extra zero appended.
* "outlen" is in words.
*/
    void
MultiByteToWideChar_alloc(UINT cp, DWORD flags,
LPCSTR in, int inlen,
LPWSTR *out, int *outlen)
{
    *outlen = MultiByteToWideChar(cp, flags, in, inlen, 0, 0);
    /* Add one one word to avoid a zero-length alloc(). */
    *out = (LPWSTR)alloc(sizeof(WCHAR) * (*outlen + 1));
    if (*out != NULL)
    {
MultiByteToWideChar(cp, flags, in, inlen, *out, *outlen);
(*out)[*outlen] = 0;
    }
}

/*
* Call WideCharToMultiByte() and allocate memory for the result.
* Returns the result in "*out[*outlen]" with an extra NUL appended.
*/
    void
WideCharToMultiByte_alloc(UINT cp, DWORD flags,
LPCWSTR in, int inlen,
LPSTR *out, int *outlen,
LPCSTR def, LPBOOL useddef)
{
    *outlen = WideCharToMultiByte(cp, flags, in, inlen, NULL, 0, def, useddef);
    /* Add one one byte to avoid a zero-length alloc(). */
    *out = alloc((unsigned)*outlen + 1);
    if (*out != NULL)
    {
WideCharToMultiByte(cp, flags, in, inlen, *out, *outlen, def, useddef);
(*out)[*outlen] = 0;
    }
}

#endif /* FEAT_MBYTE */

#ifdef FEAT_CLIPBOARD
/*
* Clipboard stuff, for cutting and pasting text to other windows.
*/

/* Type used for the clipboard type of Vim's data. */
typedef struct
{
    int type; /* MCHAR, MBLOCK or MLINE */
    int txtlen; /* length of CF_TEXT in bytes */
    int ucslen; /* length of CF_UNICODETEXT in words */
    int rawlen; /* length of clip_star.format_raw, including encoding,
excluding terminating NUL */
} VimClipType_t;

/*
* Make vim the owner of the current selection. Return OK upon success.
*/
/*ARGSUSED*/
    int
clip_mch_own_selection(VimClipboard *cbd)
{
    /*
* Never actually own the clipboard. If another application sets the
* clipboard, we don't want to think that we still own it.
*/
    return FAIL;
}

/*
* Make vim NOT the owner of the current selection.
*/
/*ARGSUSED*/
    void
clip_mch_lose_selection(VimClipboard *cbd)
{
    /* Nothing needs to be done here */
}

/*
* Copy "str[*size]" into allocated memory, changing CR-NL to NL.
* Return the allocated result and the size in "*size".
* Returns NULL when out of memory.
*/
    static char_u *
crnl_to_nl(const char_u *str, int *size)
{
    int pos = 0;
    int str_len = *size;
    char_u *ret;
    char_u *retp;

    /* Avoid allocating zero bytes, it generates an error message. */
    ret = lalloc((long_u)(str_len == 0 ? 1 : str_len), TRUE);
    if (ret != NULL)
    {
retp = ret;
for (pos = 0; pos < str_len; ++pos)
{
if (str[pos] == '\r' && str[pos + 1] == '\n')
{
++pos;
--(*size);
}
*retp++ = str[pos];
}
    }

    return ret;
}

/*
* Wait for another process to Close the Clipboard.
* Returns TRUE for success.
*/
    static int
vim_open_clipboard(void)
{
    int delay = 10;

    while (!OpenClipboard(NULL))
    {
if (delay > 500)
return FALSE; /* waited too long, give up */
Sleep(delay);
delay *= 2; /* wait for 10, 20, 40, 80, etc. msec */
    }
    return TRUE;
}

/*
* Get the current selection and put it in the clipboard register.
*
* NOTE: Must use GlobalLock/Unlock here to ensure Win32s compatibility.
* On NT/W95 the clipboard data is a fixed global memory object and
* so its handle = its pointer.
* On Win32s, however, co-operation with the Win16 system means that
* the clipboard data is moveable and its handle is not a pointer at all,
* so we can't just cast the return value of GetClipboardData to (char_u*).
* <VN>
*/
    void
clip_mch_request_selection(VimClipboard *cbd)
{
    VimClipType_t metadata = { -1, -1, -1, -1 };
    HGLOBAL hMem = NULL;
    char_u *str = NULL;
#if defined(FEAT_MBYTE) && defined(WIN3264)
    char_u *to_free = NULL;
#endif
#ifdef FEAT_MBYTE
    HGLOBAL rawh = NULL;
#endif
    int str_size = 0;
    int maxlen;
    size_t n;

    /*
* Don't pass GetActiveWindow() as an argument to OpenClipboard() because
* then we can't paste back into the same window for some reason - webb.
*/
    if (!vim_open_clipboard())
return;

    /* Check for vim's own clipboard format first. This only gets the type of
* the data, still need to use CF_UNICODETEXT or CF_TEXT for the text. */
    if (IsClipboardFormatAvailable(cbd->format))
    {
VimClipType_t *meta_p;
HGLOBAL meta_h;

/* We have metadata on the clipboard; try to get it. */
if ((meta_h = GetClipboardData(cbd->format)) != NULL
&& (meta_p = (VimClipType_t *)GlobalLock(meta_h)) != NULL)
{
/* The size of "VimClipType_t" changed, "rawlen" was added later.
* Only copy what is available for backwards compatibility. */
n = sizeof(VimClipType_t);
if (GlobalSize(meta_h) < n)
n = GlobalSize(meta_h);
memcpy(&metadata, meta_p, n);
GlobalUnlock(meta_h);
}
    }

#ifdef FEAT_MBYTE
    /* Check for Vim's raw clipboard format first. This is used without
* conversion, but only if 'encoding' matches. */
    if (IsClipboardFormatAvailable(cbd->format_raw)
&& metadata.rawlen > (int)STRLEN(p_enc))
    {
/* We have raw data on the clipboard; try to get it. */
if ((rawh = GetClipboardData(cbd->format_raw)) != NULL)
{
char_u *rawp;

rawp = (char_u *)GlobalLock(rawh);
if (rawp != NULL && STRCMP(p_enc, rawp) == 0)
{
n = STRLEN(p_enc) + 1;
str = rawp + n;
str_size = (int)(metadata.rawlen - n);
}
else
{
GlobalUnlock(rawh);
rawh = NULL;
}
}
    }
    if (str == NULL)
    {
#endif

#if defined(FEAT_MBYTE) && defined(WIN3264)
    /* Try to get the clipboard in Unicode if it's not an empty string. */
    if (IsClipboardFormatAvailable(CF_UNICODETEXT) && metadata.ucslen != 0)
    {
HGLOBAL hMemW;

if ((hMemW = GetClipboardData(CF_UNICODETEXT)) != NULL)
{
WCHAR *hMemWstr = (WCHAR *)GlobalLock(hMemW);

/* Use the length of our metadata if possible, but limit it to the
* GlobalSize() for safety. */
maxlen = (int)(GlobalSize(hMemW) / sizeof(WCHAR));
if (metadata.ucslen >= 0)
{
if (metadata.ucslen > maxlen)
str_size = maxlen;
else
str_size = metadata.ucslen;
}
else
{
for (str_size = 0; str_size < maxlen; ++str_size)
if (hMemWstr[str_size] == NUL)
break;
}
to_free = str = utf16_to_enc((short_u *)hMemWstr, &str_size);
GlobalUnlock(hMemW);
}
    }
    else
#endif
    /* Get the clipboard in the Active codepage. */
    if (IsClipboardFormatAvailable(CF_TEXT))
    {
if ((hMem = GetClipboardData(CF_TEXT)) != NULL)
{
str = (char_u *)GlobalLock(hMem);

/* The length is either what our metadata says or the strlen().
* But limit it to the GlobalSize() for safety. */
maxlen = (int)GlobalSize(hMem);
if (metadata.txtlen >= 0)
{
if (metadata.txtlen > maxlen)
str_size = maxlen;
else
str_size = metadata.txtlen;
}
else
{
for (str_size = 0; str_size < maxlen; ++str_size)
if (str[str_size] == NUL)
break;
}

# if defined(FEAT_MBYTE) && defined(WIN3264)
/* The text is in the active codepage. Convert to 'encoding',
* going through UTF-16. */
acp_to_enc(str, str_size, &to_free, &maxlen);
if (to_free != NULL)
{
str_size = maxlen;
str = to_free;
}
# endif
}
    }
#ifdef FEAT_MBYTE
    }
#endif

    if (str != NULL && *str != NUL)
    {
char_u *temp_clipboard;

/* If the type is not known detect it. */
if (metadata.type == -1)
metadata.type = MAUTO;

/* Translate <CR><NL> into <NL>. */
temp_clipboard = crnl_to_nl(str, &str_size);
if (temp_clipboard != NULL)
{
clip_yank_selection(metadata.type, temp_clipboard, str_size, cbd);
vim_free(temp_clipboard);
}
    }

    /* unlock the global object */
    if (hMem != NULL)
GlobalUnlock(hMem);
#ifdef FEAT_MBYTE
    if (rawh != NULL)
GlobalUnlock(rawh);
#endif
    CloseClipboard();
#if defined(FEAT_MBYTE) && defined(WIN3264)
    vim_free(to_free);
#endif
}

/*
* Send the current selection to the clipboard.
*/
    void
clip_mch_set_selection(VimClipboard *cbd)
{
    char_u *str = NULL;
    VimClipType_t metadata;
    long_u txtlen;
    HGLOBAL hMemRaw = NULL;
    HGLOBAL hMem = NULL;
    HGLOBAL hMemVim = NULL;
# if defined(FEAT_MBYTE) && defined(WIN3264)
    HGLOBAL hMemW = NULL;
# endif

    /* If the '*' register isn't already filled in, fill it in now */
    cbd->owned = TRUE;
    clip_get_selection(cbd);
    cbd->owned = FALSE;

    /* Get the text to be put on the clipboard, with CR-LF. */
    metadata.type = clip_convert_selection(&str, &txtlen, cbd);
    if (metadata.type < 0)
return;
    metadata.txtlen = (int)txtlen;
    metadata.ucslen = 0;
    metadata.rawlen = 0;

#ifdef FEAT_MBYTE
    /* Always set the raw bytes: 'encoding', NUL and the text. This is used
* when copy/paste from/to Vim with the same 'encoding', so that illegal
* bytes can also be copied and no conversion is needed. */
    {
LPSTR lpszMemRaw;

metadata.rawlen = (int)(txtlen + STRLEN(p_enc) + 1);
hMemRaw = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
metadata.rawlen + 1);
lpszMemRaw = (LPSTR)GlobalLock(hMemRaw);
if (lpszMemRaw != NULL)
{
STRCPY(lpszMemRaw, p_enc);
memcpy(lpszMemRaw + STRLEN(p_enc) + 1, str, txtlen + 1);
GlobalUnlock(hMemRaw);
}
else
metadata.rawlen = 0;
    }
#endif

# if defined(FEAT_MBYTE) && defined(WIN3264)
    {
WCHAR *out;
int len = metadata.txtlen;

/* Convert the text to UTF-16. This is put on the clipboard as
* CF_UNICODETEXT. */
out = (WCHAR *)enc_to_utf16(str, &len);
if (out != NULL)
{
WCHAR *lpszMemW;

/* Convert the text for CF_TEXT to Active codepage. Otherwise it's
* p_enc, which has no relation to the Active codepage. */
metadata.txtlen = WideCharToMultiByte(GetACP(), 0, out, len,
NULL, 0, 0, 0);
vim_free(str);
str = (char_u *)alloc((unsigned)(metadata.txtlen == 0 ? 1
: metadata.txtlen));
if (str == NULL)
{
vim_free(out);
return; /* out of memory */
}
WideCharToMultiByte(GetACP(), 0, out, len,
str, metadata.txtlen, 0, 0);

/* Allocate memory for the UTF-16 text, add one NUL word to
* terminate the string. */
hMemW = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
(len + 1) * sizeof(WCHAR));
lpszMemW = (WCHAR *)GlobalLock(hMemW);
if (lpszMemW != NULL)
{
memcpy(lpszMemW, out, len * sizeof(WCHAR));
lpszMemW[len] = NUL;
GlobalUnlock(hMemW);
}
vim_free(out);
metadata.ucslen = len;
}
    }
# endif

    /* Allocate memory for the text, add one NUL byte to terminate the string.
*/
    hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1);
    {
LPSTR lpszMem = (LPSTR)GlobalLock(hMem);

if (lpszMem)
{
vim_strncpy(lpszMem, str, metadata.txtlen);
GlobalUnlock(hMem);
}
    }

    /* Set up metadata: */
    {
VimClipType_t *lpszMemVim = NULL;

hMemVim = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
sizeof(VimClipType_t));
lpszMemVim = (VimClipType_t *)GlobalLock(hMemVim);
memcpy(lpszMemVim, &metadata, sizeof(metadata));
GlobalUnlock(hMemVim);
    }

    /*
* Open the clipboard, clear it and put our text on it.
* Always set our Vim format. Put Unicode and plain text on it.
*
* Don't pass GetActiveWindow() as an argument to OpenClipboard()
* because then we can't paste back into the same window for some
* reason - webb.
*/
    if (vim_open_clipboard())
    {
if (EmptyClipboard())
{
SetClipboardData(cbd->format, hMemVim);
hMemVim = 0;
# if defined(FEAT_MBYTE) && defined(WIN3264)
if (hMemW != NULL)
{
if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL)
hMemW = NULL;
}
# endif
/* Always use CF_TEXT. On Win98 Notepad won't obtain the
* CF_UNICODETEXT text, only CF_TEXT. */
SetClipboardData(CF_TEXT, hMem);
hMem = 0;
}
CloseClipboard();
    }

    vim_free(str);
    /* Free any allocations we didn't give to the clipboard: */
    if (hMemRaw)
GlobalFree(hMemRaw);
    if (hMem)
GlobalFree(hMem);
# if defined(FEAT_MBYTE) && defined(WIN3264)
    if (hMemW)
GlobalFree(hMemW);
# endif
    if (hMemVim)
GlobalFree(hMemVim);
}

#endif /* FEAT_CLIPBOARD */

#if defined(FEAT_MBYTE) || defined(PROTO)
/*
* Note: the following two functions are only guaranteed to work when using
* valid MS-Windows codepages or when iconv() is available.
*/

/*
* Convert "str" from 'encoding' to UTF-16.
* Input in "str" with length "*lenp". When "lenp" is NULL, use strlen().
* Output is returned as an allocated string. "*lenp" is set to the length of
* the result. A trailing NUL is always added.
* Returns NULL when out of memory.
*/
    short_u *
enc_to_utf16(char_u *str, int *lenp)
{
    vimconv_T conv;
    WCHAR *ret;
    char_u *allocbuf = NULL;
    int len_loc;
    int length;

    if (lenp == NULL)
    {
len_loc = (int)STRLEN(str) + 1;
lenp = &len_loc;
    }

    if (enc_codepage > 0)
    {
/* We can do any CP### -> UTF-16 in one pass, and we can do it
* without iconv() (convert_* may need iconv). */
MultiByteToWideChar_alloc(enc_codepage, 0, str, *lenp, &ret, &length);
    }
    else
    {
/* Use "latin1" by default, we might be called before we have p_enc
* set up. Convert to utf-8 first, works better with iconv(). Does
* nothing if 'encoding' is "utf-8". */
conv.vc_type = CONV_NONE;
if (convert_setup(&conv, p_enc ? p_enc : (char_u *)"latin1",
(char_u *)"utf-8") == FAIL)
return NULL;
if (conv.vc_type != CONV_NONE)
{
str = allocbuf = string_convert(&conv, str, lenp);
if (str == NULL)
return NULL;
}
convert_setup(&conv, NULL, NULL);

length = utf8_to_utf16(str, *lenp, NULL, NULL);
ret = (WCHAR *)alloc((unsigned)((length + 1) * sizeof(WCHAR)));
if (ret != NULL)
{
utf8_to_utf16(str, *lenp, (short_u *)ret, NULL);
ret[length] = 0;
}

vim_free(allocbuf);
    }

    *lenp = length;
    return (short_u *)ret;
}

/*
* Convert an UTF-16 string to 'encoding'.
* Input in "str" with length (counted in wide characters) "*lenp". When
* "lenp" is NULL, use wcslen().
* Output is returned as an allocated string. If "*lenp" is not NULL it is
* set to the length of the result.
* Returns NULL when out of memory.
*/
    char_u *
utf16_to_enc(short_u *str, int *lenp)
{
    vimconv_T conv;
    char_u *utf8_str = NULL, *enc_str = NULL;
    int len_loc;

    if (lenp == NULL)
    {
len_loc = (int)wcslen(str) + 1;
lenp = &len_loc;
    }

    if (enc_codepage > 0)
    {
/* We can do any UTF-16 -> CP### in one pass. */
int length;

WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp,
(LPSTR *)&enc_str, &length, 0, 0);
*lenp = length;
return enc_str;
    }

    /* Avoid allocating zero bytes, it generates an error message. */
    utf8_str = alloc(utf16_to_utf8(str, *lenp == 0 ? 1 : *lenp, NULL));
    if (utf8_str != NULL)
    {
*lenp = utf16_to_utf8(str, *lenp, utf8_str);

/* We might be called before we have p_enc set up. */
conv.vc_type = CONV_NONE;
convert_setup(&conv, (char_u *)"utf-8",
p_enc? p_enc: (char_u *)"latin1");
if (conv.vc_type == CONV_NONE)
{
/* p_enc is utf-8, so we're done. */
enc_str = utf8_str;
}
else
{
enc_str = string_convert(&conv, utf8_str, lenp);
vim_free(utf8_str);
}

convert_setup(&conv, NULL, NULL);
    }

    return enc_str;
}
#endif /* FEAT_MBYTE */

#if (defined(FEAT_MBYTE) && defined(WIN3264)) || defined(PROTO)
/*
* Convert from the active codepage to 'encoding'.
* Input is "str[str_size]".
* The result is in allocated memory: "out[outlen]". With terminating NUL.
*/
    void
acp_to_enc(str, str_size, out, outlen)
    char_u *str;
    int str_size;
    char_u **out;
    int *outlen;

{
    LPWSTR widestr;

    MultiByteToWideChar_alloc(GetACP(), 0, str, str_size, &widestr, outlen);
    if (widestr != NULL)
    {
++*outlen; /* Include the 0 after the string */
*out = utf16_to_enc((short_u *)widestr, outlen);
vim_free(widestr);
    }
}
#endif


/*
* Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
*/
/*ARGSUSED*/
    void
DumpPutS(
    const char *psz)
{
# ifdef MCH_WRITE_DUMP
    if (fdDump)
    {
fputs(psz, fdDump);
if (psz[strlen(psz) - 1] != '\n')
fputc('\n', fdDump);
fflush(fdDump);
    }
# endif
}

#ifdef _DEBUG

void __cdecl
Trace(
    char *pszFormat,
    ...)
{
    CHAR szBuff[2048];
    va_list args;

    va_start(args, pszFormat);
    vsprintf(szBuff, pszFormat, args);
    va_end(args);

    OutputDebugString(szBuff);
}

#endif //_DEBUG

#if !defined(FEAT_GUI) || defined(PROTO)
# if defined(FEAT_TITLE) && defined(WIN3264)
extern HWND g_hWnd; /* This is in os_win32.c. */
# endif

/*
* Showing the printer dialog is tricky since we have no GUI
* window to parent it. The following routines are needed to
* get the window parenting and Z-order to work properly.
*/
    static void
GetConsoleHwnd(void)
{
# define MY_BUFSIZE 1024 // Buffer size for console window titles.

    char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
    char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.

    /* Skip if it's already set. */
    if (s_hwnd != 0)
return;

# if defined(FEAT_TITLE) && defined(WIN3264)
    /* Window handle may have been found by init code (Windows NT only) */
    if (g_hWnd != 0)
    {
s_hwnd = g_hWnd;
return;
    }
# endif

    GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

    wsprintf(pszNewWindowTitle, "%s/%d/%d",
pszOldWindowTitle,
GetTickCount(),
GetCurrentProcessId());
    SetConsoleTitle(pszNewWindowTitle);
    Sleep(40);
    s_hwnd = FindWindow(NULL, pszNewWindowTitle);

    SetConsoleTitle(pszOldWindowTitle);
}

/*
* Console implementation of ":winpos".
*/
    int
mch_get_winpos(int *x, int *y)
{
    RECT rect;

    GetConsoleHwnd();
    GetWindowRect(s_hwnd, &rect);
    *x = rect.left;
    *y = rect.top;
    return OK;
}

/*
* Console implementation of ":winpos x y".
*/
    void
mch_set_winpos(int x, int y)
{
    GetConsoleHwnd();
    SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}
#endif

#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)

# ifdef WIN16
# define TEXT(a) a
# endif
/*=================================================================
* Win32 printer stuff
*/

static HFONT prt_font_handles[2][2][2];
static PRINTDLG prt_dlg;
static const int boldface[2] = {FW_REGULAR, FW_BOLD};
static TEXTMETRIC prt_tm;
static int prt_line_height;
static int prt_number_width;
static int prt_left_margin;
static int prt_right_margin;
static int prt_top_margin;
static char_u szAppName[] = TEXT("VIM");
static HWND hDlgPrint;
static int *bUserAbort = NULL;
static char_u *prt_name = NULL;

/* Defines which are also in vim.rc. */
#define IDC_BOX1 400
#define IDC_PRINTTEXT1 401
#define IDC_PRINTTEXT2 402
#define IDC_PROGRESS 403

/*
* Convert BGR to RGB for Windows GDI calls
*/
    static COLORREF
swap_me(COLORREF colorref)
{
    int temp;
    char *ptr = (char *)&colorref;

    temp = *(ptr);
    *(ptr ) = *(ptr + 2);
    *(ptr + 2) = temp;
    return colorref;
}

/* Attempt to make this work for old and new compilers */
#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
# define PDP_RETVAL BOOL
#else
# define PDP_RETVAL INT_PTR
#endif

/*ARGSUSED*/
    static PDP_RETVAL CALLBACK
PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
#ifdef FEAT_GETTEXT
    NONCLIENTMETRICS nm;
    static HFONT hfont;
#endif

    switch (message)
    {
case WM_INITDIALOG:
#ifdef FEAT_GETTEXT
nm.cbSize = sizeof(NONCLIENTMETRICS);
if (SystemParametersInfo(
SPI_GETNONCLIENTMETRICS,
sizeof(NONCLIENTMETRICS),
&nm,
0))
{
char buff[MAX_PATH];
int i;

/* Translate the dialog texts */
hfont = CreateFontIndirect(&nm.lfMessageFont);
for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
{
SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
SetDlgItemText(hDlg,i, _(buff));
}
SendDlgItemMessage(hDlg, IDCANCEL,
WM_SETFONT, (WPARAM)hfont, 1);
if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
SetDlgItemText(hDlg,IDCANCEL, _(buff));
}
#endif
SetWindowText(hDlg, szAppName);
if (prt_name != NULL)
{
SetDlgItemText(hDlg, IDC_PRINTTEXT2, (LPSTR)prt_name);
vim_free(prt_name);
prt_name = NULL;
}
EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
#ifndef FEAT_GUI
BringWindowToTop(s_hwnd);
#endif
return TRUE;

case WM_COMMAND:
*bUserAbort = TRUE;
EnableWindow(GetParent(hDlg), TRUE);
DestroyWindow(hDlg);
hDlgPrint = NULL;
#ifdef FEAT_GETTEXT
DeleteObject(hfont);
#endif
return TRUE;
    }
    return FALSE;
}

/*ARGSUSED*/
    static BOOL CALLBACK
AbortProc(HDC hdcPrn, int iCode)
{
    MSG msg;

    while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
{
TranslateMessage(&msg);
pDispatchMessage(&msg);
}
    }
    return !*bUserAbort;
}

#ifndef FEAT_GUI

    static UINT_PTR CALLBACK
PrintHookProc(
HWND hDlg, // handle to dialog box
UINT uiMsg, // message identifier
WPARAM wParam, // message parameter
LPARAM lParam // message parameter
)
{
    HWND hwndOwner;
    RECT rc, rcDlg, rcOwner;
    PRINTDLG *pPD;

    if (uiMsg == WM_INITDIALOG)
    {
// Get the owner window and dialog box rectangles.
if ((hwndOwner = GetParent(hDlg)) == NULL)
hwndOwner = GetDesktopWindow();

GetWindowRect(hwndOwner, &rcOwner);
GetWindowRect(hDlg, &rcDlg);
CopyRect(&rc, &rcOwner);

// Offset the owner and dialog box rectangles so that
// right and bottom values represent the width and
// height, and then offset the owner again to discard
// space taken up by the dialog box.

OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
OffsetRect(&rc, -rc.left, -rc.top);
OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);

// The new position is the sum of half the remaining
// space and the owner's original position.

SetWindowPos(hDlg,
HWND_TOP,
rcOwner.left + (rc.right / 2),
rcOwner.top + (rc.bottom / 2),
0, 0, // ignores size arguments
SWP_NOSIZE);

/* tackle the printdlg copiesctrl problem */
pPD = (PRINTDLG *)lParam;
pPD->nCopies = (WORD)pPD->lCustData;
SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
/* Bring the window to top */
BringWindowToTop(GetParent(hDlg));
SetForegroundWindow(hDlg);
    }

    return FALSE;
}
#endif

    void
mch_print_cleanup(void)
{
    int pifItalic;
    int pifBold;
    int pifUnderline;

    for (pifBold = 0; pifBold <= 1; pifBold++)
for (pifItalic = 0; pifItalic <= 1; pifItalic++)
for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);

    if (prt_dlg.hDC != NULL)
DeleteDC(prt_dlg.hDC);
    if (!*bUserAbort)
SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
}

    static int
to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
{
    int ret = 0;
    int u;
    int nr;

    u = prt_get_unit(idx);
    if (u == PRT_UNIT_NONE)
    {
u = PRT_UNIT_PERC;
nr = def_number;
    }
    else
nr = printer_opts[idx].number;

    switch (u)
    {
case PRT_UNIT_PERC:
ret = (physsize * nr) / 100;
break;
case PRT_UNIT_INCH:
ret = (nr * dpi);
break;
case PRT_UNIT_MM:
ret = (nr * 10 * dpi) / 254;
break;
case PRT_UNIT_POINT:
ret = (nr * 10 * dpi) / 720;
break;
    }

    if (ret < offset)
return 0;
    else
return ret - offset;
}

    static int
prt_get_cpl(void)
{
    int hr;
    int phyw;
    int dvoff;
    int rev_offset;
    int dpi;
#ifdef WIN16
    POINT pagesize;
#endif

    GetTextMetrics(prt_dlg.hDC, &prt_tm);
    prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;

    hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
#ifdef WIN16
    Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
    phyw = pagesize.x;
    Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
    dvoff = pagesize.x;
#else
    phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
    dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
#endif
    dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);

    rev_offset = phyw - (dvoff + hr);

    prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
    if (prt_use_number())
    {
prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
prt_left_margin += prt_number_width;
    }
    else
prt_number_width = 0;

    prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
rev_offset, 5);

    return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
}

    static int
prt_get_lpp(void)
{
    int vr;
    int phyw;
    int dvoff;
    int rev_offset;
    int bottom_margin;
    int dpi;
#ifdef WIN16
    POINT pagesize;
#endif

    vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
#ifdef WIN16
    Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
    phyw = pagesize.y;
    Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
    dvoff = pagesize.y;
#else
    phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
    dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
#endif
    dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);

    rev_offset = phyw - (dvoff + vr);

    prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);

    /* adjust top margin if there is a header */
    prt_top_margin += prt_line_height * prt_header_height();

    bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
rev_offset, 5);

    return (bottom_margin - prt_top_margin) / prt_line_height;
}

    int
mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
{
    static HGLOBAL stored_dm = NULL;
    static HGLOBAL stored_devn = NULL;
    static int stored_nCopies = 1;
    static int stored_nFlags = 0;

    LOGFONT fLogFont;
    int pifItalic;
    int pifBold;
    int pifUnderline;

    DEVMODE *mem;
    DEVNAMES *devname;
    int i;

    bUserAbort = &(psettings->user_abort);
    vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
    prt_dlg.lStructSize = sizeof(PRINTDLG);
#ifndef FEAT_GUI
    GetConsoleHwnd(); /* get value of s_hwnd */
#endif
    prt_dlg.hwndOwner = s_hwnd;
    prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
    if (!forceit)
    {
prt_dlg.hDevMode = stored_dm;
prt_dlg.hDevNames = stored_devn;
prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
#ifndef FEAT_GUI
/*
* Use hook to prevent console window being sent to back
*/
prt_dlg.lpfnPrintHook = PrintHookProc;
prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
#endif
prt_dlg.Flags |= stored_nFlags;
    }

    /*
* If bang present, return default printer setup with no dialog
* never show dialog if we are running over telnet
*/
    if (forceit
#ifndef FEAT_GUI
|| !term_console
#endif
)
    {
prt_dlg.Flags |= PD_RETURNDEFAULT;
#ifdef WIN3264
/*
* MSDN suggests setting the first parameter to WINSPOOL for
* NT, but NULL appears to work just as well.
*/
if (*p_pdev != NUL)
prt_dlg.hDC = CreateDC(NULL, p_pdev, NULL, NULL);
else
#endif
{
prt_dlg.Flags |= PD_RETURNDEFAULT;
if (PrintDlg(&prt_dlg) == 0)
goto init_fail_dlg;
}
    }
    else if (PrintDlg(&prt_dlg) == 0)
goto init_fail_dlg;
    else
    {
/*
* keep the previous driver context
*/
stored_dm = prt_dlg.hDevMode;
stored_devn = prt_dlg.hDevNames;
stored_nFlags = prt_dlg.Flags;
stored_nCopies = prt_dlg.nCopies;
    }

    if (prt_dlg.hDC == NULL)
    {
EMSG(_("E237: Printer selection failed"));
mch_print_cleanup();
return FALSE;
    }

    /* Not all printer drivers report the support of color (or grey) in the
* same way. Let's set has_color if there appears to be some way to print
* more than B&W. */
    i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
    psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
|| GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
|| i > 2 || i == -1);

    /* Ensure all font styles are baseline aligned */
    SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);

    /*
* On some windows systems the nCopies parameter is not
* passed back correctly. It must be retrieved from the
* hDevMode struct.
*/
    mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
    if (mem != NULL)
    {
#ifdef WIN3264
if (mem->dmCopies != 1)
stored_nCopies = mem->dmCopies;
#endif
if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
psettings->duplex = TRUE;
if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
psettings->has_color = TRUE;
    }
    GlobalUnlock(prt_dlg.hDevMode);

    devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
    if (devname != 0)
    {
char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
char_u *port_name = (char_u *)devname +devname->wOutputOffset;
char_u *text = _("to %s on %s");

prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
+ STRLEN(text)));
if (prt_name != NULL)
wsprintf(prt_name, text, printer_name, port_name);
    }
    GlobalUnlock(prt_dlg.hDevNames);

    /*
* Initialise the font according to 'printfont'
*/
    vim_memset(&fLogFont, 0, sizeof(fLogFont));
    if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
    {
EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
mch_print_cleanup();
return FALSE;
    }

    for (pifBold = 0; pifBold <= 1; pifBold++)
for (pifItalic = 0; pifItalic <= 1; pifItalic++)
for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
{
fLogFont.lfWeight = boldface[pifBold];
fLogFont.lfItalic = pifItalic;
fLogFont.lfUnderline = pifUnderline;
prt_font_handles[pifBold][pifItalic][pifUnderline]
= CreateFontIndirect(&fLogFont);
}

    SetBkMode(prt_dlg.hDC, OPAQUE);
    SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);

    /*
* Fill in the settings struct
*/
    psettings->chars_per_line = prt_get_cpl();
    psettings->lines_per_page = prt_get_lpp();
    psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
? prt_dlg.nCopies : 1;
    psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
? 1 : prt_dlg.nCopies;

    if (psettings->n_collated_copies == 0)
psettings->n_collated_copies = 1;

    if (psettings->n_uncollated_copies == 0)
psettings->n_uncollated_copies = 1;

    psettings->jobname = jobname;

    return TRUE;

init_fail_dlg:
    {
DWORD err = CommDlgExtendedError();

if (err)
{
#ifdef WIN16
char buf[20];

sprintf(buf, "%ld", err);
EMSG2(_("E238: Print error: %s"), buf);
#else
char_u *buf;

/* I suspect FormatMessage() doesn't work for values returned by
* CommDlgExtendedError(). What does? */
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
EMSG2(_("E238: Print error: %s"),
buf == NULL ? (char_u *)_("Unknown") : buf);
LocalFree((LPVOID)(buf));
#endif
}
else
msg_clr_eos(); /* Maybe canceled */

mch_print_cleanup();
return FALSE;
    }
}


    int
mch_print_begin(prt_settings_T *psettings)
{
    int ret;
    static DOCINFO di;
    char szBuffer[300];

    hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
prt_dlg.hwndOwner, PrintDlgProc);
#ifdef WIN16
    Escape(prt_dlg.hDC, SETABORTPROC, 0, (LPSTR)AbortProc, NULL);
#else
    SetAbortProc(prt_dlg.hDC, AbortProc);
#endif
    wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
    SetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (LPSTR)szBuffer);

    vim_memset(&di, 0, sizeof(DOCINFO));
    di.cbSize = sizeof(DOCINFO);
    di.lpszDocName = psettings->jobname;
    ret = StartDoc(prt_dlg.hDC, &di);

#ifdef FEAT_GUI
    /* Give focus back to main window (when using MDI). */
    SetFocus(s_hwnd);
#endif

    return (ret > 0);
}

/*ARGSUSED*/
    void
mch_print_end(prt_settings_T *psettings)
{
    EndDoc(prt_dlg.hDC);
    if (!*bUserAbort)
SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
}

    int
mch_print_end_page(void)
{
    return (EndPage(prt_dlg.hDC) > 0);
}

    int
mch_print_begin_page(char_u *msg)
{
    if (msg != NULL)
SetDlgItemText(hDlgPrint, IDC_PROGRESS, (LPSTR)msg);
    return (StartPage(prt_dlg.hDC) > 0);
}

    int
mch_print_blank_page(void)
{
    return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
}

static int prt_pos_x = 0;
static int prt_pos_y = 0;

    void
mch_print_start_line(margin, page_line)
    int margin;
    int page_line;
{
    if (margin)
prt_pos_x = -prt_number_width;
    else
prt_pos_x = 0;
    prt_pos_y = page_line * prt_line_height
+ prt_tm.tmAscent + prt_tm.tmExternalLeading;
}

    int
mch_print_text_out(char_u *p, int len)
{
#ifdef FEAT_PROPORTIONAL_FONTS
    SIZE sz;
#endif

    TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
prt_pos_y + prt_top_margin, p, len);
#ifndef FEAT_PROPORTIONAL_FONTS
    prt_pos_x += len * prt_tm.tmAveCharWidth;
    return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
+ prt_tm.tmOverhang > prt_right_margin);
#else
# ifdef WIN16
    GetTextExtentPoint(prt_dlg.hDC, p, len, &sz);
# else
    GetTextExtentPoint32(prt_dlg.hDC, p, len, &sz);
# endif
    prt_pos_x += (sz.cx - prt_tm.tmOverhang);
    /* This is wrong when printing spaces for a TAB. */
    if (p[len] == NUL)
return FALSE;
# ifdef WIN16
    GetTextExtentPoint(prt_dlg.hDC, p + len, 1, &sz);
# else
    GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
# endif
    return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
#endif
}

    void
mch_print_set_font(int iBold, int iItalic, int iUnderline)
{
    SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
}

    void
mch_print_set_bg(long_u bgcol)
{
    SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
swap_me((COLORREF)bgcol)));
    /*
* With a white background we can draw characters transparent, which is
* good for italic characters that overlap to the next char cell.
*/
    if (bgcol == 0xffffffUL)
SetBkMode(prt_dlg.hDC, TRANSPARENT);
    else
SetBkMode(prt_dlg.hDC, OPAQUE);
}

    void
mch_print_set_fg(long_u fgcol)
{
    SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
swap_me((COLORREF)fgcol)));
}

#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/



#if defined(FEAT_SHORTCUT) || defined(PROTO)
# include <shlobj.h>

/*
* When "fname" is the name of a shortcut (*.lnk) resolve the file it points
* to and return that name in allocated memory.
* Otherwise NULL is returned.
*/
    char_u *
mch_resolve_shortcut(char_u *fname)
{
    HRESULT hr;
    IShellLink *psl = NULL;
    IPersistFile *ppf = NULL;
    OLECHAR wsz[MAX_PATH];
    WIN32_FIND_DATA ffd; // we get those free of charge
    TCHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
    char_u *rfname = NULL;
    int len;

    /* Check if the file name ends in ".lnk". Avoid calling
* CoCreateInstance(), it's quite slow. */
    if (fname == NULL)
return rfname;
    len = (int)STRLEN(fname);
    if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
return rfname;

    CoInitialize(NULL);

    // create a link manager object and request its interface
    hr = CoCreateInstance(
&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
&IID_IShellLink, (void**)&psl);
    if (hr != S_OK)
goto shortcut_error;

    // Get a pointer to the IPersistFile interface.
    hr = psl->lpVtbl->QueryInterface(
psl, &IID_IPersistFile, (void**)&ppf);
    if (hr != S_OK)
goto shortcut_error;

    // full path string must be in Unicode.
    MultiByteToWideChar(CP_ACP, 0, fname, -1, wsz, MAX_PATH);

    // "load" the name and resolve the link
    hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
    if (hr != S_OK)
goto shortcut_error;
#if 0 // This makes Vim wait a long time if the target doesn't exist.
hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
if (hr != S_OK)
goto shortcut_error;
#endif

    // Get the path to the link target.
    ZeroMemory(buf, MAX_PATH);
    hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
    if (hr == S_OK && buf[0] != NUL)
rfname = vim_strsave(buf);

shortcut_error:
    // Release all interface pointers (both belong to the same object)
    if (ppf != NULL)
ppf->lpVtbl->Release(ppf);
    if (psl != NULL)
psl->lpVtbl->Release(psl);

    CoUninitialize();
    return rfname;
}
#endif

#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
/*
* Bring ourselves to the foreground. Does work if the OS doesn't allow it.
*/
    void
win32_set_foreground()
{
# ifndef FEAT_GUI
    GetConsoleHwnd(); /* get value of s_hwnd */
# endif
    if (s_hwnd != 0)
SetForegroundWindow(s_hwnd);
}
#endif

#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
/*
* Client-server code for Vim
*
* Originally written by Paul Moore
*/

/* In order to handle inter-process messages, we need to have a window. But
* the functions in this module can be called before the main GUI window is
* created (and may also be called in the console version, where there is no
* GUI window at all).
*
* So we create a hidden window, and arrange to destroy it on exit.
*/
HWND message_window = 0; /* window that's handling messsages */

#define VIM_CLASSNAME "VIM_MESSAGES"
#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)

/* Communication is via WM_COPYDATA messages. The message type is send in
* the dwData parameter. Types are defined here. */
#define COPYDATA_KEYS 0
#define COPYDATA_REPLY 1
#define COPYDATA_EXPR 10
#define COPYDATA_RESULT 11
#define COPYDATA_ERROR_RESULT 12
#define COPYDATA_ENCODING 20

/* This is a structure containing a server HWND and its name. */
struct server_id
{
    HWND hwnd;
    char_u *name;
};

/* Last received 'encoding' that the client uses. */
static char_u *client_enc = NULL;

/*
* Tell the other side what encoding we are using.
* Errors are ignored.
*/
    static void
serverSendEnc(HWND target)
{
    COPYDATASTRUCT data;

    data.dwData = COPYDATA_ENCODING;
#ifdef FEAT_MBYTE
    data.cbData = (DWORD)STRLEN(p_enc) + 1;
    data.lpData = p_enc;
#else
    data.cbData = (DWORD)STRLEN("latin1") + 1;
    data.lpData = "latin1";
#endif
    (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
(LPARAM)(&data));
}

/*
* Clean up on exit. This destroys the hidden message window.
*/
    static void
#ifdef __BORLANDC__
    _RTLENTRYF
#endif
CleanUpMessaging(void)
{
    if (message_window != 0)
    {
DestroyWindow(message_window);
message_window = 0;
    }
}

static int save_reply(HWND server, char_u *reply, int expr);

/*s
* The window procedure for the hidden message window.
* It handles callback messages and notifications from servers.
* In order to process these messages, it is necessary to run a
* message loop. Code which may run before the main message loop
* is started (in the GUI) is careful to pump messages when it needs
* to. Features which require message delivery during normal use will
* not work in the console version - this basically means those
* features which allow Vim to act as a server, rather than a client.
*/
    static LRESULT CALLBACK
Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if (msg == WM_COPYDATA)
    {
/* This is a message from another Vim. The dwData member of the
* COPYDATASTRUCT determines the type of message:
* COPYDATA_ENCODING:
* The encoding that the client uses. Following messages will
* use this encoding, convert if needed.
* COPYDATA_KEYS:
* A key sequence. We are a server, and a client wants these keys
* adding to the input queue.
* COPYDATA_REPLY:
* A reply. We are a client, and a server has sent this message
* in response to a request. (server2client())
* COPYDATA_EXPR:
* An expression. We are a server, and a client wants us to
* evaluate this expression.
* COPYDATA_RESULT:
* A reply. We are a client, and a server has sent this message
* in response to a COPYDATA_EXPR.
* COPYDATA_ERROR_RESULT:
* A reply. We are a client, and a server has sent this message
* in response to a COPYDATA_EXPR that failed to evaluate.
*/
COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
HWND sender = (HWND)wParam;
COPYDATASTRUCT reply;
char_u *res;
char_u winstr[30];
int retval;
char_u *str;
char_u *tofree;

switch (data->dwData)
{
case COPYDATA_ENCODING:
# ifdef FEAT_MBYTE
/* Remember the encoding that the client uses. */
vim_free(client_enc);
client_enc = enc_canonize((char_u *)data->lpData);
# endif
return 1;

case COPYDATA_KEYS:
/* Remember who sent this, for <client> */
clientWindow = sender;

/* Add the received keys to the input buffer. The loop waiting
* for the user to do something should check the input buffer. */
str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
server_to_input_buf(str);
vim_free(tofree);

# ifdef FEAT_GUI
/* Wake up the main GUI loop. */
if (s_hwnd != 0)
PostMessage(s_hwnd, WM_NULL, 0, 0);
# endif
return 1;

case COPYDATA_EXPR:
/* Remember who sent this, for <client> */
clientWindow = sender;

str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
res = eval_client_expr_to_string(str);
vim_free(tofree);

if (res == NULL)
{
res = vim_strsave(_(e_invexprmsg));
reply.dwData = COPYDATA_ERROR_RESULT;
}
else
reply.dwData = COPYDATA_RESULT;
reply.lpData = res;
reply.cbData = (DWORD)STRLEN(res) + 1;

serverSendEnc(sender);
retval = (int)SendMessage(sender, WM_COPYDATA, (WPARAM)message_window,
(LPARAM)(&reply));
vim_free(res);
return retval;

case COPYDATA_REPLY:
case COPYDATA_RESULT:
case COPYDATA_ERROR_RESULT:
if (data->lpData != NULL)
{
str = serverConvert(client_enc, (char_u *)data->lpData,
&tofree);
if (tofree == NULL)
str = vim_strsave(str);
if (save_reply(sender, str,
(data->dwData == COPYDATA_REPLY ? 0 :
(data->dwData == COPYDATA_RESULT ? 1 :
2))) == FAIL)
vim_free(str);
#ifdef FEAT_AUTOCMD
else if (data->dwData == COPYDATA_REPLY)
{
sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
TRUE, curbuf);
}
#endif
}
return 1;
}

return 0;
    }

    else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
    {
/* When the message window is activated (brought to the foreground),
* this actually applies to the text window. */
#ifndef FEAT_GUI
GetConsoleHwnd(); /* get value of s_hwnd */
#endif
if (s_hwnd != 0)
{
SetForegroundWindow(s_hwnd);
return 0;
}
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}

/*
* Initialise the message handling process. This involves creating a window
* to handle messages - the window will not be visible.
*/
    void
serverInitMessaging(void)
{
    WNDCLASS wndclass;
    HINSTANCE s_hinst;

    /* Clean up on exit */
    atexit(CleanUpMessaging);

    /* Register a window class - we only really care
* about the window procedure
*/
    s_hinst = (HINSTANCE)GetModuleHandle(0);
    wndclass.style = 0;
    wndclass.lpfnWndProc = Messaging_WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = s_hinst;
    wndclass.hIcon = NULL;
    wndclass.hCursor = NULL;
    wndclass.hbrBackground = NULL;
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = VIM_CLASSNAME;
    RegisterClass(&wndclass);

    /* Create the message window. It will be hidden, so the details don't
* matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
* focus from gvim. */
    message_window = CreateWindow(VIM_CLASSNAME, "",
WS_POPUPWINDOW | WS_CAPTION,
CW_USEDEFAULT, CW_USEDEFAULT,
100, 100, NULL, NULL,
s_hinst, NULL);
}

/* Used by serverSendToVim() to find an alternate server name. */
static char_u *altname_buf_ptr = NULL;

/*
* Get the title of the window "hwnd", which is the Vim server name, in
* "name[namelen]" and return the length.
* Returns zero if window "hwnd" is not a Vim server.
*/
    static int
getVimServerName(HWND hwnd, char *name, int namelen)
{
    int len;
    char buffer[VIM_CLASSNAME_LEN + 1];

    /* Ignore windows which aren't Vim message windows */
    len = GetClassName(hwnd, buffer, sizeof(buffer));
    if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
return 0;

    /* Get the title of the window */
    return GetWindowText(hwnd, name, namelen);
}

    static BOOL CALLBACK
enumWindowsGetServer(HWND hwnd, LPARAM lparam)
{
    struct server_id *id = (struct server_id *)lparam;
    char server[MAX_PATH];

    /* Get the title of the window */
    if (getVimServerName(hwnd, server, sizeof(server)) == 0)
return TRUE;

    /* If this is the server we're looking for, return its HWND */
    if (STRICMP(server, id->name) == 0)
    {
id->hwnd = hwnd;
return FALSE;
    }

    /* If we are looking for an alternate server, remember this name. */
    if (altname_buf_ptr != NULL
&& STRNICMP(server, id->name, STRLEN(id->name)) == 0
&& vim_isdigit(server[STRLEN(id->name)]))
    {
STRCPY(altname_buf_ptr, server);
altname_buf_ptr = NULL; /* don't use another name */
    }

    /* Otherwise, keep looking */
    return TRUE;
}

    static BOOL CALLBACK
enumWindowsGetNames(HWND hwnd, LPARAM lparam)
{
    garray_T *ga = (garray_T *)lparam;
    char server[MAX_PATH];

    /* Get the title of the window */
    if (getVimServerName(hwnd, server, sizeof(server)) == 0)
return TRUE;

    /* Add the name to the list */
    ga_concat(ga, server);
    ga_concat(ga, "\n");
    return TRUE;
}

    static HWND
findServer(char_u *name)
{
    struct server_id id;

    id.name = name;
    id.hwnd = 0;

    EnumWindows(enumWindowsGetServer, (LPARAM)(&id));

    return id.hwnd;
}

    void
serverSetName(char_u *name)
{
    char_u *ok_name;
    HWND hwnd = 0;
    int i = 0;
    char_u *p;

    /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
    ok_name = alloc((unsigned)STRLEN(name) + 10);

    STRCPY(ok_name, name);
    p = ok_name + STRLEN(name);

    for (;;)
    {
/* This is inefficient - we're doing an EnumWindows loop for each
* possible name. It would be better to grab all names in one go,
* and scan the list each time...
*/
hwnd = findServer(ok_name);
if (hwnd == 0)
break;

++i;
if (i >= 1000)
break;

sprintf((char *)p, "%d", i);
    }

    if (hwnd != 0)
vim_free(ok_name);
    else
    {
/* Remember the name */
serverName = ok_name;
#ifdef FEAT_TITLE
need_maketitle = TRUE; /* update Vim window title later */
#endif

/* Update the message window title */
SetWindowText(message_window, ok_name);

#ifdef FEAT_EVAL
/* Set the servername variable */
set_vim_var_string(VV_SEND_SERVER, serverName, -1);
#endif
    }
}

    char_u *
serverGetVimNames(void)
{
    garray_T ga;

    ga_init2(&ga, 1, 100);

    EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
    ga_append(&ga, NUL);

    return ga.ga_data;
}

    int
serverSendReply(name, reply)
    char_u *name; /* Where to send. */
    char_u *reply; /* What to send. */
{
    HWND target;
    COPYDATASTRUCT data;
    long_u n = 0;

    /* The "name" argument is a magic cookie obtained from expand("<client>").
* It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
* value of the client's message window HWND.
*/
    sscanf((char *)name, SCANF_HEX_LONG_U, &n);
    if (n == 0)
return -1;

    target = (HWND)n;
    if (!IsWindow(target))
return -1;

    data.dwData = COPYDATA_REPLY;
    data.cbData = (DWORD)STRLEN(reply) + 1;
    data.lpData = reply;

    serverSendEnc(target);
    if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
(LPARAM)(&data)))
return 0;

    return -1;
}

    int
serverSendToVim(name, cmd, result, ptarget, asExpr, silent)
    char_u *name; /* Where to send. */
    char_u *cmd; /* What to send. */
    char_u **result; /* Result of eval'ed expression */
    void *ptarget; /* HWND of server */
    int asExpr; /* Expression or keys? */
    int silent; /* don't complain about no server */
{
    HWND target;
    COPYDATASTRUCT data;
    char_u *retval = NULL;
    int retcode = 0;
    char_u altname_buf[MAX_PATH];

    /* If the server name does not end in a digit then we look for an
* alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
    if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
altname_buf_ptr = altname_buf;
    altname_buf[0] = NUL;
    target = findServer(name);
    altname_buf_ptr = NULL;
    if (target == 0 && altname_buf[0] != NUL)
/* Use another server name we found. */
target = findServer(altname_buf);

    if (target == 0)
    {
if (!silent)
EMSG2(_(e_noserver), name);
return -1;
    }

    if (ptarget)
*(HWND *)ptarget = target;

    data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
    data.cbData = (DWORD)STRLEN(cmd) + 1;
    data.lpData = cmd;

    serverSendEnc(target);
    if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
(LPARAM)(&data)) == 0)
return -1;

    if (asExpr)
retval = serverGetReply(target, &retcode, TRUE, TRUE);

    if (result == NULL)
vim_free(retval);
    else
*result = retval; /* Caller assumes responsibility for freeing */

    return retcode;
}

/*
* Bring the server to the foreground.
*/
    void
serverForeground(name)
    char_u *name;
{
    HWND target = findServer(name);

    if (target != 0)
SetForegroundWindow(target);
}

/* Replies from server need to be stored until the client picks them up via
* remote_read(). So we maintain a list of server-id/reply pairs.
* Note that there could be multiple replies from one server pending if the
* client is slow picking them up.
* We just store the replies in a simple list. When we remove an entry, we
* move list entries down to fill the gap.
* The server ID is simply the HWND.
*/
typedef struct
{
    HWND server; /* server window */
    char_u *reply; /* reply string */
    int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
} reply_T;

static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};

#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
#define REPLY_COUNT (reply_list.ga_len)

/* Flag which is used to wait for a reply */
static int reply_received = 0;

/*
* Store a reply. "reply" must be allocated memory (or NULL).
*/
    static int
save_reply(HWND server, char_u *reply, int expr)
{
    reply_T *rep;

    if (ga_grow(&reply_list, 1) == FAIL)
return FAIL;

    rep = REPLY_ITEM(REPLY_COUNT);
    rep->server = server;
    rep->reply = reply;
    rep->expr_result = expr;
    if (rep->reply == NULL)
return FAIL;

    ++REPLY_COUNT;
    reply_received = 1;
    return OK;
}

/*
* Get a reply from server "server".
* When "expr_res" is non NULL, get the result of an expression, otherwise a
* server2client() message.
* When non NULL, point to return code. 0 => OK, -1 => ERROR
* If "remove" is TRUE, consume the message, the caller must free it then.
* if "wait" is TRUE block until a message arrives (or the server exits).
*/
    char_u *
serverGetReply(HWND server, int *expr_res, int remove, int wait)
{
    int i;
    char_u *reply;
    reply_T *rep;

    /* When waiting, loop until the message waiting for is received. */
    for (;;)
    {
/* Reset this here, in case a message arrives while we are going
* through the already received messages. */
reply_received = 0;

for (i = 0; i < REPLY_COUNT; ++i)
{
rep = REPLY_ITEM(i);
if (rep->server == server
&& ((rep->expr_result != 0) == (expr_res != NULL)))
{
/* Save the values we've found for later */
reply = rep->reply;
if (expr_res != NULL)
*expr_res = rep->expr_result == 1 ? 0 : -1;

if (remove)
{
/* Move the rest of the list down to fill the gap */
mch_memmove(rep, rep + 1,
(REPLY_COUNT - i - 1) * sizeof(reply_T));
--REPLY_COUNT;
}

/* Return the reply to the caller, who takes on responsibility
* for freeing it if "remove" is TRUE. */
return reply;
}
}

/* If we got here, we didn't find a reply. Return immediately if the
* "wait" parameter isn't set. */
if (!wait)
break;

/* We need to wait for a reply. Enter a message loop until the
* "reply_received" flag gets set. */

/* Loop until we receive a reply */
while (reply_received == 0)
{
/* Wait for a SendMessage() call to us. This could be the reply
* we are waiting for. Use a timeout of a second, to catch the
* situation that the server died unexpectedly. */
MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);

/* If the server has died, give up */
if (!IsWindow(server))
return NULL;

serverProcessPendingMessages();
}
    }

    return NULL;
}

/*
* Process any messages in the Windows message queue.
*/
    void
serverProcessPendingMessages(void)
{
    MSG msg;

    while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
TranslateMessage(&msg);
pDispatchMessage(&msg);
    }
}

#endif /* FEAT_CLIENTSERVER */

#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
|| defined(PROTO)

struct charset_pair
{
    char *name;
    BYTE charset;
};

static struct charset_pair
charset_pairs[] =
{
    {"ANSI", ANSI_CHARSET},
    {"CHINESEBIG5", CHINESEBIG5_CHARSET},
    {"DEFAULT", DEFAULT_CHARSET},
    {"HANGEUL", HANGEUL_CHARSET},
    {"OEM", OEM_CHARSET},
    {"SHIFTJIS", SHIFTJIS_CHARSET},
    {"SYMBOL", SYMBOL_CHARSET},
#ifdef WIN3264
    {"ARABIC", ARABIC_CHARSET},
    {"BALTIC", BALTIC_CHARSET},
    {"EASTEUROPE", EASTEUROPE_CHARSET},
    {"GB2312", GB2312_CHARSET},
    {"GREEK", GREEK_CHARSET},
    {"HEBREW", HEBREW_CHARSET},
    {"JOHAB", JOHAB_CHARSET},
    {"MAC", MAC_CHARSET},
    {"RUSSIAN", RUSSIAN_CHARSET},
    {"THAI", THAI_CHARSET},
    {"TURKISH", TURKISH_CHARSET},
# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
&& (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
    {"VIETNAMESE", VIETNAMESE_CHARSET},
# endif
#endif
    {NULL, 0}
};

/*
* Convert a charset ID to a name.
* Return NULL when not recognized.
*/
    char *
charset_id2name(int id)
{
    struct charset_pair *cp;

    for (cp = charset_pairs; cp->name != NULL; ++cp)
if ((BYTE)id == cp->charset)
break;
    return cp->name;
}

static const LOGFONT s_lfDefault =
{
    -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
    OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
    PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
    "Fixedsys" /* see _ReadVimIni */
};

/* Initialise the "current height" to -12 (same as s_lfDefault) just
* in case the user specifies a font in "guifont" with no size before a font
* with an explicit size has been set. This defaults the size to this value
* (-12 equates to roughly 9pt).
*/
int current_font_height = -12; /* also used in gui_w48.c */

/* Convert a string representing a point size into pixels. The string should
* be a positive decimal number, with an optional decimal point (eg, "12", or
* "10.5"). The pixel value is returned, and a pointer to the next unconverted
* character is stored in *end. The flag "vertical" says whether this
* calculation is for a vertical (height) size or a horizontal (width) one.
*/
    static int
points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
{
    int pixels;
    int points = 0;
    int divisor = 0;
    HWND hwnd = (HWND)0;
    HDC hdc;
    HDC printer_dc = (HDC)pprinter_dc;

    while (*str != NUL)
    {
if (*str == '.' && divisor == 0)
{
/* Start keeping a divisor, for later */
divisor = 1;
}
else
{
if (!VIM_ISDIGIT(*str))
break;

points *= 10;
points += *str - '0';
divisor *= 10;
}
++str;
    }

    if (divisor == 0)
divisor = 1;

    if (printer_dc == NULL)
    {
hwnd = GetDesktopWindow();
hdc = GetWindowDC(hwnd);
    }
    else
hdc = printer_dc;

    pixels = MulDiv(points,
GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
72 * divisor);

    if (printer_dc == NULL)
ReleaseDC(hwnd, hdc);

    *end = str;
    return pixels;
}

/*ARGSUSED*/
    static int CALLBACK
font_enumproc(
    ENUMLOGFONT *elf,
    NEWTEXTMETRIC *ntm,
    int type,
    LPARAM lparam)
{
    /* Return value:
* 0 = terminate now (monospace & ANSI)
* 1 = continue, still no luck...
* 2 = continue, but we have an acceptable LOGFONT
* (monospace, not ANSI)
* We use these values, as EnumFontFamilies returns 1 if the
* callback function is never called. So, we check the return as
* 0 = perfect, 2 = OK, 1 = no good...
* It's not pretty, but it works!
*/

    LOGFONT *lf = (LOGFONT *)(lparam);

#ifndef FEAT_PROPORTIONAL_FONTS
    /* Ignore non-monospace fonts without further ado */
    if ((ntm->tmPitchAndFamily & 1) != 0)
return 1;
#endif

    /* Remember this LOGFONT as a "possible" */
    *lf = elf->elfLogFont;

    /* Terminate the scan as soon as we find an ANSI font */
    if (lf->lfCharSet == ANSI_CHARSET
|| lf->lfCharSet == OEM_CHARSET
|| lf->lfCharSet == DEFAULT_CHARSET)
return 0;

    /* Continue the scan - we have a non-ANSI font */
    return 2;
}

    static int
init_logfont(LOGFONT *lf)
{
    int n;
    HWND hwnd = GetDesktopWindow();
    HDC hdc = GetWindowDC(hwnd);

    n = EnumFontFamilies(hdc,
(LPCSTR)lf->lfFaceName,
(FONTENUMPROC)font_enumproc,
(LPARAM)lf);

    ReleaseDC(hwnd, hdc);

    /* If we couldn't find a useable font, return failure */
    if (n == 1)
return FAIL;

    /* Tidy up the rest of the LOGFONT structure. We set to a basic
* font - get_logfont() sets bold, italic, etc based on the user's
* input.
*/
    lf->lfHeight = current_font_height;
    lf->lfWidth = 0;
    lf->lfItalic = FALSE;
    lf->lfUnderline = FALSE;
    lf->lfStrikeOut = FALSE;
    lf->lfWeight = FW_NORMAL;

    /* Return success */
    return OK;
}

/*
* Get font info from "name" into logfont "lf".
* Return OK for a valid name, FAIL otherwise.
*/
    int
get_logfont(
    LOGFONT *lf,
    char_u *name,
    HDC printer_dc,
    int verbose)
{
    char_u *p;
    int i;
    static LOGFONT *lastlf = NULL;

    *lf = s_lfDefault;
    if (name == NULL)
return OK;

    if (STRCMP(name, "*") == 0)
    {
#if defined(FEAT_GUI_W32)
CHOOSEFONT cf;
/* if name is "*", bring up std font dialog: */
vim_memset(&cf, 0, sizeof(cf));
cf.lStructSize = sizeof(cf);
cf.hwndOwner = s_hwnd;
cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
if (lastlf != NULL)
*lf = *lastlf;
cf.lpLogFont = lf;
cf.nFontType = 0 ; //REGULAR_FONTTYPE;
if (ChooseFont(&cf))
goto theend;
#else
return FAIL;
#endif
    }

    /*
* Split name up, it could be <name>:h<height>:w<width> etc.
*/
    for (p = name; *p && *p != ':'; p++)
    {
if (p - name + 1 > LF_FACESIZE)
return FAIL; /* Name too long */
lf->lfFaceName[p - name] = *p;
    }
    if (p != name)
lf->lfFaceName[p - name] = NUL;

    /* First set defaults */
    lf->lfHeight = -12;
    lf->lfWidth = 0;
    lf->lfWeight = FW_NORMAL;
    lf->lfItalic = FALSE;
    lf->lfUnderline = FALSE;
    lf->lfStrikeOut = FALSE;

    /*
* If the font can't be found, try replacing '_' by ' '.
*/
    if (init_logfont(lf) == FAIL)
    {
int did_replace = FALSE;

for (i = 0; lf->lfFaceName[i]; ++i)
if (lf->lfFaceName[i] == '_')
{
lf->lfFaceName[i] = ' ';
did_replace = TRUE;
}
if (!did_replace || init_logfont(lf) == FAIL)
return FAIL;
    }

    while (*p == ':')
p++;

    /* Set the values found after ':' */
    while (*p)
    {
switch (*p++)
{
case 'h':
lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
break;
case 'w':
lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
break;
case 'b':
#ifndef MSWIN16_FASTTEXT
lf->lfWeight = FW_BOLD;
#endif
break;
case 'i':
#ifndef MSWIN16_FASTTEXT
lf->lfItalic = TRUE;
#endif
break;
case 'u':
lf->lfUnderline = TRUE;
break;
case 's':
lf->lfStrikeOut = TRUE;
break;
case 'c':
{
struct charset_pair *cp;

for (cp = charset_pairs; cp->name != NULL; ++cp)
if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
{
lf->lfCharSet = cp->charset;
p += strlen(cp->name);
break;
}
if (cp->name == NULL && verbose)
{
vim_snprintf((char *)IObuff, IOSIZE,
_("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
EMSG(IObuff);
break;
}
break;
}
default:
if (verbose)
{
vim_snprintf((char *)IObuff, IOSIZE,
_("E245: Illegal char '%c' in font name \"%s\""),
p[-1], name);
EMSG(IObuff);
}
return FAIL;
}
while (*p == ':')
p++;
    }

#if defined(FEAT_GUI_W32)
theend:
#endif
    /* ron: init lastlf */
    if (printer_dc == NULL)
    {
vim_free(lastlf);
lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
if (lastlf != NULL)
mch_memmove(lastlf, lf, sizeof(LOGFONT));
    }

    return OK;
}

#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Something went wrong with that request. Please try again.