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
|
/*
* Buffer management.
* Some of the functions are internal,
* and some are actually attached to user
* keys. Like everyone else, they set hints
* for the display system.
*
* $Id: buffer.c,v 1.376 2025/01/26 17:07:24 tom Exp $
*/
#include "estruct.h"
#include "edef.h"
#define update_on_chg(bp) (!b_is_temporary(bp) || show_all)
static BUFFER *last_bp; /* noautobuffer value */
static BUFFER *this_bp; /* '%' buffer */
static BUFFER *that_bp; /* '#' buffer */
static int show_all; /* true iff we show all buffers */
static int updating_list;
static int bfl_num_1st;
static int bfl_num_end;
static int bfl_buf_1st;
static int bfl_buf_end;
/*--------------------------------------------------------------------------*/
int
buffer_in_use(BUFFER *bp)
{
if (bp->b_inuse) {
mlforce("[Buffer '%s' is in use]", bp->b_bname);
return TRUE;
}
return FALSE;
}
int
buffer_is_visible(BUFFER *bp)
{
if (bp->b_nwnd != 0) { /* Error if on screen. */
mlforce("[Buffer '%s' is being displayed]", bp->b_bname);
return TRUE;
}
return FALSE;
}
int
buffer_is_solo(BUFFER *bp)
{
if ((curbp == bp) && (find_alt() == NULL)) {
mlforce("[Can't kill buffer '%s']", bp->b_bname);
return TRUE;
}
return FALSE;
}
/*
* Returns the buffer-list pointer, if it exists.
*/
static BUFFER *
find_BufferList(void)
{
return find_b_name(BUFFERLIST_BufName);
}
/*
* Look for a buffer-pointer in the list, to see if it has been delinked yet.
*/
BUFFER *
find_bp(BUFFER *bp1)
{
if (bp1 != NULL) {
BUFFER *bp;
for_each_buffer(bp) {
if (bp == bp1)
return bp;
}
}
return NULL;
}
/*
* Returns the total number of buffers in the list.
*/
static int
countBuffers(void)
{
BUFFER *bp;
int count = 0;
for_each_buffer(bp)
count++;
return count;
}
/*
* Returns the n'th buffer created
*/
static BUFFER *
find_nth_created(int n)
{
BUFFER *bp;
for_each_buffer(bp) {
if (bp->b_created == n)
return bp;
}
return NULL;
}
/*
* Returns the n'th buffer used
*/
static BUFFER *
find_nth_used(int n)
{
BUFFER *bp;
for_each_buffer(bp) {
if (bp->b_last_used == n)
return bp;
}
return NULL;
}
/*
* Returns the buffer with the largest value of 'b_last_used'.
*/
static BUFFER *
find_latest(void)
{
BUFFER *bp, *maxbp = NULL;
for_each_buffer(bp) {
if (maxbp == NULL)
maxbp = bp;
else if (maxbp->b_last_used < bp->b_last_used)
maxbp = bp;
}
return maxbp;
}
/*
* Check if the given string contains only pathname-characters.
*/
static int
is_pathchars(const char *fname)
{
int result = TRUE;
int empty = TRUE;
while (*fname != EOS) {
if (!ispath(CharOf(*fname)))
break;
empty = FALSE;
++fname;
}
return result && !empty;
}
/*
* Look for a filename in the buffer-list
*/
BUFFER *
find_b_file(const char *fname)
{
BUFFER *bp;
char nfname[NFILEN];
if (is_pathchars(fname)) {
(void) lengthen_path(vl_strncpy(nfname, fname, sizeof(nfname)));
for_each_buffer(bp) {
if (same_fname(nfname, bp, FALSE))
return bp;
}
}
return NULL;
}
/*
* Look for a specific buffer number
*/
BUFFER *
find_b_hist(int number)
{
BUFFER *bp;
if (number >= 0) {
for_each_buffer(bp) {
if (!b_is_temporary(bp) && (number-- <= 0))
break;
}
} else
bp = NULL;
return bp;
}
/*
* Look for a buffer-number (i.e., one from the buffer-list display)
*/
static BUFFER *
find_b_number(const char *number)
{
int c = 0;
while (isDigit(*number))
c = (c * 10) + (*number++ - '0');
if (!*number)
return find_b_hist(c);
return NULL;
}
/*
* Find buffer, given (possibly) filename, buffer name or buffer number.
*/
static BUFFER *
find_buffer(const char *name)
{
BUFFER *bp;
if ((bp = find_b_name(name)) == NULL /* Try buffer */
&& (bp = find_b_file(name)) == NULL /* ...then filename */
&& (bp = find_b_number(name)) == NULL) { /* ...then number */
return NULL;
}
return bp;
}
/*
* Find buffer, given (possibly) filename, buffer name or buffer number,
* warning if we do not find it.
*/
BUFFER *
find_any_buffer(const char *name)
{
BUFFER *bp;
if ((bp = find_buffer(name)) == NULL)
mlforce("[No such buffer] %s", name);
return bp;
}
/*
* Delete all instances of window pointer for a given buffer pointer
*/
int
zotwp(BUFFER *bp)
{
WINDOW *wp;
BUFFER *nbp;
BUFFER *obp = NULL;
WINDOW dummy;
int s = FALSE;
TRACE((T_CALLED "zotwp(%s)\n", bp ? bp->b_bname : "null"));
if (bp != NULL) {
/*
* Locate buffer to switch to after deleting windows. It can't be
* the same as the buffer which is being deleted and if it's an
* invisible or scratch buffer, it must have been already visible.
* From the set of allowable buffers, choose the most recently
* used.
*/
for_each_buffer(nbp) {
if (nbp != bp && (!b_is_temporary(nbp) || nbp->b_nwnd != 0)) {
if (obp == NULL)
obp = nbp;
else if (obp->b_last_used < nbp->b_last_used)
obp = nbp;
}
}
/* Delete window instances... */
for_each_window(wp) {
if (wp->w_bufp == bp && valid_window(wheadp->w_wndp)) {
dummy.w_wndp = wp->w_wndp;
s = delwp(wp);
wp = &dummy;
}
}
if (valid_buffer(obp))
s = swbuffer(obp);
}
returnCode(s);
}
/*
* Mark a buffer's created member to 0 (unused), adjusting the other buffers
* so that there are no gaps.
*/
static void
MarkDeleted(BUFFER *bp)
{
int created = bp->b_created;
if (created) {
bp->b_created = 0;
for_each_buffer(bp) {
if (bp->b_created > created)
bp->b_created -= 1;
}
}
}
/*
* Mark a buffer's last-used member to 0 (unused), adjusting the other buffers
* so that there are no gaps.
*/
static void
MarkUnused(BUFFER *bp)
{
int used = bp->b_last_used;
if (used) {
bp->b_last_used = 0;
for_each_buffer(bp) {
if (bp->b_last_used > used)
bp->b_last_used -= 1;
}
}
}
/*
* After 'bclear()', frees remaining storage for a buffer.
*/
static void
FreeBuffer(BUFFER *bp)
{
int done = FALSE;
WINDOW *wp;
beginDisplay();
if (bp->b_fname != out_of_mem)
FreeIfNeeded(bp->b_fname);
#if !WINMARK
if (is_header_line(MK, bp)) {
MK.l = NULL;
MK.o = 0;
}
#endif
lfree(buf_head(bp), bp); /* Release header line. */
if (delink_bp(bp) || is_delinked_bp(bp)) {
if (curbp == bp)
curbp = NULL;
#if OPT_HILITEMATCH
clobber_save_curbp(bp);
#endif
#if OPT_MULTIBYTE
FreeIfNeeded(bp->decode_utf_buf);
#endif
#if OPT_PERL || OPT_TCL || OPT_PLUGIN
api_free_private(bp->b_api_private);
#endif
done = TRUE;
}
for_each_window(wp) {
if (wp->w_bufp == bp) {
wp->w_bufp = NULL;
}
}
if (done)
free((char *) bp); /* Release buffer block */
endofDisplay();
}
/*
* Adjust buffer-list's last-used member to account for a new buffer.
*/
static void
TrackAlternate(BUFFER *newbp)
{
BUFFER *bp;
if (!updating_list) {
MarkUnused(newbp);
if (valid_buffer(bp = find_latest())) {
newbp->b_last_used = (bp->b_last_used + 1);
} else { /* shouldn't happen... */
newbp->b_last_used = 1;
}
}
}
/* c is an index (counting only visible/nonscratch) into buffer list */
char *
hist_lookup(int c)
{
BUFFER *bp = find_b_hist(c);
return valid_buffer(bp) ? bp->b_bname : NULL;
}
/*
* Run the $buffer-hook procedure, if it is defined. Note that we must not do
* this if curwp->w_bufp != curbp, since that would break the use of DOT and MK
* throughout the program when performing editing operations.
*/
#if OPT_HOOKS
static void
run_buffer_hook(void)
{
if (!reading_msg_line
&& valid_buffer(curbp)
&& valid_window(curwp)
&& curwp->w_bufp == curbp) {
run_a_hook(&bufhook);
}
}
#else
#define run_buffer_hook() /*EMPTY */
#endif
#define HIST_SHOW 9
/*
* Display the names of the buffers along with their index numbers on the
* minibuffer line. This is normally done in response to "_", i.e.,
* "historical-buffer".
*
* Returns the index of the "#" buffer in the list, so that we can use this in
* histbuff0() to select "#" when "_" is repeated (toggled).
*/
static int
hist_show(int lo_limit, int hi_limit, int cycle)
{
int i;
int first = -1;
char line[(HIST_SHOW + 1) * (NBUFN + 10)];
BUFFER *abp = (BUFFER *) 0;
TRACE((T_CALLED "hist_show(%d..%d) %d\n", lo_limit, hi_limit, cycle));
if (!global_g_val(GMDABUFF)) {
abp = find_alt();
}
(void) strcpy(line, "");
for (i = lo_limit; i <= hi_limit; ++i) {
int j = ((i - lo_limit) + cycle) % (hi_limit + 1 - lo_limit) + lo_limit;
BUFFER *bp = find_b_hist(j);
if (bp != NULL && bp != curbp) {
if (first < 0)
first = j;
if (!global_g_val(GMDABUFF)) {
if (abp && abp == bp)
first = j;
}
(void) lsprintf(line + strlen(line), " %d%s%s %s",
j,
b_is_changed(bp) ? "*" : "",
(abp && abp == bp) ? "#" : "",
bp->b_bname);
}
}
if (first >= 0) {
mlforce("%s", line);
}
returnCode(first);
}
static int
valid_history_index(int hist)
{
BUFFER *bp;
return ((bp = find_b_hist(hist)) != NULL && bp != curbp);
}
/*
* Given a buffer, returns any corresponding WINDOW pointer
*/
WINDOW *
bp2any_wp(BUFFER *bp)
{
WINDOW *wp;
for_each_visible_window(wp) {
if (wp->w_bufp == bp)
break;
}
/*
* Check for special case where we're running scripts before the screen is
* initialized, and want to ensure we do not put buffers into wminip.
*/
if (wp == NULL && !is_vtinit() && wnullp != NULL && wnullp->w_bufp == bp)
wp = wnullp;
return wp;
}
/*
* Worker function for histbuff() and histbuff_to_current_window().
*/
static int
histbuff0(int f, int n, int this_window)
{
int thiskey, c;
BUFFER *bp = NULL;
int cycle = 0;
int first;
char *bufn;
if (f == FALSE) {
int hi_limit = HIST_SHOW;
int lo_limit = 1;
/* verify that we have a list which we can display */
while (!valid_history_index(hi_limit) && hi_limit > 1) {
--hi_limit;
}
if (valid_history_index(0))
lo_limit = 0;
if (lo_limit > hi_limit)
return FALSE;
thiskey = lastkey;
do {
if ((first = hist_show(lo_limit, hi_limit, cycle)) < 0)
return FALSE;
c = keystroke();
mlerase();
if (c == KEY_Tab) {
if (++cycle >= hi_limit)
cycle = 0;
c = -1;
} else if (isBackTab((UINT) c)) {
if (--cycle < 0)
cycle = hi_limit - 1;
c = -1;
} else if (c == thiskey) {
c = first;
} else if (isDigit(c)) {
c = c - '0';
} else {
if (!isreturn(c))
unkeystroke(c);
return FALSE;
}
} while (c < 0);
} else {
c = n;
}
if (c >= 0) {
if ((bufn = hist_lookup(c)) == NULL) {
mlwarn("[No such buffer.]");
return FALSE;
}
/* first assume it is a buffer name, then a file name */
if ((bp = find_b_name(bufn)) == NULL)
return getfile(bufn, TRUE);
} else if (bp == NULL) {
mlwarn("[No alternate buffer]");
return FALSE;
}
return swbuffer_lfl(bp, TRUE, this_window);
}
/*
* Lets the user select a given buffer by its number.
*/
int
histbuff(int f, int n)
{
int rc;
if (!clexec)
vile_is_busy = TRUE; /* kill autocolor */
rc = histbuff0(f, n, FALSE);
if (!clexec)
vile_is_busy = FALSE;
return (rc);
}
/*
* Like histbuff, but puts buffer in current window. (Even if the
* buffer is already onscreen in a different window.)
*/
int
histbuff_to_current_window(int f, int n)
{
int rc;
if (!clexec)
vile_is_busy = TRUE; /* kill autocolor */
rc = histbuff0(f, n, TRUE);
if (!clexec)
vile_is_busy = FALSE;
return (rc);
}
/*
* Returns the alternate-buffer pointer, if any
*/
BUFFER *
find_alt(void)
{
BUFFER *bp;
if (global_g_val(GMDABUFF)) {
BUFFER *any_bp = NULL;
if ((bp = find_bp(curbp)) == NULL)
bp = bheadp;
for (; bp; bp = bp->b_bufp) {
if (bp != curbp) {
/*
* If we allow temporary buffers to be selected as the
* alternate, we get into the situation where we try
* to load the message buffer, which has a NULL filename.
* Note that the 'noautobuffer' case, below, never selects
* a temporary buffer as the alternate buffer.
*/
if (!b_is_temporary(bp))
return bp;
}
}
return any_bp;
} else {
BUFFER *last = NULL, *next = find_latest();
for_each_buffer(bp) {
if ((bp != next)
&& !b_is_temporary(bp)) {
if (last) {
if (last->b_last_used < bp->b_last_used)
last = bp;
} else
last = bp;
}
}
return last;
}
}
/* make '#' buffer in noautobuffer-mode, given filename */
void
imply_alt(char *fname, int copy, int lockfl)
{
static int nested;
BUFFER *bp;
LINE *lp;
BUFFER *savebp;
char nfname[NFILEN];
char *stripped;
TRACE((T_CALLED "imply_alt(fname=%s, copy=%d, lockfl=%d)\n",
NONNULL(fname), copy, lockfl));
if (interrupted()
|| isEmpty(fname)
|| nested)
returnVoid();
if ((stripped = is_appendname(fname)) != NULL)
fname = stripped;
/* if fname is a pipe cmd, it can be arbitrarily long */
vl_strncpy(nfname, fname, sizeof(nfname));
(void) lengthen_path(nfname);
if (global_g_val(GMDIMPLYBUFF)
&& valid_buffer(curbp)
&& curbp->b_fname != NULL
&& !same_fname(nfname, curbp, FALSE)
&& !isInternalName(fname)) {
savebp = curbp;
if ((bp = find_b_file(nfname)) == NULL) {
L_NUM top = -1;
L_NUM now = -1;
if ((bp = make_bp(fname, 0)) == NULL) {
mlforce("[Cannot create buffer]");
} else {
/* fill the buffer */
b_clr_flags(bp, BFINVS | BFCHG);
b_set_flags(bp, BFIMPLY);
bp->b_active = TRUE;
ch_fname(bp, nfname);
make_local_b_val(bp, MDNEWLINE);
#if OPT_MULTIBYTE
COPY_B_VAL(bp, curbp, VAL_BYTEORDER_MARK);
COPY_B_VAL(bp, curbp, VAL_FILE_ENCODING);
#endif
if (valid_window(curwp) && curwp->w_bufp == curbp) {
top = line_no(curbp, curwp->w_line.l);
now = line_no(curbp, DOT.l);
}
if (copy) {
for_each_line(lp, savebp) {
if (addline(bp, lvalue(lp), llength(lp)) != TRUE) {
mlforce("[Copy-buffer failed]");
zotbuf(bp);
bp = NULL;
break;
}
}
if (bp != NULL) {
set_b_val(bp, MDNEWLINE, b_val(savebp, MDNEWLINE));
}
} else {
if (readin(fname, lockfl, bp, FALSE) != TRUE) {
zotbuf(bp);
bp = NULL;
}
}
}
/* setup so that buffer-toggle works as in vi (i.e.,
* the top/current lines of the screen are the same).
*/
if (bp != NULL) {
if (now >= 0) {
for_each_line(lp, bp) {
if (--now == 0) {
bp->b_dot.l = lp;
bp->b_dot.o = curbp->b_dot.o;
break;
}
if (--top == 0) {
bp->b_wline.l = lp;
}
}
}
}
} else if (copy && !b_is_changed(bp)) {
nested++;
if (bp->b_active == TRUE) {
if (bp2readin(bp, TRUE) != TRUE) {
zotbuf(bp);
bp = NULL;
}
}
nested--;
}
DisableHook(&bufhook);
if (bp != NULL) {
make_current(bp);
infer_majormode(bp);
}
make_current(savebp);
EnableHook(&bufhook);
}
returnVoid();
}
/* switch back to the most recent buffer */
/* ARGSUSED */
int
altbuff(int f GCC_UNUSED, int n GCC_UNUSED)
{
BUFFER *bp;
TRACE((T_CALLED "altbuff()\n"));
if ((bp = find_alt()) == NULL) {
mlwarn("[No alternate filename to substitute for #]");
returnCode(FALSE);
} else {
returnCode(swbuffer(bp));
}
}
#if OPT_BNAME_CMPL
static int
qs_bname_cmp(const void *a, const void *b)
{
return strcmp(*(const char *const *) a, (*(const char *const *) b));
}
static void
free_bname_cmpl(char **list)
{
int n;
for (n = 0; list[n] != NULL; ++n) {
free(list[n]);
}
free((char *) list);
}
static char **
init_bname_cmpl(void)
{
size_t used = 0;
size_t count;
char **list = NULL;
BUFFER *bp;
if ((count = (size_t) countBuffers()) != 0) {
beginDisplay();
if ((list = typeallocn(char *, count + 1)) != NULL) {
for_each_buffer(bp) {
list[used] = strmalloc(add_backslashes(bp->b_bname));
if (list[used] == NULL) {
free_bname_cmpl(list);
list = NULL;
break;
}
++used;
}
}
if (list != NULL) {
list[used] = NULL;
qsort(list, used, sizeof(char *), qs_bname_cmp);
}
endofDisplay();
}
return list;
}
int
bname_complete(DONE_ARGS)
{
size_t cpos = *pos;
int status = FALSE;
char **nptr;
kbd_init(); /* nothing to erase */
buf[cpos] = EOS; /* terminate it for us */
if ((nptr = init_bname_cmpl()) != NULL) {
status = kbd_complete(PASS_DONE_ARGS, (const char *) nptr, sizeof(*nptr));
beginDisplay();
free_bname_cmpl(nptr);
endofDisplay();
}
return status;
}
#endif
/*
* Prompt for a buffer name (though we may use a filename - it's not a
* stringent requirement).
*/
int
ask_for_bname(const char *prompt, char *bufn, size_t len)
{
int status;
TRACE((T_CALLED "ask_for_bname\n"));
if (clexec || isnamedcmd) {
#if OPT_BNAME_CMPL
status = kbd_string(prompt, bufn, len,
'\n', KBD_NORMAL | KBD_MAYBEC, bname_complete);
#else
status = mlreply(prompt, bufn, len);
#endif
} else if ((status = screen_to_bname(bufn, len)) != TRUE) {
mlforce("[Nothing selected]");
}
returnCode(status);
}
/*
* Prompt for a buffer name, ignoring if we cannot find it (creating it then).
*/
#define ASK_FOR_NEW_BNAME(prompt, status, bufn, bp) \
bufn[0] = EOS; \
if ((status = ask_for_bname(prompt, bufn, sizeof(bufn))) != TRUE \
&& (status != SORTOFTRUE)) \
returnCode(status); \
if ((bp = find_buffer(bufn)) == NULL) { \
bp = bfind(bufn, 0); \
}
/*
* Prompt for a existing-buffer name, warning if we cannot find it.
*/
#define ASK_FOR_OLD_BNAME(prompt, status, bufn, bp) \
bufn[0] = EOS; \
if ((status = ask_for_bname(prompt, bufn, sizeof(bufn))) != TRUE \
&& (status != SORTOFTRUE)) \
returnCode(status); \
if ((bp = find_any_buffer(bufn)) == NULL) { \
returnCode(FALSE); \
}
/*
* Attach a buffer to a window. The
* values of dot and mark come from the buffer
* if the use count is 0. Otherwise, they come
* from some other window.
*/
/* ARGSUSED */
int
usebuffer(int f GCC_UNUSED, int n GCC_UNUSED)
{
BUFFER *bp;
int s;
char bufn[NBUFN];
TRACE((T_CALLED "usebuffer()\n"));
ASK_FOR_OLD_BNAME("Use buffer: ", s, bufn, bp);
returnCode(swbuffer(bp));
}
/*
* Just like "select-buffer", but we will create the buffer if it does not
* exist.
*/
int
edit_buffer(int f GCC_UNUSED, int n GCC_UNUSED)
{
BUFFER *bp;
int s;
char bufn[NBUFN];
TRACE((T_CALLED "edit_buffer()\n"));
ASK_FOR_NEW_BNAME("Edit buffer: ", s, bufn, bp);
returnCode(swbuffer(bp));
}
#if !SMALLER
/*
* Set the current window to display the given buffer.
*/
int
set_window(int f GCC_UNUSED, int n GCC_UNUSED)
{
BUFFER *bp;
int s;
char bufn[NBUFN];
TRACE((T_CALLED "set_window()\n"));
ASK_FOR_OLD_BNAME("Set window to buffer: ", s, bufn, bp);
returnCode(swbuffer_lfl(bp, TRUE, TRUE));
}
/*
* Open a window for the given buffer.
*/
int
popup_buffer(int f GCC_UNUSED, int n GCC_UNUSED)
{
BUFFER *bp;
int s;
char bufn[NBUFN];
TRACE((T_CALLED "popup_buffer()\n"));
ASK_FOR_OLD_BNAME("Popup buffer: ", s, bufn, bp);
if (bp->b_nwnd == 0)
returnCode(popupbuff(bp));
returnCode(TRUE);
}
/*
* Close all windows which display the given buffer.
*/
int
popdown_buffer(int f GCC_UNUSED, int n GCC_UNUSED)
{
BUFFER *bp;
int s;
char bufn[NBUFN];
TRACE((T_CALLED "popdown_buffer()\n"));
ASK_FOR_OLD_BNAME("Popdown buffer: ", s, bufn, bp);
returnCode(zotwp(bp));
}
#endif /* !SMALLER */
void
set_last_bp(BUFFER *bp)
{
last_bp = bp;
}
/* switch back to the first buffer (i.e., ":rewind") */
/* ARGSUSED */
int
firstbuffer(int f GCC_UNUSED, int n GCC_UNUSED)
{
int s = histbuff(TRUE, 0);
if (!global_g_val(GMDABUFF))
set_last_bp(s ? curbp : NULL);
return s;
}
/* switch to the next buffer in the buffer list */
/* ARGSUSED */
int
nextbuffer(int f GCC_UNUSED, int n GCC_UNUSED)
{
BUFFER *bp;
BUFFER *stopatbp;
int result = FALSE;
TRACE((T_CALLED "nextbuffer()\n"));
if (global_g_val(GMDABUFF)) { /* go backward thru buffer-list */
stopatbp = NULL;
while (stopatbp != bheadp) {
/* get the last buffer in the list */
bp = bheadp;
while (bp != NULL && bp->b_bufp != stopatbp)
bp = bp->b_bufp;
if (bp == NULL)
break;
stopatbp = bp;
/* if that one's invisible, keep trying */
if (!b_is_invisible(bp))
break;
}
/* we're back to the top -- they were all invisible */
result = swbuffer(stopatbp);
} else { /* go forward thru args-list */
if (last_bp == NULL) {
set_last_bp(curbp);
}
if ((bp = last_bp) != NULL) {
for (bp = last_bp->b_bufp; bp; bp = bp->b_bufp) {
if (b_is_argument(bp)) {
break;
}
}
}
if (bp != NULL) {
result = swbuffer(bp);
} else {
mlforce("[No more files to edit]");
result = FALSE;
}
}
returnCode(result);
}
#if !SMALLER
/* switch to the previous buffer in the buffer list */
/* ARGSUSED */
int
prevbuffer(int f GCC_UNUSED, int n GCC_UNUSED)
{
BUFFER *bp; /* eligible buffer to switch to */
BUFFER *stopatbp; /* eligible buffer to switch to */
int result = FALSE;
TRACE((T_CALLED "prevbuffer()\n"));
if (global_g_val(GMDABUFF)) { /* go forward thru buffer-list */
if ((bp = curbp) == NULL)
bp = bheadp;
stopatbp = bp;
while (valid_buffer(bp) && (bp = bp->b_bufp) != NULL) {
/* get the next buffer in the list */
/* if that one's invisible, skip it and try again */
if (!b_is_invisible(bp)) {
stopatbp = bp;
break;
}
}
/* we're back to the top -- they were all invisible */
result = swbuffer(stopatbp);
} else { /* go backward thru args-list */
if (curbp == NULL) {
bp = find_nth_created(1);
} else {
bp = find_nth_created(curbp->b_created - 1);
}
if (bp != NULL) {
result = swbuffer(bp);
} else {
mlforce("[No more files to edit]");
}
}
returnCode(result);
}
#endif /* !SMALLER */
/* bring nbp to the top of the list, where curbp usually lives */
void
make_current(BUFFER *nbp)
{
BUFFER *bp;
#if OPT_PROCEDURES
BUFFER *ocurbp;
ocurbp = curbp;
#endif
TrackAlternate(nbp);
if (!updating_list && global_g_val(GMDABUFF)) {
if (nbp != bheadp) { /* remove nbp from the list */
bp = bheadp;
while (valid_buffer(bp) && bp->b_bufp != nbp)
bp = bp->b_bufp;
if (valid_buffer(bp))
bp->b_bufp = nbp->b_bufp;
/* put it at the head */
nbp->b_bufp = bheadp;
bheadp = nbp;
}
curbp = bheadp;
} else
curbp = nbp;
#if OPT_PROCEDURES
if (curbp != ocurbp) {
run_buffer_hook();
}
#endif
#if OPT_TITLE
set_editor_title();
#endif
}
/* make buffer BP current */
int
swbuffer(BUFFER *bp)
{
TRACE((T_CALLED "swbuffer(bp=%p)\n", (void *) bp));
returnCode(swbuffer_lfl(bp, TRUE, FALSE));
}
#if OPT_MODELINE
#define VI_NAMES "\\(vi\\|vile\\|"PROGRAM_NAME"\\)"
static struct {
int mark;
const char *expr;
regexp *prog;
} mls_patterns[] = {
{
3, "^.*\\s" VI_NAMES ":\\s*se\\(t\\)\\?\\s\\+\\(.*\\):.*$", NULL
},
{
2, "^.*\\s" VI_NAMES ":\\s*\\(.*\\)\\s*$", NULL
},
};
static regexp *
mls_regcomp(int n)
{
regexp *prog = NULL;
if (n >= 0 && n < (int) TABLESIZE(mls_patterns)) {
prog = mls_patterns[n].prog;
if (prog == NULL) {
prog = regcomp(mls_patterns[n].expr,
strlen(mls_patterns[n].expr), TRUE);
mls_patterns[n].prog = prog;
}
}
return prog;
}
#if NO_LEAKS
static void
mls_regfree(int n)
{
if (n >= 0) {
if (n < (int) TABLESIZE(mls_patterns)) {
if (mls_patterns[n].prog != NULL) {
free(mls_patterns[n].prog);
mls_patterns[n].prog = NULL;
}
}
} else {
for (n = 0; n < (int) TABLESIZE(mls_patterns); ++n)
mls_regfree(n);
}
}
#endif
static int
found_modeline(LINE *lp, int *first, int *last)
{
int rc = 0;
unsigned n;
int limit = llength(lp);
if (limit > modeline_limit)
limit = modeline_limit;
for (n = 0; n < TABLESIZE(mls_patterns); ++n) {
regexp *prog = mls_regcomp((int) n);
if (lregexec(prog, lp, 0, limit, FALSE)) {
int j = mls_patterns[n].mark;
*first = (int) (prog->startp[j] - prog->startp[0]);
*last = (int) (prog->endp[j] - prog->startp[0]);
/*
* Real vi may have modes that we do not want to set.
*/
if (prog->endp[1] - prog->startp[1] == 2
&& !strncmp(prog->startp[1], "vi", (size_t) 2))
rc = 2;
else
rc = 1;
}
}
return rc;
}
static void
do_one_modeline(LINE *lp, int vi, int first, int last)
{
TBUFF *data = NULL;
if (lisreal(lp) && first >= 0 && last > first && last <= llength(lp)) {
tb_sappend(&data, "setl ");
/*
* Trim trailing blanks, e.g., in case we are processing a "set dos"
* in a file containing trailing ^M's.
*/
while (last > first && isSpace(lgetc(lp, last - 1)))
--last;
tb_bappend(&data, lvalue(lp) + first, (size_t) last - (size_t) first);
tb_append(&data, EOS);
in_modeline = vi;
TPRINTF(("modeline %s:%s\n",
((vi > 1) ? "vi" : PROGRAM_NAME),
tb_values(data)));
docmd(tb_values(data), TRUE, FALSE, 1);
in_modeline = FALSE;
tb_free(&data);
}
}
void
do_modelines(BUFFER *bp)
{
WINDOW *wp;
if (b_val(bp, MDMODELINE) &&
b_val(bp, VAL_MODELINES) > 0 &&
(wp = bp2any_wp(bp)) != NULL) {
LINE *lp;
int once = b_val(bp, VAL_MODELINES);
int limit = 2 * once;
int count;
int rc, first = 0, last = 0;
BUFFER *save_bp = curbp;
WINDOW *save_wp = curwp;
/*
* If we're sourcing a file, then curbp is not necessarily the same.
* Fix that.
*/
curwp = wp;
curbp = bp;
bsizes(bp);
TRACE((T_CALLED "do_modelines(%s) limit %d, size %d\n",
bp->b_bname, limit, bp->b_linecount));
count = 0;
for (lp = lforw(buf_head(bp)); count < once; lp = lforw(lp)) {
--limit;
++count;
if ((rc = found_modeline(lp, &first, &last)) != 0) {
do_one_modeline(lp, rc, first, last);
}
}
count = 0;
if (bp->b_linecount < limit) {
once = bp->b_linecount - once;
}
for (lp = lback(buf_head(bp)); count < once; lp = lback(lp)) {
++count;
if ((rc = found_modeline(lp, &first, &last)) != 0) {
do_one_modeline(lp, rc, first, last);
}
}
curbp = save_bp;
curwp = save_wp;
returnVoid();
}
}
#endif
static int
suckitin(BUFFER *bp, int copy, int lockfl)
{
int s = FALSE;
TRACE((T_CALLED "suckitin(%s, %s)\n", bp->b_bname, copy ? "copy" : "new"));
if (copy) {
WINDOW *wp;
for_each_window(wp) {
if (wp->w_bufp == bp && (is_visible_window(wp) || bp->b_active
!= TRUE))
copy_traits(&(wp->w_traits), &(bp->b_wtraits));
}
}
curwp->w_flag |= WFMODE | WFHARD; /* Quite nasty. */
if (bp->b_active != TRUE) { /* buffer not active yet */
s = bp2readin(bp, lockfl); /* read and activate it */
}
#ifdef MDCHK_MODTIME
else
s = check_file_changed(bp, bp->b_fname);
#endif
updatelistbuffers();
run_buffer_hook();
returnCode(s);
}
/*
* Make given buffer 'bp' current'.
*/
int
swbuffer_lfl(BUFFER *bp, int lockfl, int this_window)
{
int status = TRUE;
WINDOW *wp;
TRACE((T_CALLED
"swbuffer_lfl(bp=%p, lockfl=%d, this_window=%d) bname='%s'\n",
(void *) bp, lockfl, this_window, bp->b_bname));
if (!bp) {
mlforce("BUG: swbuffer passed null bp");
status = FALSE;
} else {
TRACE(("swbuffer(%s) nwnd=%d\n", bp->b_bname, bp->b_nwnd));
if (curbp == bp
&& valid_window(curwp)
&& DOT.l != NULL
&& curwp->w_bufp == bp) { /* no switching to be done */
if (!b_is_reading(bp) &&
!bp->b_active) { /* on second thought, yes there is */
status = suckitin(bp, TRUE, lockfl);
}
} else {
#if !WINMARK
/* Whatever else we do, make sure MK isn't bogus when we leave */
MK = nullmark;
#endif
if (valid_buffer(curbp)) {
/* if we'll have to take over this window, and it's the last */
if ((this_window || bp->b_nwnd == 0)
&& curbp->b_nwnd != 0 && --(curbp->b_nwnd) == 0) {
#if !SMALLER
if (!this_window)
#endif
undispbuff(curbp, curwp);
} else if (DOT.l != NULL && (this_window || bp->b_nwnd == 0)) {
/* Window is still getting taken over so make a
copy of the traits for a possible future
set-window */
copy_traits(&(curbp->b_wtraits), &(curwp->w_traits));
}
}
/* don't let make_current() call the hook -- there's
more to be done down below */
DisableHook(&bufhook);
make_current(bp); /* sets curbp */
EnableHook(&bufhook);
bp = curbp; /* if running the bufhook caused an error,
we may be in a different buffer than we
thought we were going to */
if (bp == NULL) {
mlforce("BUG: make_current set null bp");
status = FALSE;
}
#if !SMALLER
else if (this_window) {
/*
* Put buffer in this window even if it's already on the screen
*/
LINE *lp;
int trait_matches;
if (curwp == NULL || curwp->w_bufp == NULL)
returnCode(FALSE);
else if (curwp->w_bufp == bp)
returnCode(TRUE);
if (curwp->w_bufp->b_nwnd == 0) {
undispbuff(curwp->w_bufp, curwp);
} else {
copy_traits(&(curwp->w_bufp->b_wtraits),
&(curwp->w_traits));
}
/*
* Initialize the window using the saved buffer traits if
* possible. If they don't pass a sanity check, simply
* initialize the newest window to be a clone of any other
* window on the buffer, or initialize it as we do in
* popupbuff().
*/
trait_matches = 0;
#if WINMARK
#define TRAIT_MATCHES_NEEDED 5
#else
#define TRAIT_MATCHES_NEEDED 4
#endif
for_each_line(lp, bp) {
if (lp == bp->b_dot.l && llength(lp) > bp->b_dot.o)
trait_matches++;
#if WINMARK
if (lp == bp->b_mark.l && llength(lp) > bp->b_mark.o)
trait_matches++;
#endif
if (lp == bp->b_lastdot.l && llength(lp) > bp->b_lastdot.o)
trait_matches++;
if (lp == bp->b_tentative_lastdot.l && llength(lp) > bp->b_tentative_lastdot.o)
trait_matches++;
if (lp == bp->b_wline.l && llength(lp) > bp->b_wline.o)
trait_matches++;
if (trait_matches == TRAIT_MATCHES_NEEDED)
break;
}
if (trait_matches == TRAIT_MATCHES_NEEDED)
copy_traits(&(curwp->w_traits), &(bp->b_wtraits));
else if ((wp = bp2any_wp(bp)) == NULL)
init_window(curwp, bp);
else
clone_window(curwp, wp);
curwp->w_bufp = bp;
status = suckitin(bp, (bp->b_nwnd++ == 0), lockfl);
} else
#endif /* !SMALLER */
/* get it already on the screen if possible */
if (bp->b_nwnd != 0) {
/* then it's on the screen somewhere */
if ((wp = bp2any_wp(bp)) == NULL)
mlforce("BUG: swbuffer: wp still NULL");
curwp = wp;
upmode();
#ifdef MDCHK_MODTIME
(void) check_file_changed(bp, bp->b_fname);
#endif
#if OPT_UPBUFF
if (bp != find_BufferList())
updatelistbuffers();
#endif
run_buffer_hook();
status = (find_bp(bp) != NULL);
} else if (curwp == NULL) {
status = FALSE; /* we haven't started displaying yet */
} else {
if (!is_vtinit()) {
curwp = wnullp;
}
/* oh well, suck it into this window */
curwp->w_bufp = bp;
status = suckitin(bp, (bp->b_nwnd++ == 0), lockfl);
}
}
#if OPT_TITLE
set_editor_title();
#endif
}
if (status == TRUE)
set_last_bp(bp);
returnCode(status);
}
#if VILE_NEEDED
/* check to ensure any buffer that thinks it's displayed _is_ displayed */
void
buf_win_sanity(void)
{
BUFFER *bp;
for_each_buffer(bp) {
if (bp->b_nwnd != 0) { /* then it's on the screen somewhere */
WINDOW *wp = bp2any_wp(bp);
if (!wp) {
dbgwrite("BUG: swbuffer 1: wp is NULL");
}
}
}
}
#endif
int
eql_bname(BUFFER *bp, const char *name)
{
return (cs_strcmp(global_g_val(GMDFILENAME_IC), bp->b_bname, name) == 0);
}
void
undispbuff(BUFFER *bp, WINDOW *wp)
{
/* get rid of it completely if it's a scratch buffer,
or it's empty and unmodified */
if (b_is_scratch(bp)) {
/* Save the location within the help-file */
if (eql_bname(bp, HELP_BufName))
help_at = line_no(bp, wp->w_dot.l);
(void) zotbuf(bp);
} else if (global_g_val(GMDABUFF) &&
!b_is_argument(bp) &&
!b_is_changed(bp) &&
is_empty_buf(bp)
&& !ffexists(bp->b_fname)) {
(void) zotbuf(bp);
} else { /* otherwise just adjust it off the screen */
copy_traits(&(bp->b_wtraits), &(wp->w_traits));
}
}
#if !OPT_MAJORMODE
/* return true iff c-mode is active for this buffer */
static int
cmode_active(BUFFER *bp)
{
if (is_local_b_val(bp, MDCMOD))
return is_c_mode(bp);
else
return (is_c_mode(bp) && has_C_suffix(bp));
}
#endif
/* return the correct tabstop setting for this buffer */
int
tabstop_val(BUFFER *bp)
{
#if OPT_MAJORMODE
int i = b_val(bp, VAL_TAB);
#else
int i = b_val(bp, (cmode_active(bp) ? VAL_C_TAB : VAL_TAB));
#endif
if (i == 0)
return 1;
return i;
}
/* return the correct shiftwidth setting for this buffer */
int
shiftwid_val(BUFFER *bp)
{
#if OPT_MAJORMODE
int i = b_val(bp, VAL_SWIDTH);
#else
int i = b_val(bp, (cmode_active(bp) ? VAL_C_SWIDTH : VAL_SWIDTH));
#endif
if (i == 0)
return 1;
return i;
}
#if !OPT_MAJORMODE
int
has_C_suffix(BUFFER *bp)
{
int s;
s = nregexec(global_g_val_rexp(GVAL_CSUFFIXES)->reg,
bp->b_fname, (char *) 0, 0, -1,
global_g_val(GMDFILENAME_IC));
return s;
}
#endif
int
kill_that_buffer(BUFFER *bp)
{
int s;
char bufn[NFILEN];
/* ...for info-message */
(void) vl_strncpy(bufn, bp->b_bname, sizeof(bufn));
if (buffer_is_solo(bp)) {
return FALSE;
}
if (bp->b_nwnd != 0) { /* then it's on the screen somewhere */
(void) zotwp(bp);
if (find_bp(bp) == NULL) { /* delwp must have zotted us */
return FALSE;
}
}
if ((s = zotbuf(bp)) == TRUE)
mlwrite("Buffer %s gone", bufn);
return s;
}
static void
free_buffer_list(char **list)
{
int n;
for (n = 0; list[n] != NULL; ++n) {
free(list[n]);
}
free(list);
}
/*
* Make a list of buffer names to process.
*/
static char **
make_buffer_list(char *bufn)
{
BUFFER *bp;
int count = 0;
int limit = countBuffers();
char **result = typecallocn(char *, (size_t) limit + 1);
if (result != NULL) {
#if OPT_FORBUFFERS_CHOICES
#if OPT_GLOB_RANGE
/*
* If it looks like a scratch buffer, try to find exactly that before
* possibly getting confused with range expressions, etc.
*/
if (global_g_val(GVAL_FOR_BUFFERS) == FB_MIXED
&& is_scratchname(bufn)
&& (bp = find_b_name(bufn)) != NULL) {
result[count++] = strmalloc(bp->b_bname);
} else
#endif
if (global_g_val(GVAL_FOR_BUFFERS) == FB_MIXED
|| global_g_val(GVAL_FOR_BUFFERS) == FB_GLOB) {
for_each_buffer(bp) {
if (glob_match_leaf(bp->b_bname, bufn)) {
result[count++] = strmalloc(bp->b_bname);
}
}
} else if (global_g_val(GVAL_FOR_BUFFERS) == FB_REGEX) {
regexp *exp;
if ((exp = regcomp(bufn, strlen(bufn), TRUE)) != NULL) {
for_each_buffer(bp) {
if (nregexec(exp, bp->b_bname, (char *) 0, 0, -1, FALSE)) {
result[count++] = strmalloc(bp->b_bname);
}
}
free(exp);
}
}
if (count == 0) {
if ((bp = find_buffer(bufn)) != NULL) { /* Try buffer */
result[count++] = strmalloc(bp->b_bname);
}
}
#else
if ((bp = find_buffer(bufn)) != 0) { /* Try buffer */
result[count++] = strmalloc(bp->b_bname);
}
#endif
if (count == 0) {
free_buffer_list(result);
result = NULL;
}
}
return result;
}
#if !SMALLER
/*
* Select a set of buffers with the first parameter, and prompt for a command
* to execute on each buffer.
*/
int
for_buffers(int f, int n)
{
BUFFER *bp;
int s;
char bufn[NFILEN];
char command[NSTRING];
char **list;
int inlist;
int updated = 0;
bufn[0] = EOS;
if ((s = ask_for_bname("Buffers: ", bufn, sizeof(bufn))) != TRUE)
return s;
if ((list = make_buffer_list(bufn)) != NULL) {
*command = EOS;
/*
* Don't do command-completion, since we want to allow range
* expressions, e.g.,
* :fb *.h %trim
*
* FIXME: split-out logic in execute_named_command() to allow proper
* parsing to linespec and command-verb.
*/
if ((s = kbd_string("Command: ",
command,
sizeof(command),
'\n',
KBD_QUOTES,
no_completion)) == TRUE) {
char *save_execstr = execstr;
int save_clexec = clexec;
for (inlist = 0; list[inlist] != NULL; ++inlist) {
if ((bp = find_b_name(list[inlist])) != NULL
&& swbuffer(bp)) {
char this_command[NSTRING];
/*
* FIXME: does namedcmd() modify what execstr points to?
*/
execstr = vl_strncpy(this_command, command, sizeof(this_command));
clexec = TRUE;
if (namedcmd(f, n))
++updated;
}
}
execstr = save_execstr;
clexec = save_clexec;
}
free_buffer_list(list);
} else {
mlforce("[No matching buffers]");
}
if (updated) {
mlforce("[Processed %d buffer%s]", updated, PLURAL(updated));
s = TRUE;
}
return s;
}
#endif /* !SMALLER */
/*
* Dispose of a buffer, by name. If this is a screen-command, pick the name up
* from the screen. Otherwise, ask for the name. Look it up (don't get too
* upset if it isn't there at all!). Get quite upset if the buffer is being
* displayed. Clear the buffer (ask if the buffer has been changed). Then
* free the header line and the buffer header.
*
* If we are given a repeat-count, try to kill that many buffers. Killing from
* names selected from the buffer-list is a special case, because the cursor
* may be pointing to the buffer-number column. In that case, we must
* recompute the buffer-contents. Otherwise, move the cursor down one line
* (staying in the same column), so we can pick up the names from successive
* lines.
*/
int
killbuffer(int f, int n)
{
BUFFER *bp;
int s;
char bufn[NFILEN];
char **list;
int inlist;
int removed = 0;
#if OPT_UPBUFF
C_NUM save_COL;
MARK save_DOT;
MARK save_TOP;
int animated = (f
&& (n > 1)
&& valid_window(curwp)
&& valid_buffer(curbp)
&& (curbp == find_BufferList()));
/*
* Handle special case if we're pointing in the buffer list to either the
* column containing current buffer number, or buffer names. Since we'll
* remove the line containing that information as the buffer-list is
* updated, we don't want to move down a line at the same time.
*/
int special = animated && ((DOT.o >= bfl_num_1st && DOT.o < bfl_num_end) ||
(DOT.o >= bfl_buf_1st && DOT.o < bfl_buf_end));
if (animated && !special) {
save_COL = getccol(FALSE);
save_DOT = DOT;
save_TOP = curwp->w_line;
} else {
save_COL = 0; /* appease gcc */
save_DOT = nullmark;
save_TOP = nullmark;
}
#endif
n = need_a_count(f, n, 1);
for_ever {
bufn[0] = EOS;
if ((s = ask_for_bname("Kill buffer: ", bufn, sizeof(bufn))) != TRUE)
break;
if ((list = make_buffer_list(bufn)) != NULL) {
for (inlist = 0; list[inlist] != NULL; ++inlist) {
if ((bp = find_b_name(list[inlist])) != NULL
&& kill_that_buffer(bp)) {
++removed;
}
}
free_buffer_list(list);
}
if (--n > 0) {
#if OPT_UPBUFF
if (special)
(void) update(TRUE);
else
#endif
if (!forwline(FALSE, 1))
break;
} else
break;
}
if (removed) {
mlforce("[Removed %d buffer%s]", removed, PLURAL(removed));
s = TRUE;
}
#if OPT_UPBUFF
if (animated && !special) {
curgoal = save_COL;
DOT = save_DOT;
DOT.o = getgoal(DOT.l);
curwp->w_line = save_TOP;
clr_typed_flags(curwp->w_flag, USHORT, WFMOVE);
}
#endif
return s;
}
/*
* Unlink a buffer from the list, adjusting last-used and created counts.
*/
int
delink_bp(BUFFER *bp)
{
BUFFER *bp1, *bp2;
bp1 = NULL; /* Find the header. */
bp2 = bheadp;
while (bp2 != bp) {
bp1 = bp2;
bp2 = bp2->b_bufp;
if (bp2 == NULL)
return FALSE;
}
bp2 = bp2->b_bufp; /* Next one in chain. */
if (bp1 == NULL) /* Unlink it. */
bheadp = bp2;
else
bp1->b_bufp = bp2;
MarkUnused(bp);
MarkDeleted(bp);
if (bp == last_bp)
last_bp = NULL;
return TRUE;
}
char *
strip_brackets(char *dst, const char *src)
{
size_t len;
if (*src == SCRTCH_LEFT[0])
src++;
(void) strcpy(dst, src);
if ((len = strlen(dst)) != 0
&& dst[len - 1] == SCRTCH_RIGHT[0]) {
dst[--len] = EOS;
}
return dst;
}
char *
add_brackets(char *dst, const char *src)
{
if (dst != NULL && src != NULL) {
size_t len = strlen(src);
if (len > NBUFN - 3)
len = NBUFN - 3;
dst[0] = SCRTCH_LEFT[0];
(void) strncpy(&dst[1], src, len);
(void) strcpy(&dst[len + 1], SCRTCH_RIGHT);
}
return dst;
}
/* kill the buffer pointed to by bp */
int
zotbuf(BUFFER *bp)
{
int status;
TRACE((T_CALLED "zotbuf(bp=%p)\n", (void *) bp));
if (find_bp(bp) == NULL) /* delwp may have zotted us, pointer obsolete */
returnCode(TRUE);
TRACE(("zotbuf(%s)\n", bp->b_bname));
if (buffer_in_use(bp))
returnCode(FALSE);
if (buffer_is_visible(bp)) /* Error if on screen. */
returnCode(FALSE);
if (is_fake_window(wheadp)) {
WINDOW *wp;
WINDOW dummy;
/* Not on screen, but a fake window might refer to it. So
delete all such fake windows */
for_each_window(wp) {
if (is_fake_window(wp)
&& wp->w_bufp == bp && valid_window(wheadp->w_wndp)) {
dummy.w_wndp = wp->w_wndp;
(void) delwp(wp);
wp = &dummy;
}
}
}
#if OPT_LCKFILES
/* If Buffer is killed and not locked by other then release own lock */
if (global_g_val(GMDUSEFILELOCK)) {
if (bp->b_active)
if (!b_val(curbp, MDLOCKED) && !b_val(curbp, MDVIEW))
release_lock(bp->b_fname);
}
#endif
/* Blow text away. */
if ((status = bclear(bp)) == TRUE) {
#if OPT_NAMEBST
if (tb_values(bp->b_procname) != NULL) {
delete_namebst(tb_values(bp->b_procname), TRUE, FALSE, 0);
tb_free(&(bp->b_procname));
}
#endif
FreeBuffer(bp);
updatelistbuffers();
}
returnCode(status);
}
BUFFER *
zotbuf2(BUFFER *bp)
{
if (zotbuf(bp))
bp = NULL;
return bp;
}
/* Rename a buffer given a buffer pointer and new buffer name. */
int
renamebuffer(BUFFER *rbp, char *bufname)
{
BUFFER *bp; /* pointer to scan through all buffers */
char bufn[NBUFN]; /* buffer to hold buffer name */
WINDOW *wp;
TRACE((T_CALLED "renamebuffer(%s, %s)\n", rbp->b_bname, bufname));
if (*mktrimmed(vl_strncpy(bufn, bufname, sizeof(bufn))) == EOS)
returnCode(ABORT);
bp = find_b_name(bufn);
if (bp == curbp)
returnCode(ABORT); /* no change */
if (valid_buffer(bp))
returnCode(FALSE); /* name already in use */
#if OPT_NAMEBST
if (is_scratchname(rbp->b_bname)) {
char procname[NBUFN];
(void) strip_brackets(procname, rbp->b_bname);
if (search_namebst(procname, 0)
&& rename_namebst(procname, bufn, 0) != TRUE)
returnCode(ABORT);
}
#endif
set_bname(rbp, bufn); /* copy buffer name to structure */
for_each_visible_window(wp) {
if (wp->w_bufp == rbp)
wp->w_flag |= WFMODE;
}
returnCode(TRUE);
}
static int
rename_specific_buffer(BUFFER *bp)
{
static char bufn[NBUFN]; /* buffer to hold buffer name */
const char *prompt = "New name for buffer: ";
int status;
/* prompt for and get the new buffer name */
do {
if (mlreply(prompt, bufn, (UINT) sizeof(bufn)) != TRUE)
return (FALSE);
prompt = "That name's been used. New name: ";
status = renamebuffer(bp, bufn);
if (status == ABORT)
return (FALSE);
} while (status == FALSE);
b_clr_scratch(bp); /* if renamed, we must want it */
mlerase();
updatelistbuffers();
return (TRUE);
}
/* Rename the current buffer */
/* ARGSUSED */
int
namebuffer(int f GCC_UNUSED, int n GCC_UNUSED)
{
TRACE((T_CALLED "namebuffer()\n"));
returnCode(rename_specific_buffer(curbp));
}
/*
* Rename a buffer, may be the current one.
*/
int
name_a_buffer(int f GCC_UNUSED, int n GCC_UNUSED)
{
BUFFER *bp;
int s;
char bufn[NBUFN];
TRACE((T_CALLED "name_a_buffer()\n"));
ASK_FOR_OLD_BNAME("Rename buffer: ", s, bufn, bp);
returnCode(rename_specific_buffer(bp));
}
/* create or find a window, and stick this buffer in it. when
done, we own the window and the buffer, but they are _not_
necessarily curwp and curbp */
int
popupbuff(BUFFER *bp)
{
WINDOW *wp;
TRACE((T_CALLED "popupbuff(%s) nwnd=%d\n", bp->b_bname, bp->b_nwnd));
if (!valid_buffer(curbp)) {
curbp = bp; /* possibly at startup time */
curwp->w_bufp = curbp;
++curbp->b_nwnd;
} else if (bp->b_nwnd == 0) { /* Not on screen yet. */
if ((wp = wpopup()) == NULL)
return FALSE;
if (--wp->w_bufp->b_nwnd == 0)
undispbuff(wp->w_bufp, wp);
wp->w_bufp = bp;
++bp->b_nwnd;
}
for_each_window(wp) {
if (wp->w_bufp == bp) {
init_window(wp, bp);
}
}
returnCode(swbuffer(bp));
}
/*
* Invoked after changing mode 'autobuffer', this relinks the list of buffers
* sorted according to the mode: by creation or last-used order.
*/
void
sortlistbuffers(void)
{
BUFFER *bp, *newhead = NULL;
int c;
if (global_g_val(GMDABUFF)) {
c = 1;
while (valid_buffer(bp = find_nth_used(c++))) {
bp->b_relink = newhead;
newhead = bp;
}
} else {
c = countBuffers();
while (valid_buffer(bp = find_nth_created(c--))) {
bp->b_relink = newhead;
newhead = bp;
}
}
for (bp = newhead; bp; bp = bp->b_relink)
bp->b_bufp = bp->b_relink;
bheadp = newhead;
updatelistbuffers();
}
/*
* List all of the active buffers. First update the special buffer that holds
* the list. Next make sure at least 1 window is displaying the buffer list,
* splitting the screen if this is what it takes. Lastly, repaint all of the
* windows that are displaying the list. A numeric argument causes it to
* toggle listing invisible buffers; a subsequent argument forces it to a
* normal listing.
*/
int
togglelistbuffers(int f, int n)
{
int status;
BUFFER *bp;
/* if it doesn't exist, create it */
if ((bp = find_BufferList()) == NULL) {
status = listbuffers(f, n);
/* if user supplied argument, re-create */
} else if (f) {
status = listbuffers(!show_all, n);
} else {
/* if it does exist, delete the window, which in turn
will kill the BFSCRTCH buffer */
status = zotwp(bp);
}
return status;
}
/*
* Return the status flags ordered so the most important one for [Buffer List]
* is first. We use the whole string in $bflags though.
*/
void
buffer_flags(char *dst, BUFFER *bp)
{
if (valid_buffer(bp)) {
if (b_is_directory(bp))
*dst++ = ('d');
if (b_is_scratch(bp))
*dst++ = ('s');
if (!(bp->b_active))
*dst++ = ('u');
if (b_is_implied(bp))
*dst++ = ('a');
if (b_is_invisible(bp))
*dst++ = ('i');
if (b_is_changed(bp))
*dst++ = ('m');
}
*dst = EOS;
}
/*
* Track/emit footnotes for 'makebufflist()', showing only the ones we use.
*/
#if !SMALLER
static void
footnote(int c)
{
/* *INDENT-OFF* */
static struct {
const char *name;
int flag;
} table[] = {
{ "automatic", 0 },
{ "directory", 0 },
{ "invisible", 0 },
{ "modified", 0 },
{ "scratch", 0 },
{ "unread", 0 },
};
/* *INDENT-ON* */
size_t j, next;
for (j = next = 0; j < TABLESIZE(table); j++) {
if (c != 0) {
if (table[j].name[0] == c) {
bputc(c);
table[j].flag = TRUE;
break;
}
} else if (table[j].flag) {
bprintf("%s '%c'%s %s",
next ? "," : "notes:", table[j].name[0],
next ? "" : " is", table[j].name);
next++;
table[j].flag = 0;
}
}
if (next)
bputc('\n');
}
#define MakeNote(c) footnote(c)
#define ShowNotes() footnote(0)
#else
#define MakeNote(c) bputc(c)
#define ShowNotes()
#endif /* !SMALLER */
/*
* This routine rebuilds the text in the buffer that holds the buffer list. It
* is called by the list buffers command. Return TRUE if everything works.
* Return FALSE if there is an error (if there is no memory). The variable
* 'show_all' (set if the command had a repeat-count) indicates whether
* hidden buffers should be on the list.
*/
/* ARGSUSED */
static void
makebufflist(int unused GCC_UNUSED, void *dummy GCC_UNUSED)
{
BUFFER *bp;
LINE *curlp; /* entry corresponding to buffer-list */
int nbuf = 0; /* no. of buffers */
int this_or_that;
char temp[NFILEN];
curlp = NULL;
bfl_num_1st = -1;
bfl_num_end = -1;
bfl_buf_1st = -1;
bfl_buf_end = -1;
if (this_bp == NULL)
this_bp = curbp;
if (this_bp == that_bp)
that_bp = find_alt();
bprintf(" %7s %*s %s\n", "Size", NBUFN - 1, "Buffer name",
"Contents");
bpadc(' ', 6);
bpadc('-', 7);
bputc(' ');
bpadc('-', NBUFN - 1);
bputc(' ');
bpadc('-', term.cols - DOT.o);
bputc('\n');
/* output the list of buffers */
for_each_buffer(bp) {
/* skip those buffers which don't get updated when changed */
#if OPT_UPBUFF
if (!update_on_chg(bp)) {
continue;
}
#endif
/* output status flag (e.g., has the file been read in?) */
buffer_flags(temp, bp);
if (*temp != EOS)
MakeNote(*temp);
else
bputc(' ');
this_or_that = (bp == this_bp)
? EXPC_THIS
: (bp == that_bp)
? EXPC_THAT
: ' ';
if (b_is_temporary(bp)) {
bprintf(" %c ", this_or_that);
} else {
char bufnum[NSTRING];
bfl_num_1st = DOT.o;
sprintf(bufnum, "%3d", nbuf++);
bputsn_xcolor(bufnum, -1, XCOLOR_NUMBER);
bfl_num_end = DOT.o;
bprintf("%c ", this_or_that);
}
(void) bsizes(bp);
bprintf("%7lu ", bp->b_bytecount);
bfl_buf_1st = DOT.o;
bprintf("%.*s ", NBUFN - 1, bp->b_bname);
bfl_buf_end = DOT.o;
{
char *p;
if ((p = bp->b_fname) != NULL)
p = shorten_path(vl_strncpy(temp, p, sizeof(temp)), TRUE);
if (p != NULL)
bprintf("%s", p);
}
bprintf("\n");
if (bp == curbp)
curlp = lback(lback(buf_head(curbp)));
}
ShowNotes();
bprintf(" %*s %s", NBUFN - 1, "Current dir:",
current_directory(FALSE));
/* show the actual size of the buffer-list */
if (curlp != NULL) {
(void) bsizes(curbp);
(void) lsprintf(temp, "%7lu", curbp->b_bytecount);
(void) memcpy(lvalue(curlp) + 6, temp, strlen(temp));
}
}
#if OPT_UPBUFF
/*
* (Re)compute the contents of the buffer-list. Use the flag 'updating_list'
* as a semaphore to avoid adjusting the last used/created indices while
* cycling over the list of buffers.
*/
/*ARGSUSED*/
static int
show_BufferList(BUFFER *bp GCC_UNUSED)
{
int status;
if ((status = ((!updating_list++) != 0)) != FALSE) {
this_bp = curbp;
that_bp = find_alt();
status = liststuff(BUFFERLIST_BufName, FALSE,
makebufflist, 0, (void *) 0);
}
updating_list--;
return status;
}
/*
* If the list-buffers window is visible, update it after operations that
* would modify the list.
*/
void
updatelistbuffers(void)
{
show_mark_is_set(0);
update_scratch(BUFFERLIST_BufName, show_BufferList);
}
/* mark a scratch/temporary buffer for update */
void
update_scratch(const char *name, UpBuffFunc func)
{
BUFFER *bp = find_b_name(name);
if (valid_buffer(bp)) {
bp->b_upbuff = func;
b_set_obsolete(bp);
}
}
#endif /* OPT_UPBUFF */
/* ARGSUSED */
int
listbuffers(int f GCC_UNUSED, int n GCC_UNUSED)
{
int status;
#if OPT_UPBUFF
show_all = f; /* save this to use in automatic updating */
if (find_BufferList() != NULL) {
updatelistbuffers();
return TRUE;
}
this_bp = NULL;
that_bp = curbp;
status = liststuff(BUFFERLIST_BufName, FALSE,
makebufflist, 0, (void *) 0);
b_clr_obsolete(curbp);
#else
show_all = f;
this_bp = 0;
that_bp = curbp;
status = liststuff(BUFFERLIST_BufName, FALSE,
makebufflist, 0, (void *) 0);
#endif
return status;
}
/*
* This is ": args"
*/
int
vl_set_args(int f, int n)
{
BUFFER *bp;
if (end_named_cmd()) {
return listbuffers(f, n);
}
if (any_changed_buf(&bp) != 0) {
mlforce("Buffer %s is modified. Use :args! to override",
bp->b_bname);
return FALSE;
}
return set_files_to_edit("Set arguments: ", FALSE);
}
/*
* This is ": args!"
*/
int
vl_set_args2(int f, int n)
{
if (end_named_cmd()) {
return listbuffers(f, n);
}
return set_files_to_edit("Set arguments: ", FALSE);
}
/*
* The argument "text" points to a string. Append this line to the buffer.
* Handcraft the EOL on the end. Return TRUE if it worked and FALSE if you ran
* out of room.
*/
int
addline(BUFFER *bp, const char *text, int len)
{
int status = FALSE;
if (add_line_at(bp, lback(buf_head(bp)), text, len) == TRUE) {
/* If "." is at the end, move it to new line */
if (sameline(bp->b_dot, bp->b_line))
bp->b_dot.l = lback(buf_head(bp));
status = TRUE;
}
return status;
}
/*
* Add a LINE filled with the given text after the specified LINE.
*/
int
add_line_at(BUFFER *bp, LINE *prevp, const char *text, int len)
{
LINE *newlp;
LINE *nextp;
LINE *lp;
int ntext;
int result = FALSE;
if (len < 0) {
if (text != NULL) {
result = add_line_at(bp, prevp, text, (int) strlen(text));
} else {
result = add_line_at(bp, prevp, "", 0);
}
} else {
nextp = lforw(prevp);
ntext = len;
newlp = lalloc(ntext, bp);
if (newlp != NULL) {
lp = newlp;
if (ntext > 0)
(void) memcpy(lvalue(lp), text, (size_t) ntext);
/* try to maintain byte/line counts? */
if (b_is_counted(bp)) {
if (nextp == buf_head(bp)) {
set_local_b_val(bp, MDNEWLINE, TRUE);
bp->b_bytecount += (B_COUNT) (ntext + 1);
bp->b_linecount += 1;
#if !SMALLER /* tradeoff between codesize & data */
lp->l_number = bp->b_linecount;
#endif
} else
b_clr_counted(bp);
}
set_lforw(prevp, newlp); /* link into the buffer */
set_lback(newlp, prevp);
set_lback(nextp, newlp);
set_lforw(newlp, nextp);
result = TRUE;
}
}
return result;
}
/*
* return lines of a named buffer in sequence, starting at DOT
*/
char *
next_buffer_line(const char *bname)
{
WINDOW *wp = NULL;
BUFFER *bp;
B_COUNT blen;
static TBUFF *lbuf;
if ((bp = find_b_name(bname)) == NULL)
return NULL;
/* if the buffer is in a window, the buffer DOT
* is the wrong one to use. use the first window's DOT.
*/
if (bp->b_nwnd != 0) {
for_each_visible_window(wp) {
if (wp->w_bufp == bp) {
bp->b_dot = wp->w_dot;
break;
}
}
}
/* out of text? */
if (is_header_line(bp->b_dot, bp))
return NULL;
/* how much left on the current line? */
blen = (B_COUNT) llength(bp->b_dot.l);
if (blen > (B_COUNT) bp->b_dot.o)
blen -= (B_COUNT) bp->b_dot.o;
else
blen = 0;
tb_init(&lbuf, EOS);
tb_bappend(&lbuf,
lvalue(bp->b_dot.l) + bp->b_dot.o,
(size_t) blen);
tb_append(&lbuf, EOS);
/* move forward, for next time */
bp->b_dot.l = lforw(bp->b_dot.l);
bp->b_dot.o = 0;
/* again, if it's in a window, put the new DOT back where
* we got it.
*/
if (bp->b_nwnd != 0 && wp) {
wp->w_dot = bp->b_dot;
wp->w_flag |= WFMOVE;
}
return (tb_values(lbuf));
}
/*
* Look through the list of buffers. Return TRUE if there are any changed
* buffers. Buffers that hold magic internal stuff are not considered; who
* cares if the list of buffer names is hacked. Return FALSE if no buffers
* have been changed. Return a pointer to the first changed buffer, in case
* there's only one -- it's useful info then.
*/
int
any_changed_buf(BUFFER **bpp)
{
BUFFER *bp;
int cnt = 0;
if (bpp)
*bpp = NULL;
for_each_buffer(bp) {
if (!b_is_invisible(bp) && b_is_changed(bp)) {
if (bpp && !*bpp)
*bpp = bp;
cnt++;
}
}
return (cnt);
}
/* similar to above, for buffers that have not been visited */
int
any_unread_buf(BUFFER **bpp)
{
BUFFER *bp;
int cnt = 0;
if (bpp)
*bpp = NULL;
for_each_buffer(bp) {
if (!b_is_invisible(bp) && !bp->b_active) {
if (bpp && !*bpp)
*bpp = bp;
cnt++;
}
}
return (cnt);
}
/*
* Copies string to a buffer-name, trimming trailing blanks for consistency.
*/
void
set_bname(BUFFER *bp, const char *name)
{
int j, k;
char *d;
(void) strncpy0(bp->b_bname, name, (size_t) NBUFN);
d = bp->b_bname;
for (j = 0, k = -1; d[j]; j++) {
if (!isSpace(d[j]))
k = -1;
else if (k < 0)
k = j;
}
if (k >= 0)
d[k] = EOS;
}
/*
* Look for a buffer-name in the buffer-list. This assumes that the argument
* is null-terminated. If the length is longer than will fit in a b_bname
* field, then it is probably a filename.
*/
BUFFER *
find_b_name(const char *bname)
{
BUFFER *bp;
BUFFER temp;
set_bname(&temp, bname); /* make a canonical buffer-name */
for_each_buffer(bp) {
if (eql_bname(bp, temp.b_bname))
return bp;
}
return NULL;
}
/*
* Find a buffer, by name. Return a pointer to the BUFFER structure associated
* with it. If the buffer is not found, create it. The "bflag" is the
* settings for the flags in in buffer.
*/
BUFFER *
bfind(const char *bname, UINT bflag)
{
BUFFER *bp;
LINE *lp;
BUFFER *lastb = NULL; /* buffer to insert after */
BUFFER *bp2;
for_each_buffer(bp) {
if (eql_bname(bp, bname))
return (bp);
lastb = bp;
}
TRACE((T_CALLED "bfind(%s, %u)\n", bname, bflag));
beginDisplay();
/* set everything to 0's unless we want nonzero */
if ((bp = typecalloc(BUFFER)) == NULL) {
(void) no_memory("BUFFER");
} else {
/* set this first, to make it simple to trace */
set_bname(bp, bname);
lp = lalloc(0, bp);
if (lp == NULL) {
FreeAndNull(bp);
(void) no_memory("BUFFER head");
} else {
/* and set up the other buffer fields */
bp->b_values = global_b_values;
bp->b_wtraits.w_vals = global_w_values;
bp->b_active = FALSE;
bp->b_dot.l = lp;
bp->b_dot.o = 0;
bp->b_wline = bp->b_dot;
bp->b_line = bp->b_dot;
#if WINMARK
bp->b_mark = nullmark;
#endif
bp->b_lastdot = nullmark;
#if OPT_VIDEO_ATTRS
#endif
#if OPT_CURTOKENS
set_buf_fname_expr(bp);
#endif
bp->b_flag = bflag;
bp->b_acount = (short) b_val(bp, VAL_ASAVECNT);
bp->b_fname = NULL;
ch_fname(bp, "");
fileuid_invalidate(bp);
#if OPT_ENCRYPT
if (!b_is_temporary(bp)
&& cryptkey != NULL && *cryptkey != EOS) {
(void) vl_strncpy(bp->b_cryptkey, cryptkey, sizeof(bp->b_cryptkey));
set_local_b_val(bp, MDCRYPT, TRUE);
} else
bp->b_cryptkey[0] = EOS;
#endif
bp->b_udstks[0] = bp->b_udstks[1] = NULL;
bp->b_ulinep = NULL;
bp->b_udtail = NULL;
bp->b_udlastsep = NULL;
b_set_counted(bp); /* buffer is empty */
set_lforw(lp, lp);
set_lback(lp, lp);
/* append at the end */
if (lastb)
lastb->b_bufp = bp;
else
bheadp = bp;
bp->b_bufp = NULL;
bp->b_created = countBuffers();
for_each_buffer(bp2)
bp2->b_last_used += 1;
bp->b_last_used = 1;
#if OPT_PERL || OPT_TCL
bp->b_api_private = NULL;
#endif
set_record_sep(bp, (RECORD_SEP) global_b_val(VAL_RECORD_SEP));
}
}
endofDisplay();
returnPtr(bp);
}
/*
* Given a filename, set up a buffer pointer to correspond
*/
BUFFER *
make_bp(const char *fname, UINT flags)
{
BUFFER *bp;
char bname[NBUFN];
makename(bname, fname);
unqname(bname);
if (valid_buffer(bp = bfind(bname, flags)))
ch_fname(bp, fname);
return bp;
}
/*
* Create a buffer to display the output of various utility commands, e.g.,
* [Output].
*/
BUFFER *
make_ro_bp(const char *bname, UINT flags)
{
BUFFER *bp;
if (valid_buffer(bp = bfind(bname, flags))) {
b_set_invisible(bp);
b_clr_scratch(bp); /* make it nonvolatile */
bp->b_active = TRUE;
set_rdonly(bp, non_filename(), MDVIEW);
}
return bp;
}
/*
* This routine blows away all of the text in a buffer. If the buffer is
* marked as changed then we ask if it is ok to blow it away; this is to save
* the user the grief of losing text. The window chain is nearly always wrong
* if this gets called; the caller must arrange for the updates that are
* required. Return TRUE if everything looks good.
*/
int
bclear(BUFFER *bp)
{
LINE *lp;
if (!b_is_temporary(bp) /* Not invisible or scratch */
&&b_is_changed(bp)) { /* Something changed */
char ques[30 + NBUFN];
(void) vl_strncat(vl_strncpy(ques,
"Discard changes to ",
sizeof(ques)),
bp->b_bname,
sizeof(ques));
if (mlyesno(ques) != TRUE)
return FALSE;
}
#if OPT_UPBUFF
if (bp->b_rmbuff != NULL)
(bp->b_rmbuff) (bp);
#endif
b_clr_changed(bp); /* Not changed */
beginDisplay();
freeundostacks(bp, TRUE); /* do this before removing lines */
FreeAndNull(bp->b_nmmarks); /* free the named marks */
#if OPT_SELECTIONS
free_attribs(bp);
#endif
while ((lp = lforw(buf_head(bp))) != buf_head(bp)) {
lremove2(bp, lp);
}
if (bp->b_ulinep != NULL) {
lfree(bp->b_ulinep, bp);
bp->b_ulinep = NULL;
}
FreeAndNull(bp->b_ltext);
bp->b_ltext_end = NULL;
FreeAndNull(bp->b_LINEs);
bp->b_LINEs_end = NULL;
bp->b_freeLINEs = NULL;
bp->b_dot = bp->b_line; /* Fix "." */
#if WINMARK
bp->b_mark = nullmark; /* Invalidate "mark" */
#endif
bp->b_lastdot = nullmark; /* Invalidate "mark" */
#if COMPLETE_FILES || COMPLETE_DIRS
FreeAndNull(bp->b_index_list);
bp->b_index_size = 0;
bp->b_index_counter = 0;
#endif
b_set_counted(bp);
bp->b_bytecount = 0;
bp->b_linecount = 0;
free_local_vals(b_valnames, global_b_values.bv, bp->b_values.bv);
endofDisplay();
return (TRUE);
}
/*
* Update the counts for the # of bytes and lines, to use in various display
* commands.
*/
int
bsizes(BUFFER *bp)
{
int code = FALSE;
if (valid_buffer(curbp)
&& !b_is_counted(bp)) {
LINE *lp; /* current line */
B_COUNT ending = (B_COUNT) len_record_sep(bp);
B_COUNT numchars = 0; /* # of chars in file */
L_NUM numlines = 0; /* # of lines in file */
/* count chars and lines */
for_each_line(lp, bp) {
++numlines;
numchars += ((B_COUNT) llength(lp) + ending);
#if !SMALLER /* tradeoff between codesize & data */
lp->l_number = numlines;
#endif
}
if (!b_val(bp, MDNEWLINE)) {
if (numchars > ending)
numchars -= ending;
else
numchars = 0;
}
bp->b_bytecount = numchars;
bp->b_linecount = numlines;
TRACE(("bsizes %s %s %d lines, %lu bytes\n",
bp->b_bname,
b_val(bp, MDDOS) ? "dos" : "unix",
numlines,
(unsigned long) numchars));
b_set_counted(bp);
code = TRUE;
}
return code;
}
/*
* Mark a buffer 'changed'
*/
void
chg_buff(BUFFER *bp, unsigned flag)
{
WINDOW *wp;
if (is_delinked_bp(bp))
return;
b_clr_counted(bp);
b_match_attrs_dirty(bp);
if (bp->b_nwnd != 1) /* Ensure hard. */
flag |= WFHARD;
if (!b_is_changed(bp)) { /* First change, so */
flag |= WFMODE; /* update mode lines. */
b_set_changed(bp);
}
b_set_recentlychanged(bp);
#if OPT_UPBUFF
if (update_on_chg(bp))
updatelistbuffers();
#endif
for_each_visible_window(wp) {
if (wp->w_bufp == bp) {
wp->w_flag |= (USHORT) flag;
#ifdef WMDLINEWRAP
/* The change may affect the line-height displayed
* on the screen. Assume the worst-case.
*/
if ((flag & WFEDIT) && w_val(wp, WMDLINEWRAP))
wp->w_flag |= WFHARD;
#endif
}
}
}
/*
* Mark a buffer 'unchanged'
*/
void
unchg_buff(BUFFER *bp, unsigned flag)
{
WINDOW *wp;
if (b_is_changed(bp)) {
if (bp->b_nwnd != 1) /* Ensure hard. */
flag |= WFHARD;
flag |= WFMODE; /* update mode lines. */
b_clr_changed(bp);
b_clr_counted(bp);
b_set_recentlychanged(bp);
b_match_attrs_dirty(bp);
for_each_visible_window(wp) {
if (wp->w_bufp == bp)
wp->w_flag |= (USHORT) flag;
}
#if OPT_UPBUFF
if (update_on_chg(bp))
updatelistbuffers();
#endif
}
}
/* unmark the current buffers change flag */
/* ARGSUSED */
int
unmark(int f GCC_UNUSED, int n GCC_UNUSED)
{
unchg_buff(curbp, 0);
return (TRUE);
}
/* write all _changed_ buffers */
/* if you get the urge to tinker in here, bear in mind that you need
to test: :ww, 1:ww, :wwq, 1:wwq, all with 1 buffer, both modified and
unmodified, with 2 buffers (0, 1, or both modified), and with errors,
like either buffer not writeable. oh yes -- you also need to turn
on "autowrite", and try ^Z and :!cmd.
by default, we should have scrolling messages, a pressreturn call, and
a screen update.
any numeric argument will suppress the pressreturn call, and therefore
also the scrolling of the messages and screen update.
if you're leaving (quitting, or suspending to the shell, you want the
scrolling messages, but no press-return, and no update.
if there's a failure, you want a pressreturn, and an update, and you
need to return non-TRUE so you won't try to quit. we also switch to
the buffer that failed in that case.
*/
int
writeallchanged(int f, int n)
{
return writeall(f, n, !f, FALSE, FALSE, FALSE);
}
int
writeallbuffers(int f, int n)
{
return writeall(f, n, !f, FALSE, FALSE, TRUE);
}
int
writeall(int f, int n, int promptuser, int leaving, int autowriting, int all)
{
BUFFER *bp; /* scanning pointer to buffers */
BUFFER *nbp;
BUFFER *oldbp; /* original current buffer */
int status = TRUE;
int count = 0;
int failure = FALSE;
int dirtymsgline = FALSE;
TRACE((T_CALLED
"writeall(f=%d, n=%d, promptuser=%d, leaving=%d, autowriting=%d, all=%d)\n",
f, n, promptuser, leaving, autowriting, all));
beginDisplay();
oldbp = curbp; /* save in case we fail */
/*
* We cannot use for_each_buffer() for this loop, because make_current()
* will temporarily change the order of the linked list of buffers, putting
* its argument at the beginning of the list unless autobuffer is set false
* (in other words, there's the possibility of an infinite loop). We can't
* temporarily change autobuffer's setting either, since that changes the
* list order. Hence the use of 'nbp'.
*/
for (bp = bheadp; bp != NULL; bp = nbp) {
nbp = bp->b_bufp;
if (!(bp->b_active))
/* we don't have it anyway - ignore */
continue;
if (autowriting && !b_val(bp, MDAUTOWRITE))
continue;
if (b_val(bp, MDREADONLY) && (n != SPECIAL_BANG_ARG))
/* ignore read-only buffer */
continue;
if (((all && !is_internalname(bp->b_fname))
|| b_is_changed(bp))
&& !b_is_invisible(bp)) {
make_current(bp); /* do this so filesave() works */
if (dirtymsgline && (promptuser || leaving)) {
mlforce("\n");
}
status = filesave(f, n);
dirtymsgline = TRUE;
failure = (status != TRUE);
if (failure) {
#define SAVE_FAILED_FMT "[Save of %s failed]"
int outlen;
char tmp[NFILEN];
mlforce("\n");
outlen = ((term.cols - 1) -
(int) (sizeof(SAVE_FAILED_FMT) - 3));
mlforce(SAVE_FAILED_FMT,
path_trunc(is_internalname(bp->b_fname)
? bp->b_bname
: bp->b_fname,
outlen,
tmp,
(int) sizeof(tmp)));
/* dirtymsgline still TRUE */
break;
#undef SAVE_FAILED_FMT
}
count++;
}
}
endofDisplay();
/* shortcut out */
if (autowriting && !failure && !count)
returnCode(TRUE);
/* do we want a press-return message? */
if (failure || ((promptuser && !leaving) && count)) {
if (dirtymsgline)
mlforce("\n");
dirtymsgline = FALSE;
pressreturn();
}
/* get our old buffer back */
make_current(oldbp);
/* and then switch to the failed buffer */
if (failure && !insertmode /* can happen from ^Z and autowrite */ )
swbuffer(bp);
/* if we asked "press-return", then we certainly need an update */
if (failure || ((promptuser && !leaving) && count)) {
sgarbf = TRUE;
(void) update(TRUE);
} else if (dirtymsgline && (promptuser || leaving)) {
mlforce("\n");
}
/* final message -- appears on the message line after the update,
or as the last line before the post-quit prompt */
if (count)
mlforce("[Wrote %d buffer%s]", count, PLURAL(count));
else
mlforce("[No buffers written]");
if (dirtymsgline)
sgarbf = TRUE;
returnCode(status);
}
#if OPT_TITLE
void
set_editor_title(void)
{
static TBUFF *title;
const char *format;
if (auto_set_title) {
/*
* We don't use "%b" for current buffer since the current
* window may not correspond, depending on how we're invoked.
*/
if (tb_length(title_format) != 0)
format = tb_values(title_format);
else
format = global_g_val(GMDSWAPTITLE)
? "%{$cbufname} - %{$progname}"
: "%{$progname} - %{$cbufname}";
special_formatter(&title, format, curwp);
tb_copy(&request_title, title);
}
}
#endif
/* For memory-leak testing (only!), releases all buffer storage. */
#if NO_LEAKS
static BUFFER *
zap_buffer(BUFFER *bp)
{
TRACE(("zap_buffer(%s) %p\n", bp->b_bname, (void *) bp));
if (bp != NULL) {
b_clr_changed(bp); /* discard any changes */
bclear(bp);
FreeBuffer(bp);
}
return NULL;
}
void
bp_leaks(void)
{
BUFFER *bp;
TRACE((T_CALLED "bp_leaks()\n"));
bminip = zap_buffer(bminip);
btempp = zap_buffer(btempp);
while ((bp = bheadp) != NULL) {
(void) zap_buffer(bp);
}
#if OPT_MODELINE
mls_regfree(-1);
#endif
returnVoid();
}
#endif
|