1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787
|
/* session.c -- user windowing interface to Info.
Copyright 1993-2024 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Originally written by Brian Fox. */
#include "info.h"
#include "display.h"
#include "session.h"
#include "util.h"
#include "echo-area.h"
#include "search.h"
#include "footnotes.h"
#include "man.h"
#include "variables.h"
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef __MINGW32__
# undef read
# define read(f,b,s) w32_read(f,b,s)
# undef _read
# define _read(f,b,s) w32_read(f,b,s)
extern ssize_t w32_read (int, void *, size_t);
#endif
#if defined (HAVE_SYS_TIME_H)
# include <sys/time.h>
# define HAVE_STRUCT_TIMEVAL
#endif /* HAVE_SYS_TIME_H */
/* **************************************************************** */
/* */
/* Running an Info Session */
/* */
/* **************************************************************** */
static void mouse_event_handler (void);
/* The place that we are reading input from. */
static FILE *info_input_stream = NULL;
/* Becomes non-zero when 'q' is typed to an Info window. */
static int quit_info_immediately = 0;
static NODE *allfiles_node = 0;
DECLARE_INFO_COMMAND (info_all_files, _("Show all matching files"))
{
if (!allfiles_node)
{
info_error (_("No file index"));
return;
}
/* FIXME: Copy allfiles_node so it is unique in the window history? */
info_set_node_of_window (window, allfiles_node);
}
static void
allfiles_create_node (char *term, REFERENCE **fref)
{
int i;
struct text_buffer text;
text_buffer_init (&text);
text_buffer_printf (&text,
"%s File names matching '%s'\n\n"
"Info File Index\n"
"***************\n\n"
"File names that match '%s':\n",
INFO_NODE_LABEL,
term, term);
/* Mark as an index so that destinations are never hidden. */
text_buffer_add_string (&text, "\0\b[index\0\b]", 11);
text_buffer_printf (&text, "\n* Menu:\n\n");
for (i = 0; fref[i]; i++)
{
text_buffer_printf (&text, "* %4i: (%s)", i+1, fref[i]->filename);
if (fref[i]->nodename)
text_buffer_printf (&text, "%s", fref[i]->nodename);
text_buffer_printf (&text, ".\n");
}
allfiles_node = info_create_node ();
allfiles_node->fullpath = xstrdup ("");
allfiles_node->nodename = xstrdup ("*Info File Index*");
allfiles_node->contents = text_buffer_base (&text);
allfiles_node->nodelen = text_buffer_off (&text);
allfiles_node->body_start = strcspn (allfiles_node->contents, "\n");
scan_node_contents (allfiles_node, 0, 0);
}
/* Begin an info session finding the nodes specified by REFERENCES. For
each loaded node, create a new window. Always split the largest of the
available windows. Display ERROR in echo area if non-null. */
static void
begin_multiple_window_info_session (REFERENCE **references, char *error)
{
register int i;
WINDOW *window = 0;
for (i = 0; references && references[i]; i++)
{
if (!window)
{
window = active_window;
info_select_reference (window, references[i]);
if (!window->node)
window = 0;
}
else
{
/* Find the largest window in WINDOWS, and make that be the active
one. Then split it and add our window and node to the list
of remembered windows and nodes. Then tile the windows. */
WINDOW *win, *largest = NULL;
int max_height = 0;
for (win = windows; win; win = win->next)
if (win->height > max_height)
{
max_height = win->height;
largest = win;
}
if (!largest)
{
display_update_display ();
info_error ("%s", msg_cant_find_window);
return;
}
active_window = largest;
window = window_make_window ();
info_select_reference (window, references[i]);
if (!window->node)
{
/* We couldn't find the node referenced. */
window_delete_window (window);
window = 0;
}
if (window)
window_tile_windows (TILE_INTERNALS);
else
{
display_update_display ();
info_error ("%s", msg_win_too_small);
return;
}
}
}
/* Load dir node as a back-up if there were no references given, or if
none of them were valid. */
if (!window)
{
info_set_node_of_window (active_window, get_dir_node ());
return;
}
}
static void
display_startup_message (void)
{
char *format;
format = replace_in_documentation
/* TRANSLATORS: Try to keep this message (when "expanded") at most 79
characters; anything after the 79th character will not actually be
displayed on an 80-column terminal. */
(_("Welcome to Info version %s. Type \\[get-help-window] for help, \\[get-info-help-node] for tutorial."),
0);
window_message_in_echo_area (format, VERSION, NULL);
}
/* Run an Info session. If USER_FILENAME is null, create a window for each
node referenced in REF_LIST. If USER_FILENAME is not null, "--all" was
used on the command line, and display a file index with entries in
REF_LIST. ERROR is an optional error message to display at start-up. */
void
info_session (REFERENCE **ref_list, char *user_filename, char *error)
{
/* Initialize the Info session. */
initialize_info_session ();
if (!error)
display_startup_message ();
else
show_error_node (error);
if (!user_filename)
begin_multiple_window_info_session (ref_list, error);
else
{
allfiles_create_node (user_filename, ref_list);
info_set_node_of_window (active_window, allfiles_node);
}
info_read_and_dispatch ();
close_info_session ();
}
void info_next_line (WINDOW *, int count);
void info_prev_line (WINDOW *, int count);
static int info_keyseq_displayed_p;
void
info_read_and_dispatch (void)
{
COMMAND_FUNCTION *cmd;
int count;
for (quit_info_immediately = 0; !quit_info_immediately; )
{
if (!info_any_buffered_input_p ())
display_update_display ();
/* Some redisplay might be necessary if the cursor has moved and
a different reference (or no reference) has to be highlighted. */
if (hl_ref_rendition.mask)
display_update_one_window (active_window);
display_cursor_at_point (active_window);
cmd = read_key_sequence (info_keymap, 1, 1, 0, &count);
if (cmd)
{
if (!info_keyseq_displayed_p)
window_clear_echo_area ();
(*cmd) (active_window, count);
/* Don't change the goal column when going up and down. This
means we can go from a long line to a short line and back to
a long line and end back in the same column. */
if (!(cmd == &info_next_line || cmd == &info_prev_line))
active_window->flags |= W_CurrentColGoal; /* Goal is current column. */
}
}
}
/* Found in signals.c */
extern void initialize_info_signal_handler (void );
/* Initialize terminal, read configuration file and set key bindings. */
void
initialize_terminal_and_keymaps (char *init_file)
{
char *term_name = getenv ("TERM");
terminal_initialize_terminal (term_name);
read_init_file (init_file);
}
/* Initialize the first info session by starting the terminal, window,
and display systems. */
void
initialize_info_session (void)
{
if (!terminal_prep_terminal ())
{
/* Terminal too dumb to run interactively. */
char *term_name = getenv ("TERM");
info_error (msg_term_too_dumb, term_name);
exit (EXIT_FAILURE);
}
terminal_clear_screen ();
window_initialize_windows (screenwidth, screenheight);
initialize_info_signal_handler ();
display_initialize_display (screenwidth, screenheight);
/* If input has not been redirected yet, make it come from unbuffered
standard input. */
if (!info_input_stream)
{
setbuf (stdin, NULL);
info_input_stream = stdin;
}
info_windows_initialized_p = 1;
}
/* On program exit, leave the cursor at the bottom of the window, and
restore the terminal I/O. */
void
close_info_session (void)
{
terminal_goto_xy (0, screenheight - 1);
terminal_clear_to_eol ();
fflush (stdout);
terminal_unprep_terminal ();
close_dribble_file ();
}
/* Tell Info that input is coming from the file FILENAME. */
void
info_set_input_from_file (char *filename)
{
FILE *stream;
/* Input may include binary characters. */
stream = fopen (filename, FOPEN_RBIN);
if (!stream)
return;
if ((info_input_stream != NULL) &&
(info_input_stream != stdin))
fclose (info_input_stream);
info_input_stream = stream;
if (stream != stdin)
display_inhibited = 1;
}
/* **************************************************************** */
/* */
/* Input Character Buffering */
/* */
/* **************************************************************** */
static void fill_input_buffer (int wait);
static int info_gather_typeahead (int);
/* Largest number of characters that we can read in advance. */
#define MAX_INFO_INPUT_BUFFERING 512
static int pop_index = 0; /* Where to remove bytes from input buffer. */
static int push_index = 0; /* Where to add bytes to input buffer. */
static unsigned char info_input_buffer[MAX_INFO_INPUT_BUFFERING];
/* Get a key from the buffer of characters to be read.
Return the key in KEY.
Result is non-zero if there was a key, or 0 if there wasn't. */
static int
get_byte_from_input_buffer (unsigned char *key)
{
if (push_index == pop_index)
return 0;
*key = info_input_buffer[pop_index++];
if (pop_index >= MAX_INFO_INPUT_BUFFERING)
pop_index = 0;
return 1;
}
int
info_any_buffered_input_p (void)
{
fill_input_buffer (0);
return push_index != pop_index;
}
/* Wrapper around info_gather_typeahead which handles read errors and reaching
end-of-file. */
static void
fill_input_buffer (int wait)
{
while (1)
{
int success;
do
{
success = info_gather_typeahead (wait);
}
while (!success && errno == EINTR); /* Try again if the read was
interrupted due to a signal. */
if (success || !wait)
return;
/* Reading failed. If we were reading from a dribble file with
--restore, switch to standard input. Otherwise quit. */
if (info_input_stream != stdin)
{
fclose (info_input_stream);
info_input_stream = stdin;
display_inhibited = 0;
display_update_display ();
display_cursor_at_point (active_window);
}
else
{
close_info_session ();
exit (EXIT_SUCCESS);
}
}
}
/* Read bytes and stuff them into info_input_buffer. If WAIT is true, wait
for input; otherwise don't do anything if there is no input waiting.
Return 1 on success, 0 on error. ERRNO may be set by read(). */
static int
info_gather_typeahead (int wait)
{
register int i = 0;
int tty, space_avail;
long chars_avail;
unsigned char input[MAX_INFO_INPUT_BUFFERING];
tty = fileno (info_input_stream);
chars_avail = 0;
/* Clear errno. */
errno = 0;
/* There may be characters left over from last time, in which case we don't
want to wait for another key to be pressed. */
if (wait && pop_index == push_index)
{
char c;
/* Wait until there is a byte waiting, and then stuff it into the input
buffer. */
if (read (tty, &c, 1) <= 0)
return 0;
if (info_dribble_file)
dribble (c);
info_input_buffer[push_index++] = c;
if (push_index >= MAX_INFO_INPUT_BUFFERING)
push_index = 0;
/* Continue to see if there are more bytes waiting. */
}
/* Get the amount of space available in INFO_INPUT_BUFFER for new chars. */
if (pop_index > push_index)
space_avail = pop_index - push_index;
else
space_avail = sizeof (info_input_buffer) - (push_index - pop_index);
/* If we can just find out how many characters there are to read, do so. */
#if defined (FIONREAD)
{
ioctl (tty, FIONREAD, &chars_avail);
if (chars_avail > space_avail)
chars_avail = space_avail;
if (chars_avail)
chars_avail = read (tty, &input[0], chars_avail);
}
#else /* !FIONREAD */
# if defined (O_NDELAY) && defined (F_GETFL) && defined (F_SETFL)
{
int flags;
flags = fcntl (tty, F_GETFL, 0);
fcntl (tty, F_SETFL, (flags | O_NDELAY));
chars_avail = read (tty, &input[0], space_avail);
fcntl (tty, F_SETFL, flags);
if (chars_avail == -1)
chars_avail = 0;
}
# else /* !O_NDELAY */
# ifdef __DJGPP__
{
extern long pc_term_chars_avail (void);
if (isatty (tty))
chars_avail = pc_term_chars_avail ();
else
{
/* We could be more accurate by calling ltell, but we have no idea
whether tty is buffered by stdio functions, and if so, how many
characters are already waiting in the buffer. So we punt. */
struct stat st;
if (fstat (tty, &st) < 0)
chars_avail = 1;
else
chars_avail = st.st_size;
}
if (chars_avail > space_avail)
chars_avail = space_avail;
if (chars_avail)
chars_avail = read (tty, &input[0], chars_avail);
}
# else
# ifdef __MINGW32__
{
extern long w32_chars_avail (int);
chars_avail = w32_chars_avail (tty);
if (chars_avail > space_avail)
chars_avail = space_avail;
if (chars_avail)
chars_avail = read (tty, &input[0], chars_avail);
}
# endif /* _WIN32 */
# endif/* __DJGPP__ */
# endif /* O_NDELAY */
#endif /* !FIONREAD */
while (i < chars_avail)
{
if (info_dribble_file)
dribble (input[i]);
/* Add KEY to the buffer of characters to be read. */
if (input[i] != Control ('g'))
{
info_input_buffer[push_index++] = input[i];
if (push_index >= MAX_INFO_INPUT_BUFFERING)
push_index = 0;
}
else
/* Flush all pending input in the case of C-g pressed. */
push_index = pop_index;
i++;
}
/* If wait is true, there is at least one byte left in the input buffer. */
if (chars_avail <= 0 && !wait)
return 0;
return 1;
}
static int get_input_key_internal (void);
static void _scroll_forward (WINDOW *window, int count, int nodeonly);
static void _scroll_backward (WINDOW *window, int count, int nodeonly);
/* Whether to process or skip mouse events in the input stream. */
unsigned char mouse_cb, mouse_cx, mouse_cy;
/* Handle mouse event given that mouse_cb, mouse_cx and mouse_cy contain the
data from the event. See the "XTerm Control Sequences" document for their
meanings. */
void
mouse_event_handler (void)
{
window_clear_echo_area();
if (mouse_cb & 0x40)
{
switch (mouse_cb & 0x03)
{
case 0: /* Mouse button 4 (scroll up). */
_scroll_backward (active_window, 3, 1);
break;
case 1: /* Mouse button 5 (scroll down). */
_scroll_forward (active_window, 3, 1);
break;
}
}
}
/* Return number representing a key that has been pressed, which is an index
into info_keymap and echo_area_keymap. */
int
get_input_key (void)
{
int ret = -1;
while (ret == -1)
{
ret = get_input_key_internal ();
if (ret == KEY_MOUSE)
{
get_byte_from_input_buffer (&mouse_cb);
get_byte_from_input_buffer (&mouse_cx);
get_byte_from_input_buffer (&mouse_cy);
}
}
return ret;
}
/* Time in milliseconds to wait for the next byte of a byte sequence
corresponding to a key or key chord. Settable with the 'key-time' user
variable. */
int key_time = 100;
/* Read bytes from input and return what key has been pressed. Return -1 on
reading an unrecognized key. */
static int
get_input_key_internal (void)
{
BYTEMAP_ENTRY *b;
unsigned char c;
int esc_seen = 0;
int pop_start;
int byte_count = 0;
fill_input_buffer (1);
if (pop_index == push_index)
return -1; /* No input waiting. This shouldn't happen. */
b = byte_seq_to_key;
pop_start = pop_index;
while (pop_index != push_index)
{
int in_map = 0;
int unknown = 0;
if (!get_byte_from_input_buffer (&c))
break; /* Incomplete byte sequence. */
byte_count++;
switch (b[c].type)
{
case BYTEMAP_KEY:
return b[c].key;
case BYTEMAP_ESC:
esc_seen = 1;
/* Fall through. */
case BYTEMAP_MAP:
in_map = 1;
b = b[c].next;
break;
case BYTEMAP_NONE:
unknown = 1;
break;
}
if (unknown)
break;
/* If we read an incomplete byte sequence, pause a short while to
see if more bytes follow. */
if (in_map && pop_index == push_index)
{
int ready = 0;
#if defined (FD_SET)
struct timeval timer, *timerp = 0;
fd_set readfds;
FD_ZERO (&readfds);
FD_SET (fileno (info_input_stream), &readfds);
timer.tv_sec = 0;
timer.tv_usec = key_time * 1000;
timerp = &timer;
ready = select (fileno(info_input_stream)+1, &readfds,
NULL, NULL, timerp);
#else
ready = 1;
#endif /* FD_SET */
if (ready)
fill_input_buffer (0);
}
}
/* Incomplete or unknown byte sequence. Start again with the first byte. */
pop_index = pop_start;
if (!esc_seen || (byte_count >= 3 && key_time == 0))
{
/* If the sequence was incomplete, return the first byte.
Also return the first byte for sequences with ESC that are at
least three bytes long if 'key_time' is 0, to give some support for
specifying byte sequences in infokey for those sent by unrecognized
special keys (which would otherwise be skipped below). */
pop_index = pop_start;
get_byte_from_input_buffer (&c);
return c;
}
else
{
get_byte_from_input_buffer (&c); /* Should be ESC */
/* If there are no more characters, then decide that the escape key
itself has been pressed. */
if (pop_index == push_index)
return 033;
/* Skip byte sequences that look like they could have come from
unrecognized keys, e.g. F3 or C-S-Left, to avoid them as being
interpreted as random garbage. These might produce sequences
that look like "ESC O R" or "ESC [ 1 ; 6 ~", depending on
the terminal. */
/* Check if the sequence starts ESC O. */
get_byte_from_input_buffer (&c);
if (c == 'O')
{
/* If no more bytes, call it M-O. */
if (!info_any_buffered_input_p ())
return 'O' + KEYMAP_META_BASE;
/* Otherwise it could be an unrecognized key producing a sequence
ESC O (byte). Ignore it, discarding the next byte. */
get_byte_from_input_buffer (&c);
return -1;
}
/* Unknown CSI-style sequences. */
else if (c == '[')
{
/* If no more bytes, call it M-[. */
if (!get_byte_from_input_buffer (&c))
return '[' + KEYMAP_META_BASE;
/* Skip a control sequence as defined by ECMA-48. */
while (c >= 0x30 && c <= 0x3f)
if (!get_byte_from_input_buffer (&c))
break;
while (c >= 0x20 && c <= 0x2f)
if (!get_byte_from_input_buffer (&c))
break;
return -1;
}
else
{
/* The sequence started with ESC, but wasn't recognized. Treat it
as introducing a sequence produced by a key chord with the meta
key pressed. */
return c + KEYMAP_META_BASE;
}
}
}
#if defined (HAVE_SYS_TIME_H)
# include <sys/time.h>
# define HAVE_STRUCT_TIMEVAL
#endif /* HAVE_SYS_TIME_H */
#if !defined (FD_SET) && defined (__MINGW32__)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
void
pause_or_input (void)
{
#ifdef FD_SET
struct timeval timer;
fd_set readfds;
#endif
if (pop_index != push_index)
return; /* Input is already waiting. */
#ifdef FD_SET
FD_ZERO (&readfds);
FD_SET (fileno (stdin), &readfds);
timer.tv_sec = 2;
timer.tv_usec = 0;
select (fileno (stdin) + 1, &readfds, NULL, NULL, &timer);
#elif defined (__MINGW32__)
/* This is signalled on key release, so flush it and wait again. */
WaitForSingleObject (GetStdHandle (STD_INPUT_HANDLE), 2000);
FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
WaitForSingleObject (GetStdHandle (STD_INPUT_HANDLE), 2000);
#endif /* FD_SET */
}
/* **************************************************************** */
/* */
/* Error handling */
/* */
/* **************************************************************** */
/* Debugging level */
unsigned debug_level;
static void
vinfo_debug (const char *format, va_list ap)
{
fprintf (stderr, "%s: ", program_name);
vfprintf (stderr, format, ap);
fprintf (stderr, "\n");
fflush (stderr);
}
void
info_debug (const char *format, ...)
{
va_list ap;
va_start (ap, format);
vinfo_debug (format, ap);
va_end (ap);
}
/* Non-zero means ring terminal bell on errors. */
int info_error_rings_bell_p = 1;
/* Print AP according to FORMAT. If the window system was initialized,
then the message is printed in the echo area. Otherwise, a message is
output to stderr. */
static void
vinfo_error (const char *format, va_list ap)
{
if (!info_windows_initialized_p || display_inhibited)
{
fprintf (stderr, "%s: ", program_name);
vfprintf (stderr, format, ap);
fprintf (stderr, "\n");
fflush (stderr);
}
else
{
if (!echo_area_is_active)
{
if (info_error_rings_bell_p)
terminal_ring_bell ();
vwindow_message_in_echo_area (format, ap);
}
else
{
NODE *temp = build_message_node (format, ap);
if (info_error_rings_bell_p)
terminal_ring_bell ();
inform_in_echo_area (temp->contents);
free (temp->contents);
free (temp);
}
}
}
void
info_error (const char *format, ...)
{
va_list ap;
va_start (ap, format);
vinfo_error (format, ap);
va_end (ap);
}
void
show_error_node (char *error)
{
if (info_error_rings_bell_p)
terminal_ring_bell ();
if (!info_windows_initialized_p)
{
info_error ("%s", error);
}
else if (!echo_area_is_active)
{
window_message_in_echo_area ("%s", error);
}
else
inform_in_echo_area (error);
}
/* **************************************************************** */
/* */
/* Window node history */
/* */
/* **************************************************************** */
/* Free a NODE object that is suitable for being placed in a window. */
void
free_history_node (NODE *n)
{
if (n && (n->flags & N_IsInternal))
{
free (n->contents);
info_free_references (n->references);
free (n->next); free (n->prev); free (n->up);
free (n->nodename);
}
free (n);
}
static void
put_node_in_window (WINDOW *win, NODE *node)
{
win->node = node;
win->pagetop = 0;
win->point = 0;
free_matches (&win->matches);
free (win->line_starts); win->line_starts = 0;
free (win->log_line_no); win->log_line_no = 0;
win->flags |= W_UpdateWindow;
}
/* Go back one in the node history. */
int
forget_node_fast (WINDOW *win)
{
int i = win->hist_index;
if (i == 0)
return 0;
free_history_node (win->hist[i - 1]->node);
free (win->hist[i - 1]);
win->hist[i - 1] = 0;
i = --win->hist_index;
if (i == 0)
/* Window history is empty. */
win->node = 0;
else
{
put_node_in_window (win, win->hist[i - 1]->node);
win->point = win->hist[i - 1]->point;
}
return i;
}
void
forget_node (WINDOW *win)
{
int i = forget_node_fast (win);
if (i == 0)
{
win->node = 0;
return; /* Window history is empty. */
}
window_set_node_of_window (win, win->hist[i - 1]->node);
if (auto_footnotes_p)
info_get_or_remove_footnotes (win);
set_window_pagetop (win, win->hist[i - 1]->pagetop);
win->point = win->hist[i - 1]->point;
window_compute_line_map (win);
win->node->display_pos = win->point;
}
/* Remove associated list of nodes of WINDOW. */
void
forget_window_and_nodes (WINDOW *win)
{
size_t i;
for (i = 0; i < win->hist_index; i++)
{
free_history_node (win->hist[i]->node);
free (win->hist[i]);
}
free (win->hist);
}
/* Like info_set_node_of_window, but only do enough so to extend the
window history, avoiding calculating line starts. */
void
info_set_node_of_window_fast (WINDOW *win, NODE *node)
{
WINDOW_STATE *new;
if (win->hist_index && win->hist[win->hist_index - 1]->node == win->node)
{
win->hist[win->hist_index - 1]->pagetop = win->pagetop;
win->hist[win->hist_index - 1]->point = win->point;
}
put_node_in_window (win, node);
new = xmalloc (sizeof (WINDOW_STATE));
new->node = win->node;
new->pagetop = win->pagetop;
new->point = win->point;
add_pointer_to_array (new, win->hist_index, win->hist, win->hist_slots, 16);
}
/* Set WINDOW to show NODE. Remember the new window in our list of
Info windows. If we are doing automatic footnote display, try to display
the footnotes for this window. */
void
info_set_node_of_window (WINDOW *win, NODE *node)
{
WINDOW_STATE *new;
/* Remember the current values of pagetop and point if the remembered node
is the same as the current one being displayed. */
if (win->hist_index && win->hist[win->hist_index - 1]->node == win->node)
{
win->hist[win->hist_index - 1]->pagetop = win->pagetop;
win->hist[win->hist_index - 1]->point = win->point;
}
/* Put this node into the window. */
window_set_node_of_window (win, node);
/* Remember this node, the currently displayed pagetop, and the current
location of point in this window. */
new = xmalloc (sizeof (WINDOW_STATE));
new->node = win->node;
new->pagetop = win->pagetop;
new->point = win->point;
add_pointer_to_array (new, win->hist_index, win->hist, win->hist_slots, 16);
/* If doing auto-footnote display/undisplay, show the footnotes belonging
to this window's node. Don't do that if it is a footnote node itself. */
if (auto_footnotes_p
&& !((win->node->flags & N_IsInternal)
&& !strcmp (win->node->nodename, "*Footnotes*")))
info_get_or_remove_footnotes (win);
}
/* **************************************************************** */
/* */
/* Cursor movement within a node */
/* */
/* **************************************************************** */
static int forward_move_node_structure (WINDOW *window, int behaviour);
static int backward_move_node_structure (WINDOW *window, int behaviour);
/* Controls whether scroll-behavior affects line movement commands */
int cursor_movement_scrolls_p = 1;
/* Variable controlling redisplay of scrolled windows. If non-zero,
it is the desired number of lines to scroll the window in order to
make point visible. A value of 1 produces smooth scrolling. If set
to zero, the line containing point is centered within the window. */
int window_scroll_step = 1;
/* Used after cursor movement commands. Scroll window so that point is
visible, and move the terminal cursor there. */
static void
info_show_point (WINDOW *window)
{
if (window_scroll_step == 0)
window_adjust_pagetop (window);
else
{
int new_pagetop = window->pagetop;
int line = window_line_of_point (window);
if (line < window->pagetop)
new_pagetop -= window_scroll_step;
else if (line >= window->pagetop + window->height)
new_pagetop += window_scroll_step;
/* It's possible that moving by 'scroll-step' still won't show the
point. If so, call window_adjust_pagetop as a backup. */
if (line >= new_pagetop && line < new_pagetop + window->height)
set_window_pagetop (window, new_pagetop);
else
window_adjust_pagetop (window);
}
if (window->flags & W_UpdateWindow)
display_update_one_window (window);
display_cursor_at_point (window);
}
/* Advance point of WIN to the beginning of the next logical line. Compute
line map of new line. Return 0 if we can't go any further. */
static int
point_next_line (WINDOW *win)
{
int line = window_line_of_point (win);
if (line + 1 < win->line_count)
{
win->point = win->line_starts[line + 1];
window_compute_line_map (win);
return 1;
}
if (cursor_movement_scrolls_p
&& forward_move_node_structure (win, info_scroll_behaviour) == 0)
{
win->point = 0;
window_compute_line_map (win);
return 1;
}
win->point = win->node->nodelen - 1;
return 0;
}
/* Move point of WIN to the end of the previous logical line. Compute
line map of new line. Return 0 if we can't go any further. */
static int
point_prev_line (WINDOW *win)
{
int line = window_line_of_point (win);
if (line > 0)
{
win->point = win->line_starts[line - 1];
window_compute_line_map (win);
win->point = win->line_map.map[win->line_map.used - 1];
return 1;
}
if (cursor_movement_scrolls_p
&& backward_move_node_structure (win, info_scroll_behaviour) == 0)
{
win->point = win->node->nodelen - 1;
if (win->line_count > win->height)
set_window_pagetop (win, win->line_count - win->height);
window_compute_line_map (win);
return 1;
}
win->point = 0;
return 0;
}
/* Return true if POINT sits on a newline character. */
static int
looking_at_newline (WINDOW *win, long point)
{
mbi_iterator_t iter;
mbi_init (iter, win->node->contents + point,
win->node->nodelen - point);
if (!mbi_avail (iter))
return 0;
return mbi_cur (iter).wc_valid && mbi_cur (iter).wc == '\n';
}
/* Return true if WIN->point sits on an alphanumeric character. */
static int
looking_at_alnum (WINDOW *win)
{
mbi_iterator_t iter;
mbi_init (iter, win->node->contents + win->point,
win->node->nodelen - win->point);
if (!mbi_avail (iter))
return 0;
return mbi_cur (iter).wc_valid && iswalnum (mbi_cur (iter).wc);
}
/* Advance point to the next multibyte character. */
static void
point_forward_char (WINDOW *win)
{
long point = win->point;
size_t col;
/* Find column in the line map after the current one that advances the
point. (This may not be the very next character if we are at a
double-width character that occupies multiple columns.) */
col = window_point_to_column (win, point, 0) + 1;
for (; col < win->line_map.used && win->line_map.map[col] == point; col++)
;
if (col < win->line_map.used)
win->point = win->line_map.map[col];
else
point_next_line (win);
}
/* Set point to the previous multibyte character. Return 0 if we can't
go any further. */
static int
point_backward_char (WINDOW *win)
{
long point = win->point;
int col;
/* Find column in the line map before the current one that moves the
point backward. */
col = window_point_to_column (win, point, 0) - 1;
for (; col >= 0 && win->line_map.map[col] == point; col--)
;
if (col >= 0)
{
win->point = win->line_map.map[col];
return 1;
}
else
return point_prev_line (win);
}
/* Advance window point to the beginning of the next word. */
static void
point_forward_word (WINDOW *win)
{
size_t col;
col = window_point_to_column (win, win->point, &win->point);
/* Skip white space forwards. */
while (1)
{
for (; col < win->line_map.used; col++)
{
win->point = win->line_map.map[col];
if (looking_at_alnum (win))
goto skipped_whitespace;
}
if (!point_next_line (win))
return;
col = 0;
}
skipped_whitespace:
while (1)
{
for (; col < win->line_map.used; col++)
{
win->point = win->line_map.map[col];
if (!looking_at_alnum (win))
return;
}
if (!point_next_line (win))
return;
col = 0;
}
}
/* Set window point to the beginning of the previous word. */
static void
point_backward_word (WINDOW *win)
{
/* Skip any white space before current cursor position. */
while (point_backward_char (win))
{
if (looking_at_alnum (win))
goto back_to_word_start;
}
back_to_word_start:
while (point_backward_char (win))
{
if (!looking_at_alnum (win))
{
point_forward_char (win);
return;
}
}
}
void info_prev_line (WINDOW *, int count);
/* Move to goal column, or end of line. */
static void
move_to_goal_column (WINDOW *window)
{
size_t goal;
goal = window->goal_column;
if (goal >= window->line_map.used)
goal = window->line_map.used - 1;
window->point = window->line_map.map[goal];
info_show_point (window);
}
/* Move WINDOW's point down to the next line if possible. */
DECLARE_INFO_COMMAND (info_next_line, _("Move down to the next line"))
{
if (count < 0)
info_prev_line (window, -count);
else
{
if (window->flags & W_CurrentColGoal)
{
window->goal_column = window_get_cursor_column (window);
window->flags &= ~W_CurrentColGoal;
}
while (count--)
point_next_line (window);
move_to_goal_column (window);
}
}
/* Move WINDOW's point up to the previous line if possible. */
DECLARE_INFO_COMMAND (info_prev_line, _("Move up to the previous line"))
{
if (count < 0)
info_next_line (window, -count);
else
{
if (window->flags & W_CurrentColGoal)
{
window->goal_column = window_get_cursor_column (window);
window->flags &= ~W_CurrentColGoal;
}
while (count--)
point_prev_line (window);
move_to_goal_column (window);
}
}
/* Move the cursor to the desired line of the window. */
DECLARE_INFO_COMMAND (info_move_to_window_line,
_("Move the cursor to a specific line of the window"))
{
int line;
/* With no numeric argument of any kind, default to the center line. */
if (!info_explicit_arg && count == 1)
line = (window->height / 2) + window->pagetop;
else
{
if (count < 0)
line = (window->height + count) + window->pagetop;
else
line = window->pagetop + count;
}
/* If the line doesn't appear in this window, make it do so. */
if (line - window->pagetop >= window->height)
line = window->pagetop + (window->height - 1);
/* If the line is too small, make it fit. */
if (line < window->pagetop)
line = window->pagetop;
/* If the selected line is past the bottom of the node, force it back. */
if (line >= window->line_count)
line = window->line_count - 1;
window->point = window->line_starts[line];
info_show_point (window);
}
/* Move WINDOW's point to the end of the logical line. */
DECLARE_INFO_COMMAND (info_end_of_line, _("Move to the end of the line"))
{
long point;
if (!window->node)
return;
/* Find physical line with end of logical line in it. */
while (!looking_at_newline (window,
window->line_map.map[window->line_map.used - 1]))
point_next_line (window);
if (window->line_map.used == 0)
return; /* This shouldn't happen. */
/* Return offset of terminating newline. */
point = window->line_map.map[window->line_map.used - 1];
if (point != window->point)
{
window->point = point;
info_show_point (window);
}
}
/* Move WINDOW's point to the beginning of the logical line. */
DECLARE_INFO_COMMAND (info_beginning_of_line, _("Move to the start of the line"))
{
int old_point = window->point;
int point;
while (1)
{
point = window->line_map.map[0];
if (point == window->line_starts[0]
|| looking_at_newline (window, point-1))
break;
if (!point_prev_line (window))
break;
}
if (point != old_point)
{
window->point = point;
info_show_point (window);
}
else
window->point = old_point;
}
void info_backward_char (WINDOW *, int count);
/* Move point forward in the node. */
DECLARE_INFO_COMMAND (info_forward_char, _("Move forward a character"))
{
if (count < 0)
info_backward_char (window, -count);
else
{
while (count--)
point_forward_char (window);
info_show_point (window);
}
}
/* Move point backward in the node. */
DECLARE_INFO_COMMAND (info_backward_char, _("Move backward a character"))
{
if (count < 0)
info_forward_char (window, -count);
else
{
while (count--)
point_backward_char (window);
info_show_point (window);
}
}
void info_backward_word (WINDOW *, int count);
/* Move forward a word in this node. */
DECLARE_INFO_COMMAND (info_forward_word, _("Move forward a word"))
{
if (count < 0)
{
info_backward_word (window, -count);
return;
}
while (count--)
point_forward_word (window);
info_show_point (window);
}
DECLARE_INFO_COMMAND (info_backward_word, _("Move backward a word"))
{
if (count < 0)
{
info_forward_word (window, -count);
return;
}
while (count--)
point_backward_word (window);
info_show_point (window);
}
/* Move to the beginning of the node. */
DECLARE_INFO_COMMAND (info_beginning_of_node, _("Move to the start of this node"))
{
window->point = 0;
info_show_point (window);
}
/* Move to the end of the node. */
DECLARE_INFO_COMMAND (info_end_of_node, _("Move to the end of this node"))
{
window->point = window->node->nodelen - 1;
info_show_point (window);
}
/* **************************************************************** */
/* */
/* Scrolling window */
/* */
/* **************************************************************** */
/* Variable controlling the behaviour of default scrolling when you are
already at the bottom of a node. Possible values are defined in session.h.
The meanings are:
IS_Continuous Try to get first menu item, or failing that, the
"Next:" pointer, or failing that, the "Up:" and
"Next:" of the up.
IS_NextOnly Try to get "Next:" menu item.
IS_PageOnly Simply give up at the bottom of a node. */
int info_scroll_behaviour = IS_Continuous;
static void _scroll_forward (WINDOW *window, int count, int nodeonly);
static void _scroll_backward (WINDOW *window, int count, int nodeonly);
static void
_scroll_forward (WINDOW *window, int count, int nodeonly)
{
if (count < 0)
_scroll_backward (window, -count, nodeonly);
else
{
if (window->pagetop >= window->line_count - window->height)
{
if (!nodeonly)
{
/* If there are no more lines to scroll here, error, or get
another node. */
if (forward_move_node_structure (window, info_scroll_behaviour)
== 0)
window->point = 0;
else
info_end_of_node (window, 1);
}
return;
}
set_window_pagetop (window, window->pagetop + count);
}
}
static void
_scroll_backward (WINDOW *window, int count, int nodeonly)
{
if (count < 0)
_scroll_backward (window, -count, nodeonly);
else
{
int desired_top;
if (window->pagetop <= 0)
{
if (!nodeonly)
{
/* If there are no more lines to scroll here, error, or get
another node. */
if (backward_move_node_structure (window, info_scroll_behaviour)
== 0)
{
info_end_of_node (window, 1);
window->point = window->line_starts[window->pagetop];
}
else
window->point = 0;
}
return;
}
desired_top = window->pagetop - count;
if (desired_top < 0)
desired_top = 0;
set_window_pagetop (window, desired_top);
}
}
/* Lines to scroll by. -1 means scroll by screen size. */
int default_window_size = -1;
/* Show the next screen of WINDOW's node. */
DECLARE_INFO_COMMAND (info_scroll_forward, _("Scroll forward in this window"))
{
int lines;
if (info_explicit_arg)
lines = count;
else if (default_window_size > 0)
lines = default_window_size * count;
else
lines = (window->height - 2) * count;
_scroll_forward (window, lines, 0);
}
/* Show the previous screen of WINDOW's node. */
DECLARE_INFO_COMMAND (info_scroll_backward, _("Scroll backward in this window"))
{
info_scroll_forward (window, -count);
}
/* Like info_scroll_forward, but sets default_window_size as a side
effect. */
DECLARE_INFO_COMMAND (info_scroll_forward_set_window,
_("Scroll forward in this window and set default window size"))
{
if (info_explicit_arg)
{
default_window_size = count;
if (default_window_size < 0)
default_window_size *= -1;
}
info_scroll_forward (window, count);
}
/* Like info_scroll_backward, but sets default_window_size as a side
effect. */
DECLARE_INFO_COMMAND (info_scroll_backward_set_window,
_("Scroll backward in this window and set default window size"))
{
info_scroll_forward_set_window (window, -count);
}
/* Show the next screen of WINDOW's node but never advance to next node. */
DECLARE_INFO_COMMAND (info_scroll_forward_page_only, _("Scroll forward in this window staying within node"))
{
int lines;
if (info_explicit_arg)
lines = count;
else if (default_window_size > 0)
lines = default_window_size * count;
else
lines = (window->height - 2) * count;
_scroll_forward (window, lines, 1);
}
/* Show the previous screen of WINDOW's node but never move to previous
node. */
DECLARE_INFO_COMMAND (info_scroll_backward_page_only, _("Scroll backward in this window staying within node"))
{
info_scroll_forward_page_only (window, -count);
}
/* Like info_scroll_forward_page_only, but sets default_window_size as a side
effect. */
DECLARE_INFO_COMMAND (info_scroll_forward_page_only_set_window,
_("Scroll forward in this window staying within node and set default window size"))
{
int lines;
if (info_explicit_arg)
{
default_window_size = count;
count = 1;
if (default_window_size < 0)
{
default_window_size *= -1;
count = -1;
}
}
if (default_window_size > 0)
lines = default_window_size * count;
else
lines = (window->height - 2) * count;
_scroll_forward (window, lines, 1);
}
/* Like info_scroll_backward_page_only, but sets default_window_size as a side
effect. */
DECLARE_INFO_COMMAND (info_scroll_backward_page_only_set_window,
_("Scroll backward in this window staying within node and set default window size"))
{
info_scroll_forward_page_only_set_window (window, -count);
}
/* Scroll the window forward by N lines. */
DECLARE_INFO_COMMAND (info_down_line, _("Scroll down by lines"))
{
_scroll_forward (window, count, 0);
}
/* Scroll the window backward by N lines. */
DECLARE_INFO_COMMAND (info_up_line, _("Scroll up by lines"))
{
_scroll_backward (window, count, 0);
}
/* Lines to scroll when using commands that scroll by half screen size
by default. 0 means scroll by half screen size. */
int default_scroll_size = 0;
/* Scroll the window forward by N lines and remember N as default for
subsequent commands. */
DECLARE_INFO_COMMAND (info_scroll_half_screen_down,
_("Scroll down by half screen size"))
{
int lines;
if (info_explicit_arg)
{
default_scroll_size = count;
count = 1;
if (default_scroll_size < 0)
{
default_scroll_size *= -1;
count = -1;
}
}
if (default_scroll_size != 0)
lines = default_scroll_size * count;
else
lines = (window->height + 1) / 2 * count;
_scroll_forward (window, lines, 1);
}
/* Scroll the window backward by N lines and remember N as default for
subsequent commands. */
DECLARE_INFO_COMMAND (info_scroll_half_screen_up,
_("Scroll up by half screen size"))
{
info_scroll_half_screen_down (window, -count);
}
/* Scroll the "other" window of WINDOW. */
DECLARE_INFO_COMMAND (info_scroll_other_window, _("Scroll the other window"))
{
WINDOW *other;
/* If only one window, give up. */
if (!windows->next)
{
info_error ("%s", msg_one_window);
return;
}
other = window->next;
if (!other)
other = window->prev;
info_scroll_forward (other, count);
}
/* Scroll the "other" window of WINDOW. */
DECLARE_INFO_COMMAND (info_scroll_other_window_backward,
_("Scroll the other window backward"))
{
info_scroll_other_window (window, -count);
}
/* **************************************************************** */
/* */
/* Garbage collection */
/* */
/* **************************************************************** */
static void
gc_file_buffers_and_nodes (void)
{
/* Array to record whether each file buffer was referenced or not. */
int *fb_referenced = xcalloc (info_loaded_files_index, sizeof (int));
WINDOW *win;
size_t i;
size_t fb_index;
/* Loop over nodes in the history of displayed windows recording
which file buffers were referenced. */
for (win = windows; win; win = win->next)
{
if (!win->hist)
continue;
for (i = 0; win->hist[i]; i++)
{
NODE *n = win->hist[i]->node;
/* Loop over file buffers. */
for (fb_index = 0; fb_index < info_loaded_files_index; fb_index++)
{
FILE_BUFFER *fb = info_loaded_files[fb_index];
/* Each node should match at most one file, either a subfile or a
non-split file. */
if (fb->flags & F_Subfile)
{
if (n->subfile && !FILENAME_CMP (fb->fullpath, n->subfile))
{
fb_referenced[fb_index] = 1;
break;
}
}
else if (!(fb->flags & F_TagsIndirect))
{
if (n->fullpath && !FILENAME_CMP (fb->fullpath, n->fullpath))
{
fb_referenced[fb_index] = 1;
break;
}
}
}
}
}
/* Free unreferenced file buffers. */
for (i = 0; i < info_loaded_files_index; i++)
{
if (!fb_referenced[i])
{
FILE_BUFFER *fb = info_loaded_files[i];
TAG **t;
if (fb->flags & F_TagsIndirect)
continue;
/* If already gc-ed, do nothing. */
if (!fb->contents)
continue;
/* If this file had to be uncompressed, check to see if we should
gc it. This means that the user-variable "gc-compressed-files"
is non-zero. */
if ((fb->flags & F_IsCompressed) && !gc_compressed_files)
continue;
/* Don't free file buffers corresponding to files that aren't there
any more, because a node may still refer to them. */
if (fb->flags & F_Gone)
continue;
free (fb->contents);
fb->contents = 0;
/* Clear pointers into the file contents in the tags table. */
if (fb->tags)
for (t = fb->tags; (*t); t++)
{
if (!((*t)->cache.flags & N_WasRewritten))
(*t)->cache.contents = 0;
}
}
}
free (fb_referenced);
}
/* **************************************************************** */
/* */
/* Commands for Manipulating Windows */
/* */
/* **************************************************************** */
void info_prev_window (WINDOW *, int count);
/* Make the next window in the chain be the active window. */
DECLARE_INFO_COMMAND (info_next_window, _("Select the next window"))
{
if (count < 0)
{
info_prev_window (window, -count);
return;
}
/* If no other window, error now. */
if (!windows->next || echo_area_is_active)
{
info_error ("%s", msg_one_window);
return;
}
while (count--)
{
if (window->next)
window = window->next;
else
window = windows;
}
if (active_window != window)
{
if (auto_footnotes_p)
info_get_or_remove_footnotes (window);
/* Point may not be within part of node displayed in window if there
has been resizing of this window since the last time it was active. */
window_adjust_pagetop (window);
window->flags |= W_UpdateWindow;
active_window = window;
}
}
/* Make the previous window in the chain be the active window. */
DECLARE_INFO_COMMAND (info_prev_window, _("Select the previous window"))
{
if (count < 0)
{
info_next_window (window, -count);
return;
}
/* Only one window? */
if (!windows->next || echo_area_is_active)
{
info_error ("%s", msg_one_window);
return;
}
while (count--)
{
if (window != windows && window->prev)
window = window->prev;
else if (window == windows)
{
/* If we are in the first window, find the last window in the
chain. */
while (window->next)
window = window->next;
}
}
if (active_window != window)
{
if (auto_footnotes_p)
info_get_or_remove_footnotes (window);
/* Point may not be within part of node displayed in window if there
has been resizing of this window since the last time it was active. */
window_adjust_pagetop (window);
window->flags |= W_UpdateWindow;
active_window = window;
}
}
/* Split active window into two windows, both showing the same node. If we
are automatically tiling windows, re-tile after the split. */
DECLARE_INFO_COMMAND (info_split_window, _("Split the current window"))
{
WINDOW *split = window_make_window ();
if (!split)
info_error ("%s", msg_win_too_small);
else
{
NODE *copy = xmalloc (sizeof (NODE));
*copy = *window->node; /* Field-by-field copy of structure. */
if (copy->flags & N_IsInternal)
{
/* This allows us to free nodes without checking if these fields
are shared by NODE objects in other windows. For non-internal
nodes, this data is stored in the tag table. */
copy->references = info_copy_references (copy->references);
copy->nodename = xstrdup (copy->nodename);
if (copy->up)
copy->up = xstrdup (copy->up);
if (copy->next)
copy->next = xstrdup (copy->next);
if (copy->prev)
copy->prev = xstrdup (copy->prev);
copy->contents = xstrdup (copy->contents);
}
info_set_node_of_window (split, copy);
/* Make sure point still appears in the active window. */
info_show_point (window);
split->pagetop = window->pagetop;
if (auto_tiling_p)
window_tile_windows (DONT_TILE_INTERNALS);
else
window_adjust_pagetop (split);
}
}
/* Delete WINDOW, forgetting the list of last visited nodes. If we are
automatically displaying footnotes, show or remove the footnotes
window. If we are automatically tiling windows, re-tile after the
deletion. */
DECLARE_INFO_COMMAND (info_delete_window, _("Delete the current window"))
{
if (!windows->next)
info_error ("%s", msg_cant_kill_last);
else if (window->flags & W_WindowIsPerm)
info_error ("%s", _("Cannot delete a permanent window"));
else
{
info_delete_window_internal (window);
if (auto_footnotes_p)
info_get_or_remove_footnotes (active_window);
if (auto_tiling_p)
window_tile_windows (DONT_TILE_INTERNALS);
gc_file_buffers_and_nodes ();
}
}
/* Do the physical deletion of WINDOW, and forget this window and
associated nodes. */
void
info_delete_window_internal (WINDOW *window)
{
if (windows->next && ((window->flags & W_WindowIsPerm) == 0))
{
forget_window_and_nodes (window);
window_delete_window (window);
if (echo_area_is_active)
echo_area_inform_of_deleted_window (window);
}
}
/* Just keep WINDOW, deleting all others. */
DECLARE_INFO_COMMAND (info_keep_one_window, _("Delete all other windows"))
{
int num_deleted; /* The number of windows we deleted. */
int pagetop, start, end;
/* Remember a few things about this window. We may be able to speed up
redisplay later by scrolling its contents. */
pagetop = window->pagetop;
start = window->first_row;
end = start + window->height;
num_deleted = 0;
while (1)
{
WINDOW *win;
/* Find an eligible window and delete it. If no eligible windows
are found, we are done. A window is eligible for deletion if
is it not permanent, and it is not WINDOW. */
for (win = windows; win; win = win->next)
if (win != window && ((win->flags & W_WindowIsPerm) == 0))
break;
if (!win)
break;
info_delete_window_internal (win);
num_deleted++;
}
/* Scroll the contents of this window into the right place so that the
user doesn't have to wait any longer than necessary for redisplay. */
if (num_deleted)
{
int amount;
amount = (window->first_row - start);
amount -= (window->pagetop - pagetop);
display_scroll_display (start, end, amount);
}
window->flags |= W_UpdateWindow;
gc_file_buffers_and_nodes ();
}
/* Change the size of WINDOW by AMOUNT. */
DECLARE_INFO_COMMAND (info_grow_window, _("Grow (or shrink) this window"))
{
window_change_window_height (window, count);
}
/* When non-zero, tiling takes place automatically when info_split_window
is called. */
int auto_tiling_p = 0;
/* Tile all of the visible windows. */
DECLARE_INFO_COMMAND (info_tile_windows,
_("Divide the available screen space among the visible windows"))
{
window_tile_windows (TILE_INTERNALS);
}
/* Toggle the state of this window's wrapping of lines. */
DECLARE_INFO_COMMAND (info_toggle_wrap,
_("Toggle the state of line wrapping in the current window"))
{
window_toggle_wrap (window);
}
/* **************************************************************** */
/* */
/* Cross-references and menus */
/* */
/* **************************************************************** */
/* Using WINDOW for various defaults, select the node referenced by ENTRY
in it. If the node is selected, the window and node are remembered.
Display an error message if reference couldn't be selected and return 0. */
int
info_select_reference (WINDOW *window, REFERENCE *entry)
{
NODE *node;
char *file_system_error = NULL;
/* We need to copy everything from entry because the call to
info_get_node_with_defaults can free it if it came from
the tag table of a file. */
char *filename = entry->filename;
char *nodename = entry->nodename;
char *label = entry->label;
int line_number = entry->line_number;
node = info_get_node_with_defaults (filename, nodename, window->node);
/* Try something a little weird. If the node couldn't be found, and the
reference was of the form "foo::", see if the entry->label can be found
as a file, with a node of "Top". */
if (!node)
{
if (info_recent_file_error)
file_system_error = xstrdup (info_recent_file_error);
if (nodename && label && !strcmp (nodename, label))
{
free (file_system_error);
file_system_error = NULL;
node = info_get_node (label, "Top");
if (!node && info_recent_file_error)
file_system_error = xstrdup (info_recent_file_error);
}
}
if (!node)
{
if (file_system_error)
{
info_error ("%s", file_system_error);
free (file_system_error);
}
else
info_error (msg_cant_find_node, nodename ? nodename : "Top");
return 0;
}
/* If in a footnotes window, try to switch to a window containing a
node from the file. */
if (window->node && (window->node->flags & N_IsInternal)
&& !strcmp (window->node->nodename, "*Footnotes*"))
{
WINDOW *w;
for (w = windows; w; w = windows->next)
{
if (!strcmp (w->node->fullpath, window->node->fullpath)
&& !(w->flags & W_TempWindow))
{
/* Switch to this window. */
active_window = window = w;
break;
}
}
}
info_set_node_of_window (window, node);
if (line_number > 0)
{
/* Go to the line given by entry->line_number. */
long line = window_log_to_phys_line (window, line_number - 1);
if (line >= 0 && line < window->line_count)
{
window->point = window->line_starts[line];
window_adjust_pagetop (window);
}
}
return 1;
}
/* Parse the node specification in LINE using WINDOW to default the filename.
Select the parsed node in WINDOW and remember it, or error if the node
couldn't be found. */
static void
info_parse_and_select (char *line, WINDOW *window)
{
REFERENCE entry;
info_parse_node (line);
entry.filename = info_parsed_filename;
entry.nodename = info_parsed_nodename;
entry.line_number = 0;
entry.label = "*info-parse-and-select*";
info_select_reference (window, &entry);
}
/* Return menu entry indexed by KEY, where '1' is the first menu item, '2' is
the second, etc., and '0' is the last. Return value should not be freed. */
static REFERENCE *
select_menu_digit (WINDOW *window, unsigned char key)
{
register int i, item;
register REFERENCE **menu;
menu = window->node->references;
if (!menu)
return 0;
item = key - '0';
/* Special case. Item "0" is the last item in this menu. */
if (item == 0)
{
int j;
i = -1; /* Not found */
for (j = 0; menu[j]; j++)
if (menu[j]->type == REFERENCE_MENU_ITEM)
i = j;
}
else
{
int k = 0;
for (i = 0; menu[i]; i++)
{
if (menu[i]->type == REFERENCE_MENU_ITEM)
k++;
if (k == item)
break;
}
}
if (i == -1)
return 0;
return menu[i];
}
DECLARE_INFO_COMMAND (info_menu_digit, _("Select this menu item"))
{} /* Declaration only. */
/* Use KEY (a digit) to select the Nth menu item in WINDOW->node. */
void
menu_digit (WINDOW *window, int key)
{
int item = key - '0';
REFERENCE *entry;
REFERENCE **references = window->node->references;
/* Check if there is a menu in this node. */
if (references)
{
int i;
for (i = 0; references[i]; i++)
if (references[i]->type == REFERENCE_MENU_ITEM)
goto has_menu;
}
info_error ("%s", msg_no_menu_node);
return;
has_menu:
if ((entry = select_menu_digit (window, key)))
info_select_reference (window, entry);
else if (key == '0')
/* Don't print "There aren't 0 items in this menu" */
info_error ("%s", msg_no_menu_node);
else
info_error (ngettext ("There isn't %d item in this menu",
"There aren't %d items in this menu",
item),
item);
return;
}
/* Select the last menu item in WINDOW->node. */
DECLARE_INFO_COMMAND (info_last_menu_item,
_("Select the last item in this node's menu"))
{
menu_digit (window, '0');
}
static int exclude_cross_references (REFERENCE *r)
{
return r->type == REFERENCE_XREF;
}
static int exclude_menu_items (REFERENCE *r)
{
return r->type == REFERENCE_MENU_ITEM;
}
static int exclude_nothing (REFERENCE *r)
{
return 1;
}
/* Read a menu or followed reference from the user defaulting to the
reference found on the current line, and select that node. The
reading is done with completion. ASK_P is non-zero if the user should
be prompted, or zero to select the item on the current line. MENU_ITEM
and XREF control whether menu items and cross-references are eligible
for selection. */
static void
info_menu_or_ref_item (WINDOW *window, int menu_item, int xref, int ask_p)
{
REFERENCE *defentry = NULL; /* Default link */
REFERENCE **refs = window->node->references;
REFERENCE *entry;
/* Name of destination */
char *line;
int line_no;
int this_line, next_line;
int which, closest = -1;
reference_bool_fn exclude;
if (!refs)
return;
if (menu_item && !xref)
{
exclude = &exclude_cross_references;
}
else if (!menu_item && xref)
{
exclude = &exclude_menu_items;
}
else if (menu_item && xref)
{
exclude = &exclude_nothing;
}
else /* !menu_item && !xref */
return;
line_no = window_line_of_point (window);
this_line = window->line_starts[line_no];
if (window->line_starts[line_no + 1])
next_line = window->line_starts[line_no + 1];
else
next_line = window->node->nodelen;
/* Look for a reference in the current line, preferring one that
the point is in, otherwise preferring after the point. */
for (which = 0; refs[which]; which++)
{
/* If we got to the next line without finding an eligible reference. */
if (refs[which]->start >= next_line)
break;
/* Check the type of reference is one we are looking for. */
if (!( (menu_item && refs[which]->type == REFERENCE_MENU_ITEM)
|| (xref && refs[which]->type == REFERENCE_XREF)))
continue;
/* Reference is eligible if any part of it is in the line. */
if (refs[which]->start >= this_line && refs[which]->start < next_line
|| refs[which]->start < this_line && refs[which]->end > this_line)
{
closest = which;
/* Use the first reference that either contains the point
or is after the point. */
if (refs[which]->end > window->point)
break;
}
}
if (closest != -1)
defentry = refs[closest];
if (ask_p)
{
char *prompt;
/* Build the prompt string. */
if (menu_item && !xref)
{
if (defentry)
xasprintf (&prompt, _("Menu item (%s): "), defentry->label);
else
prompt = xstrdup (_("Menu item: "));
}
else
{
if (defentry)
xasprintf (&prompt, _("Follow xref (%s): "), defentry->label);
else
prompt = xstrdup (_("Follow xref: "));
}
line = info_read_completing_in_echo_area_with_exclusions (prompt, refs,
exclude);
free (prompt);
window = active_window;
/* User aborts, just quit. */
if (!line)
{
info_abort_key (window, 0);
return;
}
/* If we had a default and the user accepted it, use that. */
if (!*line)
{
free (line);
if (defentry)
line = xstrdup (defentry->label);
else
line = NULL;
}
}
else
{
/* Not going to ask any questions. If we have a default entry, use
that, otherwise return. */
if (!defentry)
return;
else
line = xstrdup (defentry->label);
}
if (line)
{
/* It is possible that the references have more than a single
entry with the same label, and also LINE is down-cased, which
complicates matters even more. Try to be as accurate as we
can: if they've chosen the default, use defentry directly. */
if (defentry && strcmp (line, defentry->label) == 0)
entry = defentry;
else
/* Find the selected label in the references. If there are
more than one label which matches, find the one that's
closest to point. */
{
register long i;
long best = -1, min_dist = window->node->nodelen;
REFERENCE *ref;
for (i = 0; refs && (ref = refs[i]); i++)
{
/* Need to use mbscasecmp because LINE is downcased
inside info_read_completing_in_echo_area. */
if (mbscasecmp (line, ref->label) == 0)
{
/* ref->end is more accurate estimate of position
for menus than ref->start. Go figure. */
long dist = labs (window->point - ref->end);
if (dist < min_dist)
{
min_dist = dist;
best = i;
}
}
}
if (best != -1)
entry = refs[best];
else
entry = NULL;
}
if (!entry && defentry)
info_error (_("The reference disappeared! (%s)"), line);
else
{
info_select_reference (window, entry);
}
free (line);
}
}
/* Read a line (with completion) which is the name of a menu item,
and select that item. */
DECLARE_INFO_COMMAND (info_menu_item, _("Read a menu item and select its node"))
{
if (window->node->references)
{
REFERENCE **r;
/* Check if there is a menu in this node. */
for (r = window->node->references; *r; r++)
if ((*r)->type == REFERENCE_MENU_ITEM)
break;
if (*r)
{
info_menu_or_ref_item (window, 1, 0, 1);
return;
}
}
info_error ("%s", msg_no_menu_node);
return;
}
/* Read a line (with completion) which is the name of a reference to
follow, and select the node. */
DECLARE_INFO_COMMAND
(info_xref_item, _("Read a footnote or cross reference and select its node"))
{
if (window->node->references)
{
REFERENCE **r;
/* Check if there is a cross-reference in this node. */
for (r = window->node->references; *r; r++)
if ((*r)->type == REFERENCE_XREF)
break;
if (*r)
{
info_menu_or_ref_item (window, 0, 1, 1);
return;
}
}
info_error ("%s", msg_no_xref_node);
return;
}
/* Position the cursor at the start of this node's menu. */
DECLARE_INFO_COMMAND (info_find_menu, _("Move to the start of this node's menu"))
{
SEARCH_BINDING binding;
long position;
binding.buffer = window->node->contents;
binding.start = 0;
binding.end = window->node->nodelen;
binding.flags = S_FoldCase | S_SkipDest;
if (search (INFO_MENU_LABEL, &binding, &position) == search_success)
{
window->point = position;
window_adjust_pagetop (window);
window->flags |= W_UpdateWindow;
}
else
{
/* If not found, it could be because the menu label was removed from
the node with hide-note-references. Move to the first menu entry
in the node if there is one. */
REFERENCE **ref;
for (ref = window->node->references; *ref != 0; ref++)
{
if ((*ref)->type == REFERENCE_MENU_ITEM)
{
window->point = (*ref)->start;
window_adjust_pagetop (window);
window->flags |= W_UpdateWindow;
return;
}
}
}
info_error ("%s", msg_no_menu_node);
}
/* Visit as many menu items as is possible, each in a separate window. */
DECLARE_INFO_COMMAND (info_visit_menu,
_("Visit as many menu items at once as possible"))
{
register int i;
REFERENCE *entry, **menu;
NODE *copy;
menu = window->node->references;
if (!menu)
{
info_error ("%s", msg_no_menu_node);
return;
}
for (i = 0; (entry = menu[i]); i++)
{
WINDOW *new;
if (entry->type != REFERENCE_MENU_ITEM) continue;
copy = xmalloc (sizeof (NODE));
*copy = *window->node; /* Field-by-field copy of structure. */
new = window_make_window ();
info_set_node_of_window (new, copy);
window_tile_windows (TILE_INTERNALS);
if (!new)
{
info_error ("%s", msg_win_too_small);
break;
}
else
{
active_window = new;
if (!info_select_reference (new, entry))
break;
}
}
}
/* Move to the next or previous cross reference in this node. Return 0 if
there aren't any. */
static int
info_move_to_xref (WINDOW *window, int dir)
{
long placement = -1;
NODE *node = window->node;
REFERENCE **ref;
/* Fail if there are no references in node */
if (!node->references || !node->references[0])
return 0;
if (dir == 1) /* Search forwards */
for (ref = node->references; *ref != 0; ref++)
{
if ((*ref)->start > window->point)
{
placement = (*ref)->start;
break;
}
}
else /* Search backwards */
for (ref = node->references; *ref != 0; ref++)
{
if ((*ref)->start >= window->point) break;
placement = (*ref)->start;
}
if (placement == -1)
{
/* There was neither a menu or xref entry appearing in this node
after point. */
return 0;
}
window->point = placement;
window_adjust_pagetop (window);
return 1;
}
void info_move_to_next_xref (WINDOW *, int count);
/* Remove history entries from START inclusive to END exclusive.
Warning: be careful about removing the last history entry, as
info_set_node_of_window includes the currently displayed node in
the history. */
static void
cleanup_history (WINDOW *window, int start, int end)
{
int i;
for (i = start; i < end; i++)
{
free_history_node (window->hist[i]->node);
free (window->hist[i]);
}
memmove (&window->hist[start], &window->hist[end],
(window->hist_index - end) * sizeof (WINDOW_STATE *));
window->hist_index -= end - start;
window->hist[window->hist_index] = 0;
}
DECLARE_INFO_COMMAND (info_move_to_prev_xref,
_("Move to the previous cross reference"))
{
if (count < 0)
info_move_to_next_xref (window, -count);
else
{
size_t last_hist_index, starting_hist_index;
char *initial_nodename = window->node->nodename;
last_hist_index = starting_hist_index = window->hist_index - 1;
while (count > 0)
{
if (info_move_to_xref (window, -1))
{
last_hist_index = window->hist_index - 1;
count--;
continue;
}
/* When cursor-movement-scrolls=Off, cycle round the node's
references. */
if (!cursor_movement_scrolls_p)
{
REFERENCE **r = window->node->references;
if (r && r[0])
{
int i = 0;
/* Choose the last menu or xref entry appearing in this
node. */
while (r[i + 1])
i++;
window->point = r[i]->start;
window_adjust_pagetop (window);
count--;
continue;
}
info_error ("%s", msg_no_xref_node);
return;
}
if (backward_move_node_structure (window, info_scroll_behaviour != 0)
|| !strcmp (window->node->nodename, initial_nodename))
{
break; /* No earlier nodes in file, or we are back where we
started. */
}
window->point = window->node->nodelen - 1;
}
/* Go back to the last place a reference was found, or
the starting place. */
while (window->hist_index > last_hist_index + 1)
forget_node (window);
/* Remove any intermediate nodes. */
if (last_hist_index != starting_hist_index)
cleanup_history (window, starting_hist_index + 1, last_hist_index);
}
}
DECLARE_INFO_COMMAND (info_move_to_next_xref,
_("Move to the next cross reference"))
{
if (count < 0)
info_move_to_prev_xref (window, -count);
else
{
size_t last_hist_index, starting_hist_index;
char *initial_nodename = window->node->nodename;
last_hist_index = starting_hist_index = window->hist_index - 1;
while (count > 0)
{
if (info_move_to_xref (window, 1))
{
last_hist_index = window->hist_index - 1;
count--;
continue;
}
/* When cursor-movement-scrolls=Off, cycle round the node's
references. */
if (!cursor_movement_scrolls_p)
{
REFERENCE **r = window->node->references;
if (r && r[0])
{
/* Choose the first menu or xref entry appearing in this
node. */
window->point = r[0]->start;
window_adjust_pagetop (window);
count--;
continue;
}
info_error ("%s", msg_no_xref_node);
return;
}
if (forward_move_node_structure (window, info_scroll_behaviour) != 0
|| !strcmp (window->node->nodename, initial_nodename))
{
/*TODO: Print an error. */
break; /* No later nodes in file, or we are back where we
started. */
}
}
/* Go back to the last place a reference was found, or
the starting place. */
while (window->hist_index > last_hist_index + 1)
forget_node (window);
/* Remove any intermediate nodes. */
if (last_hist_index != starting_hist_index)
cleanup_history (window, starting_hist_index + 1, last_hist_index);
}
}
/* Select the menu item or reference that appears on this line. */
DECLARE_INFO_COMMAND (info_select_reference_this_line,
_("Select reference or menu item appearing on this line"))
{
REFERENCE **ref = window->node->references;
if (!ref || !*ref) return; /* No references in node */
info_menu_or_ref_item (window, 1, 1, 0);
}
/* Follow the menu list in MENUS (list of strings terminated by a NULL
entry) from INITIAL_NODE. If there is an error, place a message
in ERROR. STRICT says whether to accept incomplete strings as
menu entries, and whether to return the node so far if we can't
continue at any point (that might be INITIAL_NODE itself), or to
return null. This function frees INITIAL_NODE. */
NODE *
info_follow_menus (NODE *initial_node, char **menus, char **error,
int strict)
{
NODE *node = NULL;
for (; *menus; menus++)
{
REFERENCE *entry;
char *arg = *menus; /* Remember the name of the menu entry we want. */
debug (3, ("looking for %s in %s:%s", arg, initial_node->fullpath,
initial_node->nodename));
if (!initial_node->references)
{
if (error)
{
free (*error);
xasprintf (error, _("No menu in node '%s'"),
node_printed_rep (initial_node));
}
debug (3, ("no menu found"));
if (!strict)
return initial_node;
else
{
free_history_node (initial_node);
return 0;
}
}
/* Find the specified menu item. */
entry = info_get_menu_entry_by_label (initial_node, arg, !strict);
/* If we failed to find the reference: */
if (!entry)
{
if (error)
{
free (*error);
xasprintf (error, _("No menu item '%s' in node '%s'"),
arg, node_printed_rep (initial_node));
}
debug (3, ("no entry found"));
if (!strict)
return initial_node;
else
{
free_history_node (initial_node);
return 0;
}
}
debug (3, ("entry: %s, %s", entry->filename, entry->nodename));
/* Try to find this node. */
node = info_get_node_with_defaults (entry->filename, entry->nodename,
initial_node);
if (!node)
{
debug (3, ("no matching node found"));
if (error)
{
free (*error);
xasprintf (error,
_("Unable to find node referenced by '%s' in '%s'"),
entry->label,
node_printed_rep (initial_node));
}
if (strict)
{
free_history_node (initial_node);
return 0;
}
else
return initial_node;
}
debug (3, ("node: %s, %s", node->fullpath, node->nodename));
/* Success. Go round the loop again. */
free_history_node (initial_node);
initial_node = node;
}
return initial_node;
}
/* Split STR into individual node names by writing null bytes in wherever
there are commas and constructing a list of the resulting pointers.
(We can do this since STR has had canonicalize_whitespace called on it.)
Return array terminated with NULL. */
static char **
split_list_of_nodenames (char *str)
{
unsigned len = 2;
char **nodes = xmalloc (len * sizeof (char *));
nodes[len - 2] = str;
while (*str++)
{
if (*str == ',')
{
*str++ = 0; /* get past the null byte */
len++;
nodes = xrealloc (nodes, len * sizeof (char *));
nodes[len - 2] = str;
}
}
nodes[len - 1] = NULL;
return nodes;
}
/* Read a line of input which is a sequence of menus (starting from
dir), and follow them. */
DECLARE_INFO_COMMAND (info_menu_sequence,
_("Read a list of menus starting from dir and follow them"))
{
char *line = info_read_in_echo_area (_("Follow menus: "));
/* If the user aborted, quit now. */
if (!line)
{
info_abort_key (window, 0);
return;
}
canonicalize_whitespace (line);
if (*line)
{
char *error = 0;
NODE *dir_node = get_dir_node ();
char **nodes = split_list_of_nodenames (line);
NODE *node;
/* If DIR_NODE is NULL, they might be reading a file directly,
like in "info -d . -f ./foo". Try using "Top" instead. */
if (!dir_node)
dir_node = info_get_node (window->node->fullpath, 0);
/* If we still cannot find the starting point, give up. */
if (!dir_node)
info_error (msg_cant_find_node, "Top");
else
{
node = info_follow_menus (dir_node, nodes, &error, 0);
info_set_node_of_window (window, node);
if (error)
show_error_node (error);
}
free (nodes);
}
free (line);
}
/* **************************************************************** */
/* */
/* Navigation of document structure */
/* */
/* **************************************************************** */
/* Get the node pointed to by the LABEL pointer of WINDOW->node into WINDOW.
Display error message if there is no such pointer, and return zero. */
static int
info_handle_pointer (const char *label, WINDOW *window)
{
char *description;
NODE *node;
if (!strcmp (label, "Up"))
description = window->node->up;
else if (!strcmp (label, "Next"))
description = window->node->next;
else if (!strcmp (label, "Prev"))
description = window->node->prev;
else /* Should not happen */
abort ();
if (!description)
{
info_error (msg_no_pointer, label);
return 0;
}
info_parse_node (description);
node = info_get_node_with_defaults (info_parsed_filename,
info_parsed_nodename,
window->node);
if (!node)
{
if (info_recent_file_error)
info_error ("%s", info_recent_file_error);
else
info_error (msg_cant_find_node, description);
return 0;
}
/* If we are going up, look for the current node in the menu. */
if (strcmp (label, "Up") == 0)
{
REFERENCE **r;
for (r = node->references; (*r); r++)
{
if ((*r)->type == REFERENCE_MENU_ITEM
&& strcmp ((*r)->nodename, window->node->nodename) == 0)
{
node->display_pos = (*r)->start;
break;
}
}
}
info_set_node_of_window (window, node);
return 1;
}
/* Make WINDOW display the "Next:" node of the node currently being
displayed. */
DECLARE_INFO_COMMAND (info_next_node, _("Select the Next node"))
{
info_handle_pointer ("Next", window);
}
/* Make WINDOW display the "Prev:" node of the node currently being
displayed. */
DECLARE_INFO_COMMAND (info_prev_node, _("Select the Prev node"))
{
info_handle_pointer ("Prev", window);
}
/* Make WINDOW display the "Up:" node of the node currently being
displayed. */
DECLARE_INFO_COMMAND (info_up_node, _("Select the Up node"))
{
info_handle_pointer ("Up", window);
}
/* Make WINDOW display the last node of this info file. */
DECLARE_INFO_COMMAND (info_last_node, _("Select the last node in this file"))
{
register int i;
FILE_BUFFER *fb = file_buffer_of_window (window);
NODE *node = NULL;
if (fb && fb->tags)
{
int last_node_tag_idx = -1;
/* If no explicit argument, or argument of zero, default to the
last node. */
if (count == 0 || (count == 1 && !info_explicit_arg))
count = -1;
for (i = 0; count && fb->tags[i]; i++)
if (!(fb->tags[i]->flags & T_IsAnchor)) /* don't count anchor tags */
{
count--;
last_node_tag_idx = i;
}
if (count > 0)
i = last_node_tag_idx + 1;
if (i > 0)
node = info_get_node (fb->filename, fb->tags[i - 1]->nodename);
}
if (!node)
info_error ("%s", _("This window has no additional nodes"));
else
info_set_node_of_window (window, node);
}
/* Make WINDOW display the first node of this info file. */
DECLARE_INFO_COMMAND (info_first_node, _("Select the first node in this file"))
{
FILE_BUFFER *fb = file_buffer_of_window (window);
NODE *node = NULL;
/* If no explicit argument, or argument of zero, default to the
first node. */
if (count == 0)
count = 1;
if (fb && fb->tags)
{
register int i;
int last_node_tag_idx = -1;
for (i = 0; count && fb->tags[i]; i++)
if (!(fb->tags[i]->flags & T_IsAnchor)) /* don't count anchor tags */
{
count--;
last_node_tag_idx = i;
}
if (count > 0)
i = last_node_tag_idx + 1;
if (i > 0)
node = info_get_node (fb->filename, fb->tags[i - 1]->nodename);
}
if (!node)
info_error ("%s", _("This window has no additional nodes"));
else
info_set_node_of_window (window, node);
}
/* Controls what to do when a scrolling command is issued at the end of the
last node. */
int scroll_last_node = SLN_Stop;
/* Move to 1st menu item, Next, Up/Next, or error in this window. Return
non-zero on error, 0 on success. Display an error message if there is an
error. */
static int
forward_move_node_structure (WINDOW *window, int behaviour)
{
if (window->node->flags & (N_IsInternal | N_IsManPage))
return 1;
switch (behaviour)
{
case IS_PageOnly:
info_error ("%s", msg_at_node_bottom);
return 1;
case IS_NextOnly:
return !info_handle_pointer ("Next", window);
break;
case IS_Continuous:
{
/* If this node contains a menu, select its first entry. Indices
are an exception, as their menus lead nowhere meaningful. Likewise
for dir nodes. */
if (!(window->node->flags & N_IsIndex)
&& !(window->node->flags & N_IsDir))
{
REFERENCE *entry;
if ((entry = select_menu_digit (window, '1')))
{
info_select_reference (window, entry);
return 0;
}
}
/* Okay, this node does not contain a menu. If it contains a
"Next:" pointer, use that. */
if (window->node->next)
{
return !info_handle_pointer ("Next", window);
}
/* Okay, there wasn't a "Next:" for this node. Move "Up:" until we
can move "Next:". If that isn't possible, complain that there
are no more nodes. */
{
int up_counter;
int starting_hist_index = window->hist_index;
/* Back up through the "Up:" pointers until we have found a "Next:"
that isn't the same as the first menu item found in that node. */
up_counter = 0;
while (1)
{
if (window->node->up)
{
REFERENCE *entry;
if (!info_handle_pointer ("Up", window))
return 1;
up_counter++;
/* If no "Next" pointer, keep backing up. */
if (!window->node->next)
continue;
/* If this node's first menu item is the same as this node's
Next pointer, keep backing up. */
entry = select_menu_digit (window, '1');
if (entry && !strcmp (window->node->next, entry->nodename))
continue;
/* This node has a "Next" pointer, and it is not the
same as the first menu item found in this node. */
if (!info_handle_pointer ("Next", window))
return 1;
/* Don't include intermediate nodes in the window's
history. */
cleanup_history (window, starting_hist_index,
window->hist_index - 1);
return 0;
}
else
{
/* No more "Up" pointers. We are at the last node in the
file. */
register int i;
for (i = 0; i < up_counter; i++)
forget_node (window);
switch (scroll_last_node)
{
case SLN_Stop:
info_error ("%s",
_("No more nodes within this document"));
return 1;
case SLN_Top:
info_parse_and_select ("Top", window);
return 0;
default:
abort ();
}
}
}
}
break;
}
}
return 0;
}
/* Move to earlier node in node hierarchy in WINDOW depending on BEHAVIOUR.
Display an error message if node wasn't changed. */
static int
backward_move_node_structure (WINDOW *window, int behaviour)
{
if (window->node->flags & (N_IsInternal | N_IsManPage))
return 1;
switch (behaviour)
{
case IS_PageOnly:
info_error ("%s", msg_at_node_top);
return 1;
case IS_NextOnly:
return !info_handle_pointer ("Prev", window);
break;
case IS_Continuous:
if (window->node->up)
{
/* If up is the dir node, we are at the top node.
Don't do anything. */
if (!strncasecmp (window->node->up, "(dir)", strlen ("(dir)")))
{
info_error ("%s", _("No 'Prev' or 'Up' for this node within this document"));
return 1;
}
/* If 'Prev' and 'Up' are the same, we are at the first node
of the 'Up' node's menu. Go to up node. */
else if (window->node->prev
&& !strcmp(window->node->prev, window->node->up))
{
if (!info_handle_pointer ("Up", window))
return 1;
}
/* Otherwise, go to 'Prev' node and go down the last entry
in the menus as far as possible. */
else if (window->node->prev)
{
int starting_hist_index = window->hist_index;
if (!info_handle_pointer ("Prev", window))
return 1;
if (!(window->node->flags & N_IsIndex))
{
while (1)
{
REFERENCE *entry = select_menu_digit (window, '0');
if (!entry)
break;
if (!info_select_reference (window, entry))
break;
}
/* Don't include intermediate nodes in the window's
history. */
cleanup_history (window, starting_hist_index,
window->hist_index - 1);
}
}
else /* 'Up' but no 'Prev' */
{
if (!info_handle_pointer ("Up", window))
return 1;
}
}
else if (window->node->prev) /* 'Prev' but no 'Up' */
{
if (!info_handle_pointer ("Prev", window))
return 1;
}
else
{
info_error ("%s", _("No 'Prev' or 'Up' for this node within this document"));
return 1;
}
break; /* case IS_Continuous: */
}
return 0;
}
void info_global_prev_node (WINDOW *, int count);
/* Move continuously forward through the node structure of this info file. */
DECLARE_INFO_COMMAND (info_global_next_node,
_("Move forwards or down through node structure"))
{
if (count < 0)
info_global_prev_node (window, -count);
else
{
while (count)
{
if (forward_move_node_structure (window, IS_Continuous))
break;
count--;
}
}
}
/* Move continuously backward through the node structure of this info file. */
DECLARE_INFO_COMMAND (info_global_prev_node,
_("Move backwards or up through node structure"))
{
if (count < 0)
info_global_next_node (window, -count);
else
{
while (count)
{
if (backward_move_node_structure (window, IS_Continuous))
break;
count--;
}
}
}
/* **************************************************************** */
/* */
/* Info Node Commands */
/* */
/* **************************************************************** */
/* Read a line of input which is a node name, and go to that node. */
DECLARE_INFO_COMMAND (info_goto_node, _("Read a node name and select it"))
{
char *line;
#define GOTO_COMPLETES
#if defined (GOTO_COMPLETES)
/* Build a completion list of all of the known nodes. */
{
register int fbi, i;
FILE_BUFFER *current;
REFERENCE **items = NULL;
size_t items_index = 0;
size_t items_slots = 0;
current = file_buffer_of_window (window);
for (fbi = 0; info_loaded_files && info_loaded_files[fbi]; fbi++)
{
FILE_BUFFER *fb;
REFERENCE *entry;
int this_is_the_current_fb;
fb = info_loaded_files[fbi];
this_is_the_current_fb = (current == fb);
entry = xmalloc (sizeof (REFERENCE));
entry->filename = entry->nodename = NULL;
entry->label = xmalloc (4 + strlen (fb->filename));
sprintf (entry->label, "(%s)*", fb->filename);
add_pointer_to_array (entry, items_index, items, items_slots, 10);
if (fb->tags)
{
for (i = 0; fb->tags[i]; i++)
{
entry = xmalloc (sizeof (REFERENCE));
entry->filename = entry->nodename = NULL;
if (this_is_the_current_fb)
entry->label = xstrdup (fb->tags[i]->nodename);
else
{
entry->label = xmalloc
(4 + strlen (fb->filename) +
strlen (fb->tags[i]->nodename));
sprintf (entry->label, "(%s)%s",
fb->filename, fb->tags[i]->nodename);
}
add_pointer_to_array (entry, items_index, items,
items_slots, 100);
}
}
}
line = info_read_maybe_completing (_("Goto node: "), items);
info_free_references (items);
}
#else /* !GOTO_COMPLETES */
line = info_read_in_echo_area (_("Goto node: "));
#endif /* !GOTO_COMPLETES */
/* If the user aborted, quit now. */
if (!line)
{
info_abort_key (window, 0);
return;
}
canonicalize_whitespace (line);
if (*line)
info_parse_and_select (line, window);
free (line);
}
static NODE *
find_invocation_node_by_nodename (FILE_BUFFER *fb, char *program)
{
NODE *node = 0;
TAG **n;
char *try1, *try2;
n = fb->tags;
if (!n)
return 0;
xasprintf (&try1, "Invoking %s", program);
xasprintf (&try2, "%s invocation", program);
for (; *n; n++)
{
if ((*n)->nodename
&& (!strcasecmp ((*n)->nodename, try1)
|| !strcasecmp ((*n)->nodename, try2)))
{
node = info_get_node_of_file_buffer (fb, (*n)->nodename);
break;
}
}
free (try1); free (try2);
return node;
}
/* Find the node in the file with name FILE that is the best candidate to
list the PROGRAM's invocation info and its command-line options, by looking
for menu items and chains of menu items with characteristic names. This
function frees NODE. Return value should be freed by caller with
info_reference_free. */
REFERENCE *
info_intuit_options_node (NODE *node, char *program)
{
/* The list of node names typical for GNU manuals where the program
usage and specifically the command-line arguments are described.
This is pure heuristics. I gathered these node names by looking
at all the Info files I could put my hands on. If you are
looking for evidence to complain to the GNU project about
non-uniform style of documentation, here you have your case! */
static const char *invocation_nodes[] = {
"%s invocation",
"Invoking %s",
"Preliminaries", /* m4 has Invoking under Preliminaries! */
"Invocation",
"Command Arguments",/* Emacs */
"Invoking `%s'",
"%s options",
"Options",
"Option ", /* e.g. "Option Summary" */
"Invoking",
"All options", /* tar, paxutils */
"Arguments",
"%s cmdline", /* ar */
"%s", /* last resort */
(const char *)0
};
char *filename = node->fullpath;
if (!strcmp ("Top", node->nodename))
{
/* Look through the list of nodes (and anchors) in the file for a node to
start at. There may be an invocation node that is not listed in the
top-level menu (this is the case for the Bash 4.2 manual), or it may
be referred to with an anchor ("Invoking makeinfo" in Texinfo
manual). */
FILE_BUFFER *fb;
NODE *n;
fb = info_find_file (filename);
if (!fb)
return 0;
n = find_invocation_node_by_nodename (fb, program);
if (n)
{
free_history_node (node);
node = n;
}
}
/* We keep looking deeper and deeper in the menu structure until
there are no more menus or no menu items from the above list.
Some manuals have the invocation node sitting 3 or 4 levels deep
in the menu hierarchy... */
while (1)
{
const char **try_node;
REFERENCE *entry = NULL;
/* If no menu in this node, stop here. Perhaps this node
is the one they need. */
if (!node->references)
break;
/* Look for node names typical for usage nodes in this menu. */
for (try_node = invocation_nodes; *try_node; try_node++)
{
char *nodename;
xasprintf (&nodename, *try_node, program);
/* The last resort "%s" is dangerous, so we restrict it
to exact matches here. */
entry = info_get_menu_entry_by_label
(node, nodename, strcmp (*try_node, "%s"));
free (nodename);
if (entry)
break;
}
if (!entry)
break;
/* Go down into menu, and repeat. */
if (!entry->filename)
entry->filename = xstrdup (filename);
{
NODE *node2;
node2 = info_get_node (entry->filename, entry->nodename);
if (!node2)
break;
free_history_node (node);
node = node2;
}
}
{
char *n = node->nodename;
node->nodename = 0;
free_history_node (node);
return info_new_reference (filename, n);
}
}
/* Given a name of an Info file, find the name of the package it describes by
removing the leading directories and extensions. Return value should be
freed by caller. */
char *
program_name_from_file_name (char *file_name)
{
int i;
char *program_name = xstrdup (filename_non_directory (file_name));
for (i = strlen (program_name) - 1; i > 0; i--)
if (program_name[i] == '.'
&& (FILENAME_CMPN (program_name + i, ".info", 5) == 0
|| FILENAME_CMPN (program_name + i, ".inf", 4) == 0
#ifdef __MSDOS__
|| FILENAME_CMPN (program_name + i, ".i", 2) == 0
#endif
/* a man page foo.1 */
|| isdigit ((unsigned char) program_name[i + 1])))
{
program_name[i] = 0;
break;
}
return program_name;
}
DECLARE_INFO_COMMAND (info_goto_invocation_node,
_("Find the node describing program invocation"))
{
const char *invocation_prompt = _("Find Invocation node of [%s]: ");
char *program_name, *line;
char *default_program_name, *prompt, *file_name;
NODE *top_node;
REFERENCE *invocation_ref;
/* Intuit the name of the program they are likely to want.
We use the file name of the current Info file as a hint. */
file_name = window->node->fullpath;
default_program_name = program_name_from_file_name (file_name);
xasprintf (&prompt, invocation_prompt, default_program_name);
line = info_read_in_echo_area (prompt);
free (prompt);
if (!line)
{
info_abort_key (window, 0);
free (default_program_name);
return;
}
if (*line)
program_name = line;
else
program_name = default_program_name;
/* In interactive usage they'd probably expect us to begin looking
from the Top node. */
top_node = info_get_node (file_name, 0);
if (!top_node)
info_error (msg_cant_find_node, "Top");
invocation_ref = info_intuit_options_node (top_node, program_name);
/* We've got our best shot at the invocation node. Now select it. */
if (invocation_ref)
{
info_select_reference (window, invocation_ref);
info_reference_free (invocation_ref);
}
free (line);
free (default_program_name);
}
DECLARE_INFO_COMMAND (info_man, _("Read a manpage reference and select it"))
{
char *line;
line = info_read_in_echo_area (_("Get Manpage: "));
if (!line)
{
info_abort_key (window, 0);
return;
}
canonicalize_whitespace (line);
if (*line)
{
NODE *manpage = info_get_node (MANPAGE_FILE_BUFFER_NAME, line);
if (manpage)
info_set_node_of_window (window, manpage);
}
free (line);
}
/* Move to the "Top" node in this file. */
DECLARE_INFO_COMMAND (info_top_node, _("Select the node 'Top' in this file"))
{
info_parse_and_select ("Top", window);
}
/* Move to the node "(dir)Top". */
DECLARE_INFO_COMMAND (info_dir_node, _("Select the node '(dir)'"))
{
info_parse_and_select ("(dir)Top", window);
}
DECLARE_INFO_COMMAND (info_display_file_info,
_("Show full file name of node being displayed"))
{
if (window->node->fullpath && *window->node->fullpath)
{
int line = window_line_of_point (window) + 1;
window_message_in_echo_area ("File name: %s, line %d of %ld (%ld%%)",
window->node->subfile
? window->node->subfile
: window->node->fullpath,
line, window->line_count,
line * 100 / window->line_count);
}
else
window_message_in_echo_area ("Internal node");
}
DECLARE_INFO_COMMAND (info_history_node,
_("Select the most recently selected node"))
{
if (window->hist_index > 1)
forget_node (window);
else
info_error (_("No earlier node in history"));
}
/* Read the name of a file and select the entire file. */
DECLARE_INFO_COMMAND (info_view_file, _("Read the name of a file and select it"))
{
char *line;
line = info_read_in_echo_area (_("Find file: "));
if (!line)
{
info_abort_key (active_window, 1);
return;
}
if (*line)
{
NODE *node;
node = info_get_node (line, "*");
if (!node)
{
if (info_recent_file_error)
info_error ("%s", info_recent_file_error);
else
info_error (_("Cannot find '%s'"), line);
}
else
info_set_node_of_window (window, node);
free (line);
}
}
/* **************************************************************** */
/* */
/* Dumping and Printing Nodes */
/* */
/* **************************************************************** */
struct info_namelist_entry
{
struct info_namelist_entry *next;
char name[1];
};
static int
info_namelist_add (struct info_namelist_entry **ptop, const char *name)
{
struct info_namelist_entry *p;
for (p = *ptop; p; p = p->next)
if (fncmp (p->name, name) == 0)
return 1;
p = xmalloc (sizeof (*p) + strlen (name));
strcpy (p->name, name);
p->next = *ptop;
*ptop = p;
return 0;
}
static void
info_namelist_free (struct info_namelist_entry *top)
{
while (top)
{
struct info_namelist_entry *next = top->next;
free (top);
top = next;
}
}
enum
{
DUMP_SUCCESS,
DUMP_INFO_ERROR,
DUMP_SYS_ERROR
};
static int dump_node_to_stream (FILE_BUFFER *file_buffer,
char *nodename, FILE *stream, int dump_subnodes);
static void initialize_dumping (void);
/* Dump the nodes specified with REFERENCES to the file named
in OUTPUT_FILENAME. If DUMP_SUBNODES is set, recursively dump
the nodes which appear in the menu of each node dumped. */
void
dump_nodes_to_file (REFERENCE **references,
char *output_filename, int dump_subnodes)
{
int i;
FILE *output_stream;
if (!references)
return;
/* Get the stream to print the nodes to. Special case of an output
filename of "-" means to dump the nodes to stdout. */
if (strcmp (output_filename, "-") == 0)
output_stream = stdout;
else
output_stream = fopen (output_filename, "w");
if (!output_stream)
{
info_error (_("Could not create output file '%s'"), output_filename);
return;
}
initialize_dumping ();
/* Print each node to stream. */
for (i = 0; references[i]; i++)
{
FILE_BUFFER *file_buffer;
char *nodename;
file_buffer = info_find_file (references[i]->filename);
if (!file_buffer)
{
if (info_recent_file_error)
info_error ("%s", info_recent_file_error);
continue;
}
if (references[i]->nodename && *references[i]->nodename)
nodename = references[i]->nodename;
else
nodename = "Top";
if (dump_node_to_stream (file_buffer, nodename,
output_stream, dump_subnodes) == DUMP_SYS_ERROR)
{
info_error (_("error writing to %s: %s"), output_filename,
strerror (errno));
exit (EXIT_FAILURE);
}
}
if (output_stream != stdout)
fclose (output_stream);
debug (1, (_("closing %s"), output_filename));
}
/* A place to remember already dumped nodes. */
static struct info_namelist_entry *dumped_already;
static void
initialize_dumping (void)
{
info_namelist_free (dumped_already);
dumped_already = NULL;
}
/* Get and print the node specified by FILENAME and NODENAME to STREAM.
If DUMP_SUBNODES is non-zero, recursively dump the nodes which appear
in the menu of each node dumped. */
static int
dump_node_to_stream (FILE_BUFFER *file_buffer,
char *nodename,
FILE *stream, int dump_subnodes)
{
register int i;
NODE *node;
node = info_get_node_of_file_buffer (file_buffer, nodename);
if (!node)
{
info_error (msg_cant_find_node, nodename);
return DUMP_INFO_ERROR;
}
/* If we have already dumped this node, don't dump it again. */
if (info_namelist_add (&dumped_already, node->nodename))
{
free (node);
return DUMP_SUCCESS;
}
/* Maybe we should print some information about the node being output. */
debug (1, (_("writing node %s..."), node_printed_rep (node)));
if (write_node_to_stream (node, stream))
{
free (node);
return DUMP_SYS_ERROR;
}
/* If we are dumping subnodes, get the list of menu items in this node,
and dump each one recursively. */
if (dump_subnodes)
{
REFERENCE **menu = NULL;
/* If this node is an Index, do not dump the menu references. */
if (string_in_line ("Index", node->nodename) == -1)
menu = node->references;
if (menu)
{
for (i = 0; menu[i]; i++)
{
if (REFERENCE_MENU_ITEM != menu[i]->type) continue;
/* We don't dump Info files which are different than the
current one. */
if (!menu[i]->filename)
if (dump_node_to_stream (file_buffer, menu[i]->nodename,
stream, dump_subnodes) == DUMP_SYS_ERROR)
{
free (node);
return DUMP_SYS_ERROR;
}
}
}
}
free (node);
return DUMP_SUCCESS;
}
#if !defined (DEFAULT_INFO_PRINT_COMMAND)
# define DEFAULT_INFO_PRINT_COMMAND "lpr"
#endif /* !DEFAULT_INFO_PRINT_COMMAND */
DECLARE_INFO_COMMAND (info_print_node,
_("Pipe the contents of this node through INFO_PRINT_COMMAND"))
{
FILE *printer_pipe;
char *print_command = getenv ("INFO_PRINT_COMMAND");
int piping = 0;
if (!print_command || !*print_command)
print_command = DEFAULT_INFO_PRINT_COMMAND;
/* Note that on MS-DOS/MS-Windows, this MUST open the pipe in the
(default) text mode, since the printer drivers there need to see
DOS-style CRLF pairs at the end of each line.
FIXME: if we are to support Mac-style text files, we might need
to convert the text here. */
/* INFO_PRINT_COMMAND which says ">file" means write to that file.
Presumably, the name of the file is the local printer device. */
if (*print_command == '>')
printer_pipe = fopen (++print_command, "w");
else
{
printer_pipe = popen (print_command, "w");
piping = 1;
}
if (!printer_pipe)
{
info_error (_("Cannot open pipe to '%s'"), print_command);
return;
}
/* Maybe we should print some information about the node being output. */
debug (1, (_("printing node %s..."), node_printed_rep (window->node)));
write_node_to_stream (window->node, printer_pipe);
if (piping)
pclose (printer_pipe);
else
fclose (printer_pipe);
debug (1, (_("finished printing node %s"), node_printed_rep (window->node)));
}
int
write_node_to_stream (NODE *node, FILE *stream)
{
return fwrite (node->contents, node->nodelen, 1, stream) != 1;
}
/* **************************************************************** */
/* */
/* Info Searching Commands */
/* */
/* **************************************************************** */
/* Variable controlling the garbage collection of files briefly visited
during searches. Such files are normally gc'ed, unless they were
compressed to begin with. If this variable is non-zero, it says
to gc even those file buffer contents which had to be uncompressed. */
int gc_compressed_files = 0;
static char *search_string = NULL;
static int isearch_is_active = 0;
static int last_search_direction = 0;
static int last_search_case_sensitive = 0;
/* Whether to use regexps or not for search. */
static int use_regex = 1;
/* Toggle the usage of regular expressions in searches. */
DECLARE_INFO_COMMAND (info_toggle_regexp,
_("Toggle the usage of regular expressions in searches"))
{
use_regex = !use_regex;
window_message_in_echo_area (use_regex
? _("Using regular expressions for searches")
: _("Using literal strings for searches"));
}
/* Search for STRING in NODE starting at START. The DIR argument says which
direction to search in. If it is positive, search forward, else backwards.
If the string was found, return its location in POFF, set the
window's node, its point to be the found string, and readjust
the window's pagetop. NODE can be retained as a field within WINDOW.
WINDOW->matches should be a list of matches for NODE->contents, or null.
If new matches are calculated, they are saved in WINDOW->matches. */
static enum search_result
info_search_in_node_internal (WINDOW *window, NODE *node,
char *string, long start,
int dir, int case_sensitive,
int match_regexp, long *poff)
{
enum search_result result = search_not_found;
long start1, end1;
size_t match_index;
long new_point;
MATCH_STATE matches;
/* Check if we need to calculate new results. */
if (!matches_ready (&window->matches)
|| strcmp (window->search_string, string)
|| window->search_is_case_sensitive != case_sensitive)
{
free_matches (&window->matches);
free (window->search_string);
window->search_string = xstrdup (string);
window->search_is_case_sensitive = case_sensitive;
result = regexp_search (string, !match_regexp, !case_sensitive,
node->contents, node->nodelen, &matches);
}
else
{
matches = window->matches;
result = search_success;
}
if (result != search_success)
return result;
if (node->flags & N_Simple)
{
/* There are matches in the node, but it hasn't been scanned yet. Get
the node again, because its contents may differ. */
enum search_result subresult;
NODE *full_node;
free_matches (&matches);
full_node = info_get_node (node->fullpath, node->nodename);
subresult = info_search_in_node_internal (window, full_node,
string, start,
dir, case_sensitive,
match_regexp, poff);
if (window->node != full_node)
free (full_node);
return subresult;
}
if (dir > 0)
{
start1 = start;
end1 = node->nodelen;
}
else
{
start1 = 0;
end1 = start + 1; /* include start byte in search area */
}
if (start1 < node->body_start)
start1 = node->body_start;
if (end1 < node->body_start)
end1 = node->body_start;
result = match_in_match_list (&matches, start1, end1, dir, &match_index);
if (result != search_success)
return result;
*poff = match_by_index (&matches, match_index).rm_so;
window->flags |= W_UpdateWindow;
if (window->node != node)
info_set_node_of_window (window, node);
window->matches = matches;
if (isearch_is_active && dir > 0)
new_point = match_by_index (&matches, match_index).rm_eo;
else
new_point = match_by_index (&matches, match_index).rm_so;
window->point = new_point;
return result;
}
/* Search for STRING starting in WINDOW, starting at *START_OFF.
If the string is found in this node, set point to that position.
Otherwise, get the file buffer associated with WINDOW's node, and search
through each node in that file.
If the search succeeds, return non-zero. *START_OFF is given the start of
the found string instance. Set node and point of window to where the string
was found. (If the variable ISEARCH_IS_ACTIVE is non-zero and the search is
forwards, the point is set to the end of the search.)
Return non-zero if the search fails. */
static int
info_search_internal (char *string, WINDOW *window,
int dir, int case_sensitive,
long *start_off)
{
register int i;
FILE_BUFFER *file_buffer;
long start;
enum search_result result;
int search_other_nodes = 1;
int number_of_tags = -1, starting_tag = -1, current_tag = -1;
NODE *node = window->node; /* Node to search in. */
char *subfile_name = 0;
TAG *tag;
char *msg = 0;
int first_time = 1;
/* If this node isn't part of a larger file, search this node only. */
file_buffer = file_buffer_of_window (window);
if (!file_buffer || !file_buffer->tags
|| !strcmp (window->node->nodename, "*"))
search_other_nodes = 0;
/* Find number of tags and current tag. */
if (search_other_nodes)
{
char *initial_nodename = window->node->nodename;
for (i = 0; file_buffer->tags[i]; i++)
if (strcmp (initial_nodename, file_buffer->tags[i]->nodename) == 0)
{
starting_tag = i;
subfile_name = file_buffer->tags[i]->filename;
}
number_of_tags = i;
/* Our tag wasn't found. This shouldn't happen. */
if (starting_tag == -1)
return -1;
current_tag = starting_tag;
}
/* Set starting position of search. */
start = *start_off;
/* Search through subsequent nodes, wrapping around to the top
of the Info file until we find the string or return to this
window's node and point. */
while (1)
{
result = info_search_in_node_internal (window, node, string, start, dir,
case_sensitive, use_regex, start_off);
if (node != window->node)
free_history_node (node);
if (result == search_invalid)
return 1;
if (result == search_success)
{
if (!echo_area_is_active)
{
if (msg)
window_message_in_echo_area ("%s", _(msg));
else
window_clear_echo_area ();
}
return 0;
}
if (!search_other_nodes)
break;
search_next_node:
/* If we've searched our starting node twice, there are no matches.
Bail out. (We searched the second time in case there were matches
before the starting offset.) */
if (current_tag == starting_tag && !first_time)
break;
first_time = 0;
/* Find the next tag that isn't an anchor. */
for (i = current_tag + dir; ; i += dir)
{
if (i < 0)
{
msg = N_("Search continued from the end of the document");
i = number_of_tags - 1;
}
else if (i == number_of_tags)
{
msg = N_("Search continued from the beginning of the document");
i = 0;
}
tag = file_buffer->tags[i];
if (!(tag->flags & T_IsAnchor))
break;
}
current_tag = i;
/* Display message when searching a new subfile. */
if (!echo_area_is_active && tag->filename != subfile_name)
{
subfile_name = tag->filename;
window_message_in_echo_area
(_("Searching subfile %s ..."),
filename_non_directory (subfile_name));
}
/* Get a new node to search in. */
free_matches (&window->matches);
node = info_node_of_tag_fast (file_buffer, &tag);
if (!node)
{
/* If not doing i-search... */
if (!echo_area_is_active)
{
if (info_recent_file_error)
{
info_error ("%s", info_recent_file_error);
return -1;
}
else
{
info_error (msg_cant_file_node,
filename_non_directory (file_buffer->filename),
tag->nodename);
goto search_next_node;
}
}
}
if (dir < 0)
start = tag->cache.nodelen;
else
start = 0;
/* Allow C-g to quit the search, failing it if pressed. */
fill_input_buffer (0); \
if (info_input_buffer[pop_index] == Control ('g'))
goto funexit;
}
/* Not in interactive search. */
if (!echo_area_is_active)
info_error ("%s", _("Search failed"));
funexit:
return -1;
}
/* Minimal length of a search string */
int min_search_length = 1;
/* Read a string from the user, storing the result in SEARCH_STRING. Return 0
if the user aborted. */
static int
ask_for_search_string (int case_sensitive, int use_regex, int direction)
{
char *line, *prompt;
/* convert int to size_t for comparison with string length. Bogus
values obtained with negative values should not be a concern. */
size_t min_length = min_search_length;
if (search_string)
xasprintf (&prompt, _("%s%s%s [%s]: "),
use_regex ? _("Regexp search") : _("Search"),
case_sensitive ? _(" case-sensitively") : "",
direction < 0 ? _(" backward") : "",
search_string);
else
xasprintf (&prompt, _("%s%s%s: "),
use_regex ? _("Regexp search") : _("Search"),
case_sensitive ? _(" case-sensitively") : "",
direction < 0 ? _(" backward") : "");
line = info_read_in_echo_area (prompt);
free (prompt);
if (!line) /* User aborted. */
{
return 0;
}
if (!*line)
{
free (line);
return 1;
}
if (mbslen (line) < min_length)
{
info_error ("%s", _("Search string too short"));
free (line);
return 1;
}
free (search_string);
search_string = line;
return 1;
}
/* Common entry point for the search functions. Arguments:
WINDOW The window to search in
COUNT The sign of this argument defines the search
direction (negative for searching backwards);
its absolute value gives number of repetitions.
CASE_SENSITIVE Whether the search is case-sensitive or not.
*/
static void
info_search_1 (WINDOW *window, int count, int case_sensitive)
{
int result;
int direction;
long start_off;
char *p;
if (count < 0)
{
direction = -1;
count = -count;
}
else
{
direction = 1;
if (count == 0)
count = 1; /* for backward compatibility */
}
if (!ask_for_search_string (case_sensitive, use_regex, direction)
|| !search_string)
return;
/* Disable any active tree search. */
window->node->active_menu = 0;
start_off = window->point + direction;
/* If the search string includes upper-case letters, make the search
case-sensitive. */
if (case_sensitive == 0)
for (p = search_string; *p; p++)
if (isupper ((unsigned char) *p))
{
case_sensitive = 1;
break;
}
last_search_direction = direction;
last_search_case_sensitive = case_sensitive;
for (result = 0; result == 0 && count--; )
result = info_search_internal (search_string,
active_window, direction, case_sensitive,
&start_off);
window_adjust_pagetop (window);
/* Perhaps free the unreferenced file buffers that were searched, but
not retained. */
gc_file_buffers_and_nodes ();
}
/* Set *T to an offset within the tags table of the node referenced by R,
using WINDOW for defaults. *FB is set to the file buffer structure for
the node. */
static int
tag_of_reference (REFERENCE *r, WINDOW *window, FILE_BUFFER **fb, TAG ***t)
{
char *filename, *nodename;
int i;
filename = r->filename;
nodename = r->nodename;
if (!filename)
filename = window->node->fullpath;
if (!nodename || !*nodename)
nodename = "Top";
*fb = info_find_file (filename);
if (!*fb)
return 0;
for (i = 0; *(*t = &(*fb)->tags[i]); i++)
if (!strcmp (nodename, (**t)->nodename))
goto found_tag;
return 0;
found_tag: ;
return 1;
}
/* Value for NODE.active_menu */
#define BEFORE_MENU -99
static void tree_search_check_node (WINDOW *window);
static void tree_search_check_node_backwards (WINDOW *window);
/* Search in WINDOW->node, and nodes reachable by menus from it, for
WINDOW->search_string. */
static void
tree_search_check_node (WINDOW *window)
{
long start_off;
enum search_result result;
char *string;
int previous_match;
if (window->node->active_menu != 0)
previous_match = 1;
else
{
previous_match = 0;
window->node->active_menu = BEFORE_MENU;
}
string = xstrdup (window->search_string);
goto check_node;
check_node:
result = info_search_in_node_internal (window, window->node,
string,
window->point + 1,
1, /* Search forwards */
1, /* Case-sensitive */
0, /* No regular expressions. */
&start_off);
if (result == search_success)
{
info_show_point (window);
goto funexit;
}
/* Otherwise, try each menu entry in turn. */
if (matches_ready (&window->matches))
window->point++; /* Find this match again if/when we come back. */
goto check_menus;
/* At this juncture, window->node->active_menu is the index of the last
reference in the node to have been checked, plus one. BEFORE_MENU is a
special code to say that none of them have been checked. */
check_menus:
if (!(window->node->flags & N_IsIndex)) /* Don't go down menus in index */
{ /* nodes, because this leads to loops. */
REFERENCE *r;
int ref_index;
if (window->node->active_menu != BEFORE_MENU)
ref_index = window->node->active_menu;
else
ref_index = 0;
for (; (r = window->node->references[ref_index]); ref_index++)
if (r->type == REFERENCE_MENU_ITEM)
{
FILE_BUFFER *file_buffer;
TAG **tag;
NODE *node;
if (!tag_of_reference (r, window, &file_buffer, &tag))
continue;
if ((*tag)->flags & T_SeenBySearch)
continue;
(*tag)->flags |= T_SeenBySearch;
window->node->active_menu = ref_index + 1;
node = info_node_of_tag (file_buffer, tag);
if (!node)
continue;
info_set_node_of_window_fast (window, node);
window->node->active_menu = BEFORE_MENU;
goto check_node;
}
}
goto go_up;
go_up:
/* If no more menu entries, try going back. */
if (window->hist_index >= 2
&& window->hist[window->hist_index - 2]->node->active_menu != 0)
{
forget_node_fast (window);
goto check_menus;
}
/* Go back to the final match. */
if (previous_match)
{
message_in_echo_area (_("Going back to last match from %s"),
window->node->nodename);
/* This is a trick.
Set active_menu to one more than the number of references,
and add an arbitrary node to the window history.
When we call tree_search_check_node_backwards, this will go
backwards through the tree structure to the last match.
Change active_menu back to a valid value afterwards .*/
{
int n = 0;
while (window->node->references[n])
n++;
window->node->active_menu = n + 1;
info_parse_and_select ("Top", window);
/* Check if this worked. */
if (strcmp (window->node->nodename, "Top"))
{
/* Loading "Top" node failed. */
window->node->active_menu = 0;
goto funexit;
}
window->node->active_menu = BEFORE_MENU;
}
window->point = window->node->body_start;
tree_search_check_node_backwards (window);
}
info_error (previous_match ? _("No more matches") : _("Search failed"));
funexit:
free (string);
} /*********** end tree_search_check_node *************/
/* The same as tree_search_check_node, but backwards. */
static void
tree_search_check_node_backwards (WINDOW *window)
{
long start_off;
enum search_result result;
char *string;
int previous_match;
previous_match = (window->node->active_menu != 0);
string = xstrdup (window->search_string);
goto check_node;
check_node:
result = info_search_in_node_internal (window, window->node,
string,
window->point - 1,
-1, /* Search backwards */
1, /* Case-sensitive */
0, /* No regular expressions. */
&start_off);
if (result == search_success)
{
info_show_point (window);
goto funexit;
}
goto go_up;
/* Check through menus in current node, in reverse order.
At this juncture, window->node->active_menu is the index of the last
reference in the node to have been checked, plus one. BEFORE_MENU is a
special code to say that none of them have been checked. */
check_menus:
if (!(window->node->flags & N_IsIndex)) /* Don't go down menus in index */
{ /* nodes, because this leads to loops. */
REFERENCE *r;
int ref_index;
if (window->node->active_menu == BEFORE_MENU)
goto check_node;
else
ref_index = window->node->active_menu - 2;
for (; ref_index >= 0; ref_index--)
{
r = window->node->references[ref_index];
if (r->type == REFERENCE_MENU_ITEM)
{
TAG **tag;
FILE_BUFFER *file_buffer;
NODE *node;
if (!tag_of_reference (r, window, &file_buffer, &tag))
continue;
/* This inverts what is done for the forwards search. It's
possible that we will visit the nodes in a different order if
there is more than one reference to a node. */
if (!((*tag)->flags & T_SeenBySearch))
continue;
node = info_node_of_tag (file_buffer, tag);
if (!node)
continue;
window->node->active_menu = ref_index + 1;
info_set_node_of_window_fast (window, node);
window->point = window->node->nodelen;
{
/* Start at the last menu entry in the subordinate node. */
int i;
i = 0;
while(window->node->references[i])
i++;
window->node->active_menu = i + 1;
}
goto check_menus;
}
}
}
window->node->active_menu = BEFORE_MENU;
goto check_node;
/* Try going back. */
go_up:
if (window->hist_index >= 2
&& window->hist[window->hist_index - 2]->node->active_menu != 0)
{
TAG **tag;
REFERENCE *r;
FILE_BUFFER *file_buffer;
forget_node_fast (window);
r = window->node->references[window->node->active_menu - 1];
/* Clear the flag to say we've been to the node we just came back
from. This reverse the order from the forwards search, where
we set this flag just before going down. */
if (r && tag_of_reference (r, window, &file_buffer, &tag))
{
(*tag)->flags &= ~T_SeenBySearch;
}
goto check_menus;
}
/* Otherwise, no result. */
info_error (previous_match ? _("No more matches") : _("Search failed"));
funexit:
free (string);
} /*********** end tree_search_check_node_backwards *************/
/* Clear T_SeenBySearch for all node tags. */
void
wipe_seen_flags (void)
{
size_t fb_index;
TAG **t;
for (fb_index = 0; fb_index < info_loaded_files_index; fb_index++)
{
t = info_loaded_files[fb_index]->tags;
if (!t)
continue; /* Probably a sub-file of a split file. */
for (; *t; t++)
{
(*t)->flags &= ~T_SeenBySearch;
}
}
}
DECLARE_INFO_COMMAND (info_tree_search,
_("Search this node and subnodes for a string"))
{
char *prompt, *line;
size_t i;
/* TODO: Display manual name */
/* TRANSLATORS: %s is the title of a node. */
xasprintf (&prompt, _("Search under %s: "),
window->node->nodename);
line = info_read_in_echo_area (prompt); free (prompt);
if (!line)
return;
/* Remove relics of previous tree searches. */
wipe_seen_flags ();
for (i = 0; i < window->hist_index; i++)
window->hist[i]->node->active_menu = 0;
window->search_string = line;
tree_search_check_node (window);
}
DECLARE_INFO_COMMAND (info_tree_search_next,
_("Go to next match in Info sub-tree"))
{
if (!window->search_string || window->node->active_menu == 0)
{
info_error (_("No active search"));
return;
}
tree_search_check_node (window);
}
DECLARE_INFO_COMMAND (info_tree_search_previous,
_("Go to previous match in Info sub-tree"))
{
if (!window->search_string || window->node->active_menu == 0)
{
info_error (_("No active search"));
return;
}
tree_search_check_node_backwards (window);
}
#undef BEFORE_MENU
DECLARE_INFO_COMMAND (info_search_case_sensitively,
_("Read a string and search for it case-sensitively"))
{
info_search_1 (window, count, 1);
}
DECLARE_INFO_COMMAND (info_search, _("Read a string and search for it"))
{
info_search_1 (window, count, 0);
}
DECLARE_INFO_COMMAND (info_search_backward,
_("Read a string and search backward for it"))
{
info_search_1 (window, -count, 0);
}
int search_skip_screen_p = 0;
DECLARE_INFO_COMMAND (info_search_next,
_("Repeat last search in the same direction"))
{
long start_off = window->point + 1;
NODE *starting_node = window->node;
int result;
if (window->search_string && window->node->active_menu)
{
tree_search_check_node (window);
return;
}
if (!last_search_direction || !search_string)
{
info_error ("%s", _("No previous search string"));
return;
}
if (search_skip_screen_p)
{
/* Find window bottom */
long n = window->height + window->pagetop;
if (n < window->line_count)
start_off = window->line_starts[n];
else
start_off = window->node->nodelen;
}
for (result = 0; result == 0 && count--; )
result = info_search_internal (search_string,
active_window, 1,
last_search_case_sensitive,
&start_off);
if (result == 0 && window->node == starting_node && search_skip_screen_p)
{
long match_line = window_line_of_point (window);
long new_pagetop;
/* Scroll down a whole number of screenfulls to make match visible. */
new_pagetop = window->pagetop;
new_pagetop += (match_line - window->pagetop) / window->height
* window->height;
set_window_pagetop (window, new_pagetop);
}
else
window_adjust_pagetop (window);
}
DECLARE_INFO_COMMAND (info_search_previous,
_("Repeat last search in the reverse direction"))
{
long start_off = window->point - 1;
NODE *starting_node = window->node;
int result;
if (window->search_string && window->node->active_menu)
{
tree_search_check_node_backwards (window);
return;
}
if (!last_search_direction || !search_string)
{
info_error ("%s", _("No previous search string"));
return;
}
if (search_skip_screen_p)
start_off = window->line_starts[window->pagetop] - 1;
/* start_off can be negative here, in which case info_search_internal
will go to the previous node straight away. */
for (result = 0; result == 0 && count--; )
result = info_search_internal (search_string,
active_window, -1,
last_search_case_sensitive,
&start_off);
if (result == 0 && window->node == starting_node && search_skip_screen_p)
{
long match_line = window_line_of_point (window);
long new_pagetop;
/* Scroll up a whole number of screenfulls to make match visible.
This means if 'info_search_next' was the last command, we'll
go back to the same place. */
new_pagetop = window->pagetop - window->height;
new_pagetop -= (window->pagetop - match_line - 1) / window->height
* window->height;
if (new_pagetop < 0)
new_pagetop = 0;
set_window_pagetop (window, new_pagetop);
}
else
window_adjust_pagetop (window);
}
/* If highlight-searches=On, this will clear any highlighted regions on the
screen. */
DECLARE_INFO_COMMAND (info_clear_search,
_("Clear displayed search matches"))
{
free_matches (&window->matches);
window->flags |= W_UpdateWindow;
}
/* **************************************************************** */
/* */
/* Incremental Searching */
/* */
/* **************************************************************** */
static void incremental_search (WINDOW *window, int count);
DECLARE_INFO_COMMAND (isearch_forward,
_("Search interactively for a string as you type it"))
{
incremental_search (window, count);
}
DECLARE_INFO_COMMAND (isearch_backward,
_("Search interactively for a string as you type it"))
{
incremental_search (window, -count);
}
/* Structure defining the current state of an incremental search. */
typedef struct {
char *fullpath;
char *nodename;
long pagetop; /* LINE_STARTS[PAGETOP] is first line in WINDOW. */
long point; /* Point in window. */
long start; /* Offset in node contents where search started. */
int search_index; /* Offset of the last char in the search string. */
int direction; /* The direction that this search is heading in. */
enum search_result failing; /* Whether or not this search failed. */
} SEARCH_STATE;
/* Incrementally search for a string as it is typed. */
/* The last accepted incremental search string. */
static char *last_isearch_accepted = NULL;
/* The current incremental search string. */
static char *isearch_string = NULL;
static int isearch_string_index = 0;
static int isearch_string_size = 0;
/* Array of search states. */
static SEARCH_STATE **isearch_states = NULL;
static size_t isearch_states_index = 0;
static size_t isearch_states_slots = 0;
/* Get the state of WINDOW, and save it in STATE. */
static void
window_get_state (WINDOW *window, SEARCH_STATE *state)
{
state->fullpath = window->node->fullpath;
state->nodename = window->node->nodename;
state->pagetop = window->pagetop;
state->point = window->point;
}
/* Set the node, pagetop, and point of WINDOW. */
static void
window_set_state (WINDOW *window, SEARCH_STATE *state)
{
if (strcmp(window->node->fullpath, state->fullpath)
|| strcmp(window->node->nodename, state->nodename))
{
NODE *n = info_get_node (state->fullpath, state->nodename);
info_set_node_of_window (window, n);
}
window->pagetop = state->pagetop;
window->point = state->point;
}
/* Push the state of this search. */
static void
push_isearch (WINDOW *window, int search_index, int direction,
enum search_result failing, long start_off)
{
SEARCH_STATE *state;
state = xmalloc (sizeof (SEARCH_STATE));
window_get_state (window, state);
state->search_index = search_index;
state->direction = direction;
state->failing = failing;
state->start = start_off;
add_pointer_to_array (state, isearch_states_index, isearch_states,
isearch_states_slots, 20);
}
/* Pop the state of this search to WINDOW, SEARCH_INDEX, and DIRECTION. */
static void
pop_isearch (WINDOW *window, int *search_index, int *direction,
enum search_result *failing, long *start_off)
{
SEARCH_STATE *state;
if (isearch_states_index)
{
isearch_states_index--;
state = isearch_states[isearch_states_index];
window_set_state (window, state);
*search_index = state->search_index;
*direction = state->direction;
*failing = state->failing;
*start_off = state->start;
free (state);
isearch_states[isearch_states_index] = NULL;
}
}
/* Free the memory used by isearch_states. */
static void
free_isearch_states (void)
{
register size_t i;
for (i = 0; i < isearch_states_index; i++)
{
free (isearch_states[i]);
isearch_states[i] = NULL;
}
isearch_states_index = 0;
}
/* Display the current search in the echo area. */
static void
show_isearch_prompt (int dir, unsigned char *string,
enum search_result failing)
{
register size_t i;
const char *prefix;
char *prompt, *p_rep;
unsigned int prompt_len, p_rep_index, p_rep_size;
if (dir < 0)
prefix = use_regex ? _("Regexp I-search backward: ")
: _("I-search backward: ");
else
prefix = use_regex ? _("Regexp I-search: ")
: _("I-search: ");
p_rep_index = p_rep_size = 0;
p_rep = NULL;
for (i = 0; string[i]; i++)
{
char *rep;
switch (string[i])
{
case ' ': rep = " "; break;
case LFD: rep = "\\n"; break;
case TAB: rep = "\\t"; break;
default:
rep = pretty_keyname (string[i]);
}
if ((p_rep_index + strlen (rep) + 1) >= p_rep_size)
p_rep = xrealloc (p_rep, p_rep_size += 100);
strcpy (p_rep + p_rep_index, rep);
p_rep_index += strlen (rep);
}
prompt_len = strlen (prefix) + p_rep_index + 1;
if (failing != search_success)
prompt_len += strlen (_("Failing "));
prompt = xmalloc (prompt_len);
sprintf (prompt, "%s%s%s",
failing != search_success ? _("Failing ") : "",
prefix, p_rep ? p_rep : "");
window_message_in_echo_area ("%s", prompt);
free (p_rep);
free (prompt);
display_cursor_at_point (active_window);
}
/* Read and dispatch loop for incremental search mode. */
static void
incremental_search (WINDOW *window, int count)
{
int key;
enum search_result last_search_result, search_result;
int dir;
SEARCH_STATE orig_state; /* Window state at start of incremental search. */
SEARCH_STATE mystate; /* State before each search. */
char *p;
int case_sensitive;
long start_off = window->point;
int starting_history_entry = window->hist_index - 1;
if (count < 0)
dir = -1;
else
dir = 1;
last_search_result = search_result = search_success;
window_get_state (window, &orig_state);
isearch_string_index = 0;
if (!isearch_string_size)
isearch_string = xmalloc (isearch_string_size = 50);
isearch_string[isearch_string_index] = '\0';
isearch_is_active = 1;
/* Save starting position of search. */
push_isearch (window, isearch_string_index, dir, search_result, start_off);
while (isearch_is_active)
{
COMMAND_FUNCTION *func = NULL;
int quoted = 0;
/* Show the search string in the echo area. */
show_isearch_prompt (dir, (unsigned char *) isearch_string,
search_result);
/* If a recent display was interrupted, then do the redisplay now if
it is convenient. */
if (!info_any_buffered_input_p () && display_was_interrupted_p)
{
display_update_display ();
display_cursor_at_point (active_window);
}
/* Read keys, looking in both keymaps for a recognized key sequence. */
{
Keymap info_kp, ea_kp;
info_kp = info_keymap;
ea_kp = echo_area_keymap;
key = get_input_key ();
while (1)
{
if (key >= 32 && key < 256)
break;
if (info_kp && info_kp[key].type == ISFUNC)
{
if (info_kp[key].value.function)
func = info_kp[key].value.function->func;
if (func == &isearch_forward
|| func == &isearch_backward
|| func == &info_abort_key)
{
goto gotfunc;
}
else
{
func = 0;
info_kp = 0;
}
}
else if (info_kp) /* ISKMAP */
info_kp = info_kp[key].value.keymap;
if (ea_kp && ea_kp[key].type == ISFUNC)
{
if (ea_kp[key].value.function)
func = ea_kp[key].value.function->func;
if (func == &ea_abort
|| func == &ea_quoted_insert
|| func == &ea_rubout)
{
func = ea_kp[key].value.function->func;
goto gotfunc;
}
else
{
func = 0;
ea_kp = 0;
}
}
else if (ea_kp) /* ISKMAP */
ea_kp = ea_kp[key].value.keymap;
if (!info_kp && !ea_kp)
break;
key = get_input_key ();
}
}
gotfunc:
if (func == &ea_quoted_insert)
{
/* User wants to insert a character. */
key = get_input_key ();
if (key < 0 || key >= 256)
continue; /* The user pressed a key like an arrow key. */
quoted = 1;
}
if (quoted || (!func && key >= 32 && key < 256))
{
push_isearch (window, isearch_string_index, dir,
search_result, start_off);
if (isearch_string_index + 2 >= isearch_string_size)
isearch_string = xrealloc
(isearch_string, isearch_string_size += 100);
isearch_string[isearch_string_index++] = key;
isearch_string[isearch_string_index] = '\0';
if (search_result != search_success && !use_regex)
continue;
}
else if (func == &ea_rubout)
{
/* User wants to delete one level of search? */
if (!isearch_states_index)
{
terminal_ring_bell ();
continue;
}
else
{
int end = isearch_string_index;
/* Remove a complete multi-byte character from the end of the
search string. */
do
{
pop_isearch (window, &isearch_string_index,
&dir, &search_result, &start_off);
}
while (isearch_string_index > 0
&& (long) mbrlen (isearch_string + isearch_string_index,
end - isearch_string_index, NULL) <= 0);
isearch_string[isearch_string_index] = '\0';
if (isearch_string_index == 0)
{
/* Don't search for an empty string. Clear the search. */
free_matches (&window->matches);
display_update_one_window (window);
continue;
}
if (search_result != search_success)
{
display_update_one_window (window);
continue;
}
}
}
else if (func == &isearch_forward || func == &isearch_backward)
{
/* If this key invokes an incremental search, then this
means that we will either search again in the same
direction, search again in the reverse direction, or
insert the last search string that was accepted through
incremental searching. */
if (func == &isearch_forward && dir > 0
|| func == &isearch_backward && dir < 0)
{
/* If the user has typed no characters, then insert the
last successful search into the current search string. */
if (isearch_string_index == 0)
{
/* Of course, there must be something to insert. */
if (last_isearch_accepted)
{
if (strlen ((char *) last_isearch_accepted) + 1
>= (unsigned int) isearch_string_size)
isearch_string = (char *)
xrealloc (isearch_string,
isearch_string_size += 10 +
strlen (last_isearch_accepted));
strcpy (isearch_string, last_isearch_accepted);
isearch_string_index = strlen (isearch_string);
}
else
continue;
}
else
{
/* Search again in the same direction. This means start
from a new place if the last search was successful. */
if (search_result == search_success)
{
start_off = window->point;
if (dir < 0)
/* Position before match to avoid finding same match
agin. */
start_off--;
}
}
}
else
{
/* Reverse the direction of the search. */
dir = -dir;
}
}
else if (func == &info_abort_key
&& isearch_states_index && search_result != search_success)
{
/* If C-g pressed, and the search is failing, pop the search
stack back to the last unfailed search. */
terminal_ring_bell ();
while (isearch_states_index && search_result != search_success)
pop_isearch (window, &isearch_string_index, &dir,
&search_result, &start_off);
isearch_string[isearch_string_index] = '\0';
show_isearch_prompt (dir, (unsigned char *) isearch_string,
search_result);
continue;
}
else if (func == &info_abort_key || func == &ea_abort || !func)
{
/* The character is not printable, or it has a function which is
non-null. Exit the search, remembering the search string. */
if (isearch_string_index && func != &info_abort_key)
{
free (last_isearch_accepted);
last_isearch_accepted = xstrdup (isearch_string);
}
if (func == &info_abort_key)
{
if (isearch_states_index)
window_set_state (window, &orig_state);
}
if (!echo_area_is_active)
window_clear_echo_area ();
if (auto_footnotes_p)
info_get_or_remove_footnotes (active_window);
isearch_is_active = 0;
continue;
}
/* Show the new search string in the prompt. */
show_isearch_prompt (dir, (unsigned char *) isearch_string,
search_result);
/* Make the search case-sensitive only if the search string includes
upper-case letters. */
case_sensitive = 0;
for (p = isearch_string; *p; p++)
if (isupper ((unsigned char) *p))
{
case_sensitive = 1;
break;
}
last_search_result = search_result;
window_get_state (window, &mystate);
search_result = info_search_internal (isearch_string,
window, dir, case_sensitive,
&start_off);
/* If this search failed, and we didn't already have a failed search,
then ring the terminal bell. */
if (search_result != search_success
&& last_search_result == search_success)
{
terminal_ring_bell ();
}
else if (search_result == search_success)
{
/* Make sure the match is visible, and update the display. */
if (!strcmp(window->node->fullpath, mystate.fullpath)
&& !strcmp(window->node->nodename, mystate.nodename)
&& mystate.pagetop != window->pagetop)
{
int newtop = window->pagetop;
window->pagetop = mystate.pagetop;
set_window_pagetop (window, newtop);
}
window_adjust_pagetop (window);
/* Call display_update_display to update the window and an automatic
footnotes window if present. */
display_update_display ();
display_cursor_at_point (window);
}
}
/* Free the memory used to remember each search state. */
free_isearch_states ();
/* Alter the window history so that we have added at most one node in the
incremental search, so that going back once with "l" goes to where we
started the incremental search if the match was in a different node. */
{
int i = window->hist_index - 1;
int j = starting_history_entry;
if (i > j)
{
if (!strcmp(window->hist[i]->node->nodename,
window->hist[j]->node->nodename)
&& !strcmp(window->hist[j]->node->fullpath,
window->hist[i]->node->fullpath))
{
/* If we end up at the same node we started at, don't extend
the history at all. */
cleanup_history (window, j, i);
}
else
{
cleanup_history (window, j + 1, i);
}
}
}
/* Perhaps GC some file buffers. */
gc_file_buffers_and_nodes ();
/* After searching, leave the window in the correct state. */
if (!echo_area_is_active)
window_clear_echo_area ();
}
/* **************************************************************** */
/* */
/* Miscellaneous Info Commands */
/* */
/* **************************************************************** */
/* What to do when C-g is pressed in a window. */
DECLARE_INFO_COMMAND (info_abort_key, _("Cancel current operation"))
{
/* If error printing doesn't oridinarily ring the bell, do it now,
since C-g always rings the bell. Otherwise, let the error printer
do it. */
if (!info_error_rings_bell_p)
terminal_ring_bell ();
info_error ("%s", _("Quit"));
info_initialize_numeric_arg ();
}
DECLARE_INFO_COMMAND (info_info_version, _("Display version of Info being run"))
{
window_message_in_echo_area (_("GNU Info version %s"), VERSION);
}
/* Clear the screen and redraw its contents. Given a numeric argument,
move the line the cursor is on to the COUNT'th line of the window. */
DECLARE_INFO_COMMAND (info_redraw_display, _("Redraw the display"))
{
if ((!info_explicit_arg && count == 1) || echo_area_is_active)
{
terminal_clear_screen ();
display_clear_display (the_display);
window_mark_chain (windows, W_UpdateWindow);
display_update_display ();
}
else
{
int desired_line, point_line;
int new_pagetop;
point_line = window_line_of_point (window) - window->pagetop;
if (count < 0)
desired_line = window->height + count;
else
desired_line = count;
if (desired_line < 0)
desired_line = 0;
if (desired_line >= window->height)
desired_line = window->height - 1;
if (desired_line == point_line)
return;
new_pagetop = window->pagetop + (point_line - desired_line);
set_window_pagetop (window, new_pagetop);
}
}
/* Exit from info */
DECLARE_INFO_COMMAND (info_quit, _("Quit using Info"))
{
if (window->next || window->prev)
info_delete_window (window, count);
else
quit_info_immediately = 1;
}
/* **************************************************************** */
/* */
/* Reading Keys and Dispatching on Them */
/* */
/* **************************************************************** */
DECLARE_INFO_COMMAND (info_do_lowercase_version,
_("Run command bound to this key's lowercase variant"))
{} /* Declaration only. */
static void
dispatch_error (int *keyseq)
{
char *rep;
rep = pretty_keyseq (keyseq);
if (!echo_area_is_active)
info_error (_("Unknown command (%s)"), rep);
else
{
char *temp = xmalloc (1 + strlen (rep) + strlen (_("\"%s\" is invalid")));
sprintf (temp, _("'%s' is invalid"), rep);
terminal_ring_bell ();
inform_in_echo_area (temp);
free (temp);
}
}
/* Keeping track of key sequences. */
static int *info_keyseq = NULL;
static int info_keyseq_index = 0;
static int info_keyseq_size = 0;
static int info_keyseq_displayed_p = 0;
/* Initialize the length of the current key sequence. */
void
initialize_keyseq (void)
{
info_keyseq_index = 0;
info_keyseq_displayed_p = 0;
}
/* Add CHARACTER to the current key sequence. */
void
add_char_to_keyseq (int character)
{
if (info_keyseq_index + 2 >= info_keyseq_size)
info_keyseq = xrealloc (info_keyseq,
sizeof (int) * (info_keyseq_size += 10));
info_keyseq[info_keyseq_index++] = character;
info_keyseq[info_keyseq_index] = '\0';
}
/* Display the current value of info_keyseq. If argument EXPECTING is
non-zero, input is expected to be read after the key sequence is
displayed, so add an additional prompting character to the sequence. */
static void
display_info_keyseq (int expecting_future_input)
{
char *rep;
if (!info_keyseq || info_keyseq_index == 0)
return;
rep = pretty_keyseq (info_keyseq);
if (expecting_future_input)
strcat (rep, "-");
if (echo_area_is_active)
inform_in_echo_area (rep);
else
{
window_message_in_echo_area (rep, NULL, NULL);
display_cursor_at_point (active_window);
}
info_keyseq_displayed_p = 1;
}
/* Called by interactive commands to read another key when keys have already
been read as part of the current command (and possibly displayed in status
line with display_info_keyseq). */
int
get_another_input_key (void)
{
int ready = !info_keyseq_displayed_p; /* ready if new and pending key */
/* If there isn't any input currently available, then wait a
moment looking for input. If we don't get it fast enough,
prompt a little bit with the current key sequence. */
if (!info_keyseq_displayed_p)
{
ready = 1;
if (!info_any_buffered_input_p ())
{
#if defined (FD_SET)
struct timeval timer;
fd_set readfds;
FD_ZERO (&readfds);
FD_SET (fileno (info_input_stream), &readfds);
timer.tv_sec = 1;
timer.tv_usec = 750;
ready = select (fileno(info_input_stream)+1, &readfds,
NULL, NULL, &timer);
#else
ready = 0;
#endif /* FD_SET */
}
}
if (!ready)
display_info_keyseq (1);
return get_input_key ();
}
/* Non-zero means that an explicit argument has been passed to this
command, as in C-u C-v. */
int info_explicit_arg = 0;
/* As above, but used when C-u is typed in the echo area to avoid
overwriting this information when "C-u ARG M-x" is typed. */
int ea_explicit_arg = 0;
void info_universal_argument (WINDOW *, int count);
void info_add_digit_to_numeric_arg (WINDOW *, int count);
/* Read a key sequence and look up its command in MAP. Handle C-u style
numeric args, as well as M--, and M-digits. Return argument in COUNT if it
is non-null.
Some commands can be executed directly, in which case null is returned
instead:
If MENU, call info_menu_digit on ACTIVE_WINDOW if a number key was
pressed.
If MOUSE, call mouse_event_handler if a mouse event occurred.
If INSERT, call ea_insert if a printable character was input.
*/
COMMAND_FUNCTION *
read_key_sequence (Keymap map, int menu, int mouse,
int insert, int *count)
{
int key;
int reading_universal_argument = 0;
int numeric_arg = 1, numeric_arg_sign = 1, *which_explicit_arg;
COMMAND_FUNCTION *func;
/* Process the right numeric argument. */
if (!echo_area_is_active)
which_explicit_arg = &info_explicit_arg;
else
which_explicit_arg = &ea_explicit_arg;
*which_explicit_arg = 0;
initialize_keyseq ();
key = get_input_key ();
if (key == KEY_MOUSE)
{
if (mouse)
mouse_event_handler ();
return 0;
}
if (insert
&& (key >= 040 && key < 0200
|| ISO_Latin_p && key >= 0200 && key < 0400))
{
ea_insert (the_echo_area, 1, key);
return 0;
}
add_char_to_keyseq (key);
while (1)
{
int dash_typed = 0, digit_typed = 0;
func = 0;
if (display_was_interrupted_p && !info_any_buffered_input_p ())
display_update_display ();
if (active_window != the_echo_area)
display_cursor_at_point (active_window);
/* If reading a universal argument, both <digit> and M-<digit> help form
the argument. Don't look up the pressed key in the key map. */
if (reading_universal_argument)
{
int k = key;
if (k >= KEYMAP_META_BASE)
k -= KEYMAP_META_BASE;
if (k == '-')
{
dash_typed = 1;
}
else if (isdigit (k))
{
digit_typed = 1;
}
else
/* Note: we may still read another C-u after this. */
reading_universal_argument = 0;
}
if (!dash_typed && !digit_typed && map[key].type == ISFUNC)
{
func = map[key].value.function ? map[key].value.function->func : 0;
if (!func)
{
dispatch_error (info_keyseq);
return 0;
}
}
if (dash_typed || digit_typed || func == &info_add_digit_to_numeric_arg)
{
int k = key;
if (k > KEYMAP_META_BASE)
k -= KEYMAP_META_BASE;
reading_universal_argument = 1;
if (dash_typed || k == '-')
{
if (!*which_explicit_arg)
{
numeric_arg_sign = -1;
numeric_arg = 1;
}
}
else if (digit_typed || isdigit (k))
{
if (*which_explicit_arg)
numeric_arg = numeric_arg * 10 + (k - '0');
else
numeric_arg = (k - '0');
*which_explicit_arg = 1;
}
}
else if (func == info_do_lowercase_version)
{
int lowerkey;
if (key >= KEYMAP_META_BASE)
{
lowerkey = key;
lowerkey -= KEYMAP_META_BASE;
lowerkey = tolower (lowerkey);
lowerkey += KEYMAP_META_BASE;
}
else
lowerkey = tolower (key);
if (lowerkey == key)
{
dispatch_error (info_keyseq);
return 0;
}
key = lowerkey;
continue;
}
else if (func == &info_universal_argument)
{
/* Multiply by 4. */
/* TODO: Maybe C-u should also terminate the universal argument
sequence, as in Emacs. (C-u 6 4 C-u 1 inserts 64 1's.) */
if (!*which_explicit_arg)
numeric_arg *= 4;
reading_universal_argument = 1;
}
else if (menu && func == &info_menu_digit)
{
/* key can either be digit, or M-digit for --vi-keys. */
int k = key;
if (k > KEYMAP_META_BASE)
k -= KEYMAP_META_BASE;
window_clear_echo_area ();
menu_digit (active_window, k);
return 0;
}
else if (insert
&& (func == &ea_possible_completions || func == &ea_complete)
&& !echo_area_completion_items)
{
ea_insert (the_echo_area, 1, key);
return 0;
}
else if (func)
{
/* Don't update the key sequence if we have finished reading a key
sequence in the echo area. This means that a key sequence like
"C-u 2 Left" appears to take effect immediately, instead of there
being a delay while the message is displayed. */
if (!echo_area_is_active && info_keyseq_displayed_p)
display_info_keyseq (0);
if (count)
*count = numeric_arg * numeric_arg_sign;
/* *which_explicit_arg has not been set yet if only a sequence of
C-u's was typed (each of which has multiplied the argument by
four). */
if (*count != 1 && !*which_explicit_arg)
*which_explicit_arg = 1;
return func;
}
else if (map[key].type == ISKMAP)
{
if (map[key].value.keymap != NULL)
map = map[key].value.keymap;
else
{
dispatch_error (info_keyseq);
return 0;
}
if (info_keyseq_displayed_p)
display_info_keyseq (1);
}
do
key = get_another_input_key ();
while (key == KEY_MOUSE);
add_char_to_keyseq (key);
}
return 0;
}
/* Add the current digit to the argument in progress. */
DECLARE_INFO_COMMAND (info_add_digit_to_numeric_arg,
_("Add this digit to the current numeric argument"))
{} /* Declaration only. */
/* C-u, universal argument. Multiply the current argument by 4.
Read a key. If the key has nothing to do with arguments, then
dispatch on it. If the key is the abort character then abort. */
DECLARE_INFO_COMMAND (info_universal_argument,
_("Start (or multiply by 4) the current numeric argument"))
{} /* Declaration only. */
/* Create a default argument. */
void
info_initialize_numeric_arg (void)
{
if (!echo_area_is_active)
{
info_explicit_arg = 0;
}
else
{
ea_explicit_arg = 0;
}
}
|