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
|
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
@c Copyright (C) 1990--1995, 1998--1999, 2001--2025 Free Software
@c Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@node Command Loop
@chapter Command Loop
@cindex editor command loop
@cindex command loop
When you run Emacs, it enters the @dfn{editor command loop} almost
immediately. This loop reads key sequences, executes their definitions,
and displays the results. In this chapter, we describe how these things
are done, and the subroutines that allow Lisp programs to do them.
@menu
* Command Overview:: How the command loop reads commands.
* Defining Commands:: Specifying how a function should read arguments.
* Interactive Call:: Calling a command, so that it will read arguments.
* Distinguish Interactive:: Making a command distinguish interactive calls.
* Command Loop Info:: Variables set by the command loop for you to examine.
* Adjusting Point:: Adjustment of point after a command.
* Input Events:: What input looks like when you read it.
* Reading Input:: How to read input events from the keyboard or mouse.
* Special Events:: Events processed immediately and individually.
* Waiting:: Waiting for user input or elapsed time.
* Quitting:: How @kbd{C-g} works. How to catch or defer quitting.
* Prefix Command Arguments:: How the commands to set prefix args work.
* Recursive Editing:: Entering a recursive edit,
and why you usually shouldn't.
* Disabling Commands:: How the command loop handles disabled commands.
* Command History:: How the command history is set up, and how accessed.
* Keyboard Macros:: How keyboard macros are implemented.
@end menu
@node Command Overview
@section Command Loop Overview
The first thing the command loop must do is read a key sequence,
which is a sequence of input events that translates into a command.
It does this by calling the function @code{read-key-sequence}. Lisp
programs can also call this function (@pxref{Key Sequence Input}).
They can also read input at a lower level with @code{read-key} or
@code{read-event} (@pxref{Reading One Event}), or discard pending
input with @code{discard-input} (@pxref{Event Input Misc}).
The key sequence is translated into a command through the currently
active keymaps. @xref{Key Lookup}, for information on how this is done.
The result should be a keyboard macro or an interactively callable
function. If the key is @kbd{M-x}, then it reads the name of another
command, which it then calls. This is done by the command
@code{execute-extended-command} (@pxref{Interactive Call}).
Prior to executing the command, Emacs runs @code{undo-boundary} to
create an undo boundary. @xref{Maintaining Undo}.
To execute a command, Emacs first reads its arguments by calling
@code{command-execute} (@pxref{Interactive Call}). For commands
written in Lisp, the @code{interactive} specification says how to read
the arguments. This may use the prefix argument (@pxref{Prefix
Command Arguments}) or may read with prompting in the minibuffer
(@pxref{Minibuffers}). For example, the command @code{find-file} has
an @code{interactive} specification which says to read a file name
using the minibuffer. The function body of @code{find-file} does not
use the minibuffer, so if you call @code{find-file} as a function from
Lisp code, you must supply the file name string as an ordinary Lisp
function argument.
If the command is a keyboard macro (i.e., a string or vector),
Emacs executes it using @code{execute-kbd-macro} (@pxref{Keyboard
Macros}).
@defvar pre-command-hook
This normal hook is run by the editor command loop before it executes
each command. At that time, @code{this-command} contains the command
that is about to run, and @code{last-command} describes the previous
command. @xref{Command Loop Info}.
@end defvar
@defvar post-command-hook
This normal hook is run by the editor command loop after it executes
each command (including commands terminated prematurely by quitting or
by errors). At that time, @code{this-command} refers to the command
that just ran, and @code{last-command} refers to the command before
that.
This hook is also run when Emacs first enters the command loop (at
which point @code{this-command} and @code{last-command} are both
@code{nil}).
@end defvar
Quitting is suppressed while running @code{pre-command-hook} and
@code{post-command-hook}. If an error happens while executing one of
these hooks, it does not terminate execution of the hook; instead
the error is silenced and the function in which the error occurred
is removed from the hook.
A request coming into the Emacs server (@pxref{Emacs Server,,,
emacs, The GNU Emacs Manual}) runs these two hooks just as a keyboard
command does.
Note that, when the buffer text includes very long lines, these two
hooks are called as if they were in a @code{with-restriction} form
(@pxref{Narrowing}), with a
@code{long-line-optimizations-in-command-hooks} label and with the
buffer narrowed to a portion around point.
@node Defining Commands
@section Defining Commands
@cindex defining commands
@cindex commands, defining
@cindex functions, making them interactive
@cindex interactive function
The special form @code{interactive} turns a Lisp function into a
command. The @code{interactive} form must be located at top-level in
the function body, usually as the first form in the body; this applies
to both lambda expressions (@pxref{Lambda Expressions}) and
@code{defun} forms (@pxref{Defining Functions}). This form does
nothing during the actual execution of the function; its presence
serves as a flag, telling the Emacs command loop that the function can
be called interactively. The argument of the @code{interactive} form
specifies how the arguments for an interactive call should be read.
@cindex @code{interactive-form} property
Alternatively, an @code{interactive} form may be specified in a
function symbol's @code{interactive-form} property. A non-@code{nil}
value for this property takes precedence over any @code{interactive}
form in the function body itself. This feature is seldom used.
@anchor{The interactive-only property}
@cindex @code{interactive-only} property
Sometimes, a function is only intended to be called interactively,
never directly from Lisp. In that case, give the function a
non-@code{nil} @code{interactive-only} property, either directly
or via @code{declare} (@pxref{Declare Form}). This causes the
byte compiler to warn if the command is called from Lisp. The output
of @code{describe-function} will include similar information.
The value of the property can be: a string, which the byte-compiler
will use directly in its warning (it should end with a period, and not
start with a capital, e.g., @code{"use (system-name) instead."}); @code{t}; any
other symbol, which should be an alternative function to use in Lisp
code.
Generic functions (@pxref{Generic Functions}) cannot be turned into
commands by adding the @code{interactive} form to them.
@menu
* Using Interactive:: General rules for @code{interactive}.
* Interactive Codes:: The standard letter-codes for reading arguments
in various ways.
* Interactive Examples:: Examples of how to read interactive arguments.
* Command Modes:: Specifying that commands are for a specific mode.
* Generic Commands:: Select among command alternatives.
@end menu
@node Using Interactive
@subsection Using @code{interactive}
@cindex arguments, interactive entry
@cindex interactive spec, using
This section describes how to write the @code{interactive} form that
makes a Lisp function an interactively-callable command, and how to
examine a command's @code{interactive} form.
@defspec interactive &optional arg-descriptor &rest modes
This special form declares that a function is a command, and that it
may therefore be called interactively (via @kbd{M-x} or by entering a
key sequence bound to it). The argument @var{arg-descriptor} declares
how to compute the arguments to the command when the command is called
interactively.
A command may be called from Lisp programs like any other function, but
then the caller supplies the arguments and @var{arg-descriptor} has no
effect.
@cindex @code{interactive-form}, symbol property
The @code{interactive} form must be located at top-level in the
function body, or in the function symbol's @code{interactive-form}
property (@pxref{Symbol Properties}). It has its effect because the
command loop looks for it before calling the function
(@pxref{Interactive Call}). Once the function is called, all its body
forms are executed; at this time, if the @code{interactive} form
occurs within the body, the form simply returns @code{nil} without
even evaluating its argument.
The @var{modes} list allows specifying which modes the command is
meant to be used in. See @ref{Command Modes} for more details about
the effect of specifying @var{modes}, and when to use it.
By convention, you should put the @code{interactive} form in the
function body, as the first top-level form. If there is an
@code{interactive} form in both the @code{interactive-form} symbol
property and the function body, the former takes precedence. The
@code{interactive-form} symbol property can be used to add an
interactive form to an existing function, or change how its arguments
are processed interactively, without redefining the function.
@end defspec
There are three possibilities for the argument @var{arg-descriptor}:
@itemize @bullet
@item
It may be omitted or @code{nil}; then the command is called with no
arguments. This leads quickly to an error if the command requires one
or more arguments.
@item
It may be a string; its contents are a sequence of elements separated
by newlines, one for each argument@footnote{Some elements actually
supply two arguments.}. Each element consists of a code character
(@pxref{Interactive Codes}) optionally followed by a prompt (which
some code characters use and some ignore). Here is an example:
@smallexample
(interactive "P\nbFrobnicate buffer: ")
@end smallexample
@noindent
The code letter @samp{P} sets the command's first argument to the raw
command prefix (@pxref{Prefix Command Arguments}). @samp{bFrobnicate
buffer: } prompts the user with @samp{Frobnicate buffer: } to enter
the name of an existing buffer, which becomes the second and final
argument.
The prompt string can use @samp{%} to include previous argument values
(starting with the first argument) in the prompt. This is done using
@code{format-message} (@pxref{Formatting Strings}). For example, here is how
you could read the name of an existing buffer followed by a new name to
give to that buffer:
@smallexample
@group
(interactive "bBuffer to rename: \nsRename buffer %s to: ")
@end group
@end smallexample
@cindex @samp{*} in @code{interactive}
@cindex read-only buffers in interactive
If @samp{*} appears at the beginning of the string, then an error is
signaled if the buffer is read-only.
@cindex @samp{@@} in @code{interactive}
If @samp{@@} appears at the beginning of the string, and if the key
sequence used to invoke the command includes any mouse events, then
the window associated with the first of those events is selected
before the command is run.
@cindex @samp{^} in @code{interactive}
@cindex shift-selection, and @code{interactive} spec
If @samp{^} appears at the beginning of the string, and if the command
was invoked through @dfn{shift-translation}, set the mark and activate
the region temporarily, or extend an already active region, before the
command is run. If the command was invoked without shift-translation,
and the region is temporarily active, deactivate the region before the
command is run. Shift-translation is controlled on the user level by
@code{shift-select-mode}; see @ref{Shift Selection,,, emacs, The GNU
Emacs Manual}.
You can use @samp{*}, @samp{@@}, and @code{^} together; the order does
not matter. Actual reading of arguments is controlled by the rest of
the prompt string (starting with the first character that is not
@samp{*}, @samp{@@}, or @samp{^}).
@item
It may be a Lisp expression that is not a string; then it should be a
form that is evaluated to get a list of arguments to pass to the
command. Usually this form will call various functions to read input
from the user, most often through the minibuffer (@pxref{Minibuffers})
or directly from the keyboard (@pxref{Reading Input}).
Providing point or the mark as an argument value is also common, but
if you do this @emph{and} read input (whether using the minibuffer or
not), be sure to get the integer values of point or the mark after
reading. The current buffer may be receiving subprocess output; if
subprocess output arrives while the command is waiting for input, it
could relocate point and the mark.
Here's an example of what @emph{not} to do:
@smallexample
(interactive
(list (region-beginning) (region-end)
(read-string "Foo: " nil 'my-history)))
@end smallexample
@noindent
Here's how to avoid the problem, by examining point and the mark after
reading the keyboard input:
@smallexample
(interactive
(let ((string (read-string "Foo: " nil 'my-history)))
(list (region-beginning) (region-end) string)))
@end smallexample
@strong{Warning:} the argument values should not include any data
types that can't be printed and then read. Some facilities save
@code{command-history} in a file to be read in the subsequent
sessions; if a command's arguments contain a data type that prints
using @samp{#<@dots{}>} syntax, those facilities won't work.
There are, however, a few exceptions: it is ok to use a limited set of
expressions such as @code{(point)}, @code{(mark)},
@code{(region-beginning)}, and @code{(region-end)}, because Emacs
recognizes them specially and puts the expression (rather than its
value) into the command history. To see whether the expression you
wrote is one of these exceptions, run the command, then examine
@code{(car command-history)}.
@end itemize
@cindex examining the @code{interactive} form
@defun interactive-form function
This function returns the @code{interactive} form of @var{function}.
If @var{function} is an interactively callable function
(@pxref{Interactive Call}), the value is the command's
@code{interactive} form @code{(interactive @var{spec})}, which
specifies how to compute its arguments. Otherwise, the value is
@code{nil}. If @var{function} is a symbol, its function definition is
used.
When called on an OClosure, the work is delegated to the generic
function @code{oclosure-interactive-form}.
@end defun
@defun oclosure-interactive-form function
Just like @code{interactive-form}, this function takes a command and
returns its interactive form. The difference is that it is a generic
function and it is only called when @var{function} is an OClosure
(@pxref{OClosures}). The purpose is to make it possible for some
OClosure types to compute their interactive forms dynamically instead
of carrying it in one of their slots.
This is used for example for @code{kmacro} functions in order to
reduce their memory size, since they all share the same interactive
form. It is also used for @code{advice} functions, where the
interactive form is computed from the interactive forms of its
components, so as to make this computation more lazily and to
correctly adjust the interactive form when one of its component's
is redefined.
@end defun
@node Interactive Codes
@subsection Code Characters for @code{interactive}
@cindex interactive code description
@cindex description for interactive codes
@cindex codes, interactive, description of
@cindex characters for interactive codes
The code character descriptions below contain a number of key words,
defined here as follows:
@table @b
@item Completion
@cindex interactive completion
Provide completion. @key{TAB}, @key{SPC}, and @key{RET} perform name
completion because the argument is read using @code{completing-read}
(@pxref{Completion}). @kbd{?} displays a list of possible completions.
@item Existing
Require the name of an existing object. An invalid name is not
accepted; the commands to exit the minibuffer do not exit if the current
input is not valid.
@item Default
@cindex default argument string
A default value of some sort is used if the user enters no text in the
minibuffer. The default depends on the code character.
@item No I/O
This code letter computes an argument without reading any input.
Therefore, it does not use a prompt string, and any prompt string you
supply is ignored.
Even though the code letter doesn't use a prompt string, you must follow
it with a newline if it is not the last code character in the string.
@item Prompt
A prompt immediately follows the code character. The prompt ends either
with the end of the string or with a newline.
@item Special
This code character is meaningful only at the beginning of the
interactive string, and it does not look for a prompt or a newline.
It is a single, isolated character.
@end table
@cindex reading interactive arguments
Here are the code character descriptions for use with @code{interactive}:
@table @samp
@item *
Signal an error if the current buffer is read-only. Special.
@item @@
Select the window mentioned in the first mouse event in the key
sequence that invoked this command. Special.
@item ^
If the command was invoked through shift-translation, set the mark and
activate the region temporarily, or extend an already active region,
before the command is run. If the command was invoked without
shift-translation, and the region is temporarily active, deactivate
the region before the command is run. Special.
@item a
A function name (i.e., a symbol satisfying @code{fboundp}). Existing,
Completion, Prompt.
@item b
The name of an existing buffer. By default, uses the name of the
current buffer (@pxref{Buffers}). Existing, Completion, Default,
Prompt.
@item B
A buffer name. The buffer need not exist. By default, uses the name of
a recently used buffer other than the current buffer. Completion,
Default, Prompt.
@item c
A character. The cursor does not move into the echo area. Prompt.
@item C
A command name (i.e., a symbol satisfying @code{commandp}). Existing,
Completion, Prompt.
@item d
@cindex position argument
The position of point, as an integer (@pxref{Point}). No I/O.
@item D
A directory. The default is the current default directory of the
current buffer, @code{default-directory} (@pxref{File Name Expansion}).
Existing, Completion, Default, Prompt.
@item e
The first or next non-keyboard event in the key sequence that invoked
the command. More precisely, @samp{e} gets events that are lists, so
you can look at the data in the lists. @xref{Input Events}. No I/O.
You use @samp{e} for mouse events and for special system events
(@pxref{Misc Events}). The event list that the command receives
depends on the event. @xref{Input Events}, which describes the forms
of the list for each event in the corresponding subsections.
You can use @samp{e} more than once in a single command's interactive
specification. If the key sequence that invoked the command has
@var{n} events that are lists, the @var{n}th @samp{e} provides the
@var{n}th such event. Events that are not lists, such as function keys
and @acronym{ASCII} characters, do not count where @samp{e} is concerned.
@item f
A file name of an existing file (@pxref{File Names}). @xref{Reading
File Names}, for details about default values. Existing, Completion,
Default, Prompt.
@item F
A file name. The file need not exist. Completion, Default, Prompt.
@item G
A file name. The file need not exist. If the user enters just a
directory name, then the value is just that directory name, with no
file name within the directory added. Completion, Default, Prompt.
@item i
An irrelevant argument. This code always supplies @code{nil} as
the argument's value. No I/O.
@item k
A key sequence (@pxref{Key Sequences}). This keeps reading events
until a command (or undefined command) is found in the current key
maps. The key sequence argument is represented as a string or vector.
The cursor does not move into the echo area. Prompt.
If @samp{k} reads a key sequence that ends with a down-event, it also
reads and discards the following up-event. You can get access to that
up-event with the @samp{U} code character.
This kind of input is used by commands such as @code{describe-key} and
@code{keymap-global-set}.
@item K
A key sequence on a form that can be used as input to functions like
@code{keymap-set}. This works like @samp{k}, except that it
suppresses, for the last input event in the key sequence, the
conversions that are normally used (when necessary) to convert an
undefined key into a defined one (@pxref{Key Sequence Input}), so this
form is usually used when prompting for a new key sequence that is to
be bound to a command.
@item m
@cindex marker argument
The position of the mark, as an integer. No I/O.
@item M
Arbitrary text, read in the minibuffer using the current buffer's input
method, and returned as a string (@pxref{Input Methods,,, emacs, The GNU
Emacs Manual}). Prompt.
@item n
A number, read with the minibuffer. If the input is not a number, the
user has to try again. @samp{n} never uses the prefix argument.
Prompt.
@item N
The numeric prefix argument; but if there is no prefix argument, read
a number as with @kbd{n}. The value is always a number. @xref{Prefix
Command Arguments}. Prompt.
@item p
@cindex numeric prefix argument usage
The numeric prefix argument. (Note that this @samp{p} is lower case.)
No I/O.
@item P
@cindex raw prefix argument usage
The raw prefix argument. (Note that this @samp{P} is upper case.) No
I/O.
@item r
@cindex region argument
Point and the mark, as two numeric arguments, smallest first. This is
the only code letter that specifies two successive arguments rather than
one. This will signal an error if the mark is not set in the buffer
which is current when the command is invoked. If Transient Mark mode
is turned on (@pxref{The Mark}) --- as it is by default --- and user
option @code{mark-even-if-inactive} is @code{nil}, Emacs will signal
an error even if the mark @emph{is} set, but is inactive. No I/O.
@item s
Arbitrary text, read in the minibuffer and returned as a string
(@pxref{Text from Minibuffer}). Terminate the input with either
@kbd{C-j} or @key{RET}. (@kbd{C-q} may be used to include either of
these characters in the input.) Prompt.
@item S
An interned symbol whose name is read in the minibuffer. Terminate
the input with either @kbd{C-j} or @key{RET}. Other characters that
normally terminate a symbol (e.g., whitespace, parentheses and
brackets) do not do so here. Prompt.
@item U
A key sequence or @code{nil}. Can be used after a @samp{k} or
@samp{K} argument to get the up-event that was discarded (if any)
after @samp{k} or @samp{K} read a down-event. If no up-event has been
discarded, @samp{U} provides @code{nil} as the argument. No I/O.
@item v
A variable declared to be a user option (i.e., satisfying the
predicate @code{custom-variable-p}). This reads the variable using
@code{read-variable}. @xref{Definition of read-variable}. Existing,
Completion, Prompt.
@item x
A Lisp object, specified with its read syntax, terminated with a
@kbd{C-j} or @key{RET}. The object is not evaluated. @xref{Object from
Minibuffer}. Prompt.
@item X
@cindex evaluated expression argument
A Lisp form's value. @samp{X} reads as @samp{x} does, then evaluates
the form so that its value becomes the argument for the command.
Prompt.
@item z
A coding system name (a symbol). If the user enters null input, the
argument value is @code{nil}. @xref{Coding Systems}. Completion,
Existing, Prompt.
@item Z
A coding system name (a symbol)---but only if this command has a prefix
argument. With no prefix argument, @samp{Z} provides @code{nil} as the
argument value. Completion, Existing, Prompt.
@end table
@node Interactive Examples
@subsection Examples of Using @code{interactive}
@cindex examples of using @code{interactive}
@cindex @code{interactive}, examples of using
Here are some examples of @code{interactive}:
@example
@group
(defun foo1 () ; @r{@code{foo1} takes no arguments,}
(interactive) ; @r{just moves forward two words.}
(forward-word 2))
@result{} foo1
@end group
@group
(defun foo2 (n) ; @r{@code{foo2} takes one argument,}
(interactive "^p") ; @r{which is the numeric prefix.}
; @r{under @code{shift-select-mode},}
; @r{will activate or extend region.}
(forward-word (* 2 n)))
@result{} foo2
@end group
@group
(defun foo3 (n) ; @r{@code{foo3} takes one argument,}
(interactive "nCount:") ; @r{which is read with the Minibuffer.}
(forward-word (* 2 n)))
@result{} foo3
@end group
@group
(defun three-b (b1 b2 b3)
"Select three existing buffers.
Put them into three windows, selecting the last one."
@end group
(interactive "bBuffer1:\nbBuffer2:\nbBuffer3:")
(delete-other-windows)
(split-window (selected-window) 8)
(switch-to-buffer b1)
(other-window 1)
(split-window (selected-window) 8)
(switch-to-buffer b2)
(other-window 1)
(switch-to-buffer b3))
@result{} three-b
@group
(three-b "*scratch*" "declarations.texi" "*mail*")
@result{} nil
@end group
@end example
@node Command Modes
@subsection Specifying Modes For Commands
@cindex commands, mode-specific
@cindex commands, specify as mode-specific
@cindex mode-specific commands
Many commands in Emacs are general, and not tied to any specific mode.
For instance, @kbd{M-x kill-region} can be used in pretty much any
mode that has editable text, and commands that display information
(like @kbd{M-x list-buffers}) can be used in pretty much any context.
Many other commands, however, are specifically tied to a mode, and
make no sense outside of that context. For instance, @code{M-x
dired-diff} will just signal an error if used outside of a Dired
buffer.
Emacs therefore has a mechanism for specifying what mode (or modes) a
command ``belongs'' to:
@lisp
(defun dired-diff (...)
...
(interactive "p" dired-mode)
...)
@end lisp
This will mark the command as applicable to @code{dired-mode} only (or
any modes that are derived from @code{dired-mode}). Any number of
modes can be added to the @code{interactive} form.
@vindex read-extended-command-predicate
Specifying modes affects command completion in @kbd{M-S-x}
(@code{execute-extended-command-for-buffer}, @pxref{Interactive
Call}). It may also affect completion in @kbd{M-x}, depending on the
value of @code{read-extended-command-predicate}.
For instance, when using the
@code{command-completion-default-include-p} predicate as the value of
@code{read-extended-command-predicate}, @kbd{M-x} won't list commands
that have been marked as being applicable to a specific mode (unless
you are in a buffer that uses that mode, of course). This goes for
both major and minor modes. (By contrast, @kbd{M-S-x} always omits
inapplicable commands from the completion candidates.)
By default, @code{read-extended-command-predicate} is @code{nil}, and
completion in @kbd{M-x} lists all the commands that match what the
user has typed, whether those commands are or aren't marked as
applicable to the current buffer's mode.
Marking commands to be applicable to a mode will also make @kbd{C-h m}
list these commands (if they aren't bound to any keys).
If using this extended @code{interactive} form isn't convenient
(because the code is supposed to work in older versions of Emacs that
don't support the extended @code{interactive} form), the following
equivalent declaration (@pxref{Declare Form}) can be used instead:
@lisp
(declare (modes dired-mode))
@end lisp
Which commands to tag with modes is to some degree a matter of taste,
but commands that clearly do not work outside of the mode should be
tagged. This includes commands that will signal an error if called
from somewhere else, but also commands that are destructive when
called from an unexpected mode. (This usually includes most of the
commands that are written for special (i.e., non-editing) modes.)
Some commands may be harmless, and ``work'' when called from other
modes, but should still be tagged with a mode if they don't actually
make much sense to use elsewhere. For instance, many special modes
have commands to exit the buffer bound to @kbd{q}, and may not do
anything but issue a message like "Goodbye from this mode" and then
call @code{kill-buffer}. This command will ``work'' from any mode,
but it is highly unlikely that anybody would actually want to use the
command outside the context of this special mode.
Many modes have a set of different commands that start the mode in
different ways (e.g., @code{eww-open-in-new-buffer} and
@code{eww-open-file}). Commands like that should never be tagged as
mode-specific, as they can be issued by the user from pretty much any
context.
@node Generic Commands
@subsection Select among Command Alternatives
@cindex generic commands
@cindex alternative commands, defining
Sometimes it is useful to define a command that serves as a ``generic
dispatcher'' capable of invoking one of a set of commands according to
the user's needs. For example, imagine that you want to define a
command named @samp{open} that can ``open'' and display several
different types of objects. Or you could have a command named
@samp{mua} (which stands for Mail User Agent) that can read and send
email using one of several email backends, such as Rmail, Gnus, or
MH-E. The macro @code{define-alternatives} can be used to define such
@dfn{generic commands}. A generic command is an interactive function
whose implementation can be selected from several alternatives, as a
matter of user preference.
@defmac define-alternatives command &rest customizations
This macro defines the new generic @var{command}, which can have
several alternative implementations. The argument @var{command}
should be an unquoted symbol.
When invoked, the macro creates an interactive Lisp closure
(@pxref{Closures}). When the user runs @w{@kbd{M-x @var{command}
@key{RET}}} for the first time, Emacs asks to select one of the
alternative implementations of @var{command}, offering completion for
the names of these alternatives. These names come from the user
option whose name is @code{@var{command}-alternatives}, which the
macro creates (if it didn't exist before). To be useful, this
variable's value should be an alist whose elements have the form
@w{@code{(@var{alt-name} . @var{alt-func})}}, where @var{alt-name} is
the name of the alternative and @var{alt-func} is the interactive
function to be called if this alternative is selected. When the user
selects an alternative, Emacs remembers the selection, and will
thereafter automatically call that selected alternative without
prompting when the user invokes @kbd{M-x @var{command}} again. To
choose a different alternative, type @w{@kbd{C-u M-x @var{command}
@key{RET}}}--then Emacs will again prompt for one of the alternatives,
and the selection will override the previous one.
The variable @code{@var{command}-alternatives} can be created before
calling @code{define-alternatives}, with the appropriate values;
otherwise the macro creates the variable with a @code{nil} value, and
it should then be populated with the associations describing the
alternatives. Packages that wish to provide their own implementation
of an existing generic command can use @code{autoload} cookies
(@pxref{Autoload}) to add to the alist, for example:
@lisp
;;;###autoload (push '("My name" . my-foo-symbol) foo-alternatives
@end lisp
If the optional argument @var{customizations} is non-@code{nil}, it
should consist of alternating @code{defcustom} keywords (typically
@code{:group} and @code{:version}) and values to add to the definition
of the @code{defcustom} @code{@var{command}-alternatives}.
Here is an example of a simple generic dispatcher command named
@code{open} with 3 alternative implementations:
@example
@group
(define-alternatives open
:group 'files
:version "42.1")
@end group
@group
(setq open-alternatives
'(("file" . find-file)
("directory" . dired)
("hexl" . hexl-find-file)))
@end group
@end example
@end defmac
@node Interactive Call
@section Interactive Call
@cindex interactive call
After the command loop has translated a key sequence into a command,
it invokes that command using the function @code{command-execute}. If
the command is a function, @code{command-execute} calls
@code{call-interactively}, which reads the arguments and calls the
command. You can also call these functions yourself.
Note that the term ``command'', in this context, refers to an
interactively callable function (or function-like object), or a
keyboard macro. It does not refer to the key sequence used to invoke
a command (@pxref{Keymaps}).
@defun commandp object &optional for-call-interactively
This function returns @code{t} if @var{object} is a command.
Otherwise, it returns @code{nil}.
Commands include strings and vectors (which are treated as keyboard
macros), lambda expressions that contain a top-level
@code{interactive} form (@pxref{Using Interactive}), byte-code
function objects made from such lambda expressions, autoload objects
that are declared as interactive (non-@code{nil} fourth argument to
@code{autoload}), and some primitive functions. Also, a symbol is
considered a command if it has a non-@code{nil}
@code{interactive-form} property, or if its function definition
satisfies @code{commandp}.
If @var{for-call-interactively} is non-@code{nil}, then
@code{commandp} returns @code{t} only for objects that
@code{call-interactively} could call---thus, not for keyboard macros.
See @code{documentation} in @ref{Accessing Documentation}, for a
realistic example of using @code{commandp}.
@end defun
@defun call-interactively command &optional record-flag keys
This function calls the interactively callable function @var{command},
providing arguments according to its interactive calling specifications.
It returns whatever @var{command} returns.
If, for instance, you have a function with the following signature:
@example
(defun foo (begin end)
(interactive "r")
...)
@end example
then saying
@example
(call-interactively 'foo)
@end example
will call @code{foo} with the region (@code{point} and @code{mark}) as
the arguments.
An error is signaled if @var{command} is not a function or if it
cannot be called interactively (i.e., is not a command). Note that
keyboard macros (strings and vectors) are not accepted, even though
they are considered commands, because they are not functions. If
@var{command} is a symbol, then @code{call-interactively} uses its
function definition.
@cindex record command history
If @var{record-flag} is non-@code{nil}, then this command and its
arguments are unconditionally added to the list @code{command-history}.
Otherwise, the command is added only if it uses the minibuffer to read
an argument. @xref{Command History}.
The argument @var{keys}, if given, should be a vector which specifies
the sequence of events to supply if the command inquires which events
were used to invoke it. If @var{keys} is omitted or @code{nil}, the
default is the return value of @code{this-command-keys-vector}.
@xref{Definition of this-command-keys-vector}.
@end defun
@defun funcall-interactively function &rest arguments
This function works like @code{funcall} (@pxref{Calling Functions}),
but it makes the call look like an interactive invocation: a call to
@code{called-interactively-p} inside @var{function} will return
@code{t}. If @var{function} is not a command, it is called without
signaling an error.
@end defun
@defun command-execute command &optional record-flag keys special
@cindex keyboard macro execution
This function executes @var{command}. The argument @var{command} must
satisfy the @code{commandp} predicate; i.e., it must be an interactively
callable function or a keyboard macro.
A string or vector as @var{command} is executed with
@code{execute-kbd-macro}. A function is passed to
@code{call-interactively} (see above), along with the
@var{record-flag} and @var{keys} arguments.
If @var{command} is a symbol, its function definition is used in its
place. A symbol with an @code{autoload} definition counts as a
command if it was declared to stand for an interactively callable
function. Such a definition is handled by loading the specified
library and then rechecking the definition of the symbol.
The argument @var{special}, if given, means to ignore the prefix
argument and not clear it. This is used for executing special events
(@pxref{Special Events}).
@end defun
@deffn Command execute-extended-command prefix-argument
@cindex read command name
This function reads a command name from the minibuffer using
@code{completing-read} (@pxref{Completion}). Then it uses
@code{command-execute} to call the specified command. Whatever that
command returns becomes the value of @code{execute-extended-command}.
@cindex execute with prefix argument
If the command asks for a prefix argument, it receives the value
@var{prefix-argument}. If @code{execute-extended-command} is called
interactively, the current raw prefix argument is used for
@var{prefix-argument}, and thus passed on to whatever command is run.
@c !!! Should this be @kindex?
@cindex @kbd{M-x}
@code{execute-extended-command} is the normal definition of @kbd{M-x},
so it uses the string @w{@samp{M-x }} as a prompt. (It would be better
to take the prompt from the events used to invoke
@code{execute-extended-command}, but that is painful to implement.) A
description of the value of the prefix argument, if any, also becomes
part of the prompt.
@example
@group
(execute-extended-command 3)
---------- Buffer: Minibuffer ----------
3 M-x forward-word @key{RET}
---------- Buffer: Minibuffer ----------
@result{} t
@end group
@end example
@vindex read-extended-command-predicate
@findex command-completion-default-include-p
This command heeds the @code{read-extended-command-predicate}
variable, which can filter out commands that are not applicable to the
current major mode (or enabled minor modes). By default, the value of
this variable is @code{nil}, and no commands are filtered out.
However, customizing it to invoke the function
@code{command-completion-default-include-p} will perform
mode-dependent filtering. @code{read-extended-command-predicate} can
be any predicate function; it will be called with two parameters: the
command's symbol and the current buffer. If should return
non-@code{nil} if the command is to be included when completing in
that buffer.
@end deffn
@kindex @kbd{M-X}
@kindex @kbd{M-S-x}
@deffn Command execute-extended-command-for-buffer prefix-argument
This is like @code{execute-extended-command}, but limits the commands
offered for completion to those commands that are of particular
relevance to the current major mode (and enabled minor modes). This
includes commands that are tagged with the modes (@pxref{Using
Interactive}), and also commands that are bound to locally active
keymaps. This command is the normal definition of @kbd{M-S-x}
(that's ``meta shift x'').
@end deffn
Both these commands prompt for a command name, but with different
completion rules. You can toggle between these two modes by using the
@kbd{M-S-x} command while being prompted.
@node Distinguish Interactive
@section Distinguish Interactive Calls
@cindex distinguish interactive calls
@cindex is this call interactive
Sometimes a command should display additional visual feedback (such
as an informative message in the echo area) for interactive calls
only. There are three ways to do this. The recommended way to test
whether the function was called using @code{call-interactively} is to
give it an optional argument @code{print-message} and use the
@code{interactive} spec to make it non-@code{nil} in interactive
calls. Here's an example:
@example
(defun foo (&optional print-message)
(interactive "p")
(when print-message
(message "foo")))
@end example
@noindent
We use @code{"p"} because the numeric prefix argument is never
@code{nil}. Defined in this way, the function does display the
message when called from a keyboard macro.
The above method with the additional argument is usually best,
because it allows callers to say ``treat this call as interactive''.
But you can also do the job by testing @code{called-interactively-p}.
@defun called-interactively-p kind
This function returns @code{t} when the calling function was called
using @code{call-interactively}.
The argument @var{kind} should be either the symbol @code{interactive}
or the symbol @code{any}. If it is @code{interactive}, then
@code{called-interactively-p} returns @code{t} only if the call was
made directly by the user---e.g., if the user typed a key sequence
bound to the calling function, but @emph{not} if the user ran a
keyboard macro that called the function (@pxref{Keyboard Macros}). If
@var{kind} is @code{any}, @code{called-interactively-p} returns
@code{t} for any kind of interactive call, including keyboard macros.
If in doubt, use @code{any}; the only known proper use of
@code{interactive} is if you need to decide whether to display a
helpful message while a function is running.
A function is never considered to be called interactively if it was
called via Lisp evaluation (or with @code{apply} or @code{funcall}).
@end defun
@noindent
Here is an example of using @code{called-interactively-p}:
@example
@group
(defun foo ()
(interactive)
(when (called-interactively-p 'any)
(message "Interactive!")
'foo-called-interactively))
@end group
@group
;; @r{Type @kbd{M-x foo}.}
@print{} Interactive!
@end group
@group
(foo)
@result{} nil
@end group
@end example
@noindent
Here is another example that contrasts direct and indirect calls to
@code{called-interactively-p}.
@example
@group
(defun bar ()
(interactive)
(message "%s" (list (foo) (called-interactively-p 'any))))
@end group
@group
;; @r{Type @kbd{M-x bar}.}
@print{} (nil t)
@end group
@end example
@node Command Loop Info
@section Information from the Command Loop
@cindex command loop variables
The editor command loop sets several Lisp variables to keep status
records for itself and for commands that are run. With the exception of
@code{this-command} and @code{last-command} it's generally a bad idea to
change any of these variables in a Lisp program.
@defvar last-command
This variable records the name of the previous command executed by the
command loop (the one before the current command). Normally the value
is a symbol with a function definition, but this is not guaranteed.
The value is copied from @code{this-command} when a command returns to
the command loop, except when the command has specified a prefix
argument for the following command.
This variable is always local to the current terminal and cannot be
buffer-local. @xref{Multiple Terminals}.
@end defvar
@defvar real-last-command
This variable is set up by Emacs just like @code{last-command},
but never altered by Lisp programs.
@end defvar
@defvar last-repeatable-command
This variable stores the most recently executed command that was not
part of an input event. This is the command @code{repeat} will try to
repeat, @xref{Repeating,,, emacs, The GNU Emacs Manual}.
@end defvar
@defvar this-command
@cindex current command
This variable records the name of the command now being executed by
the editor command loop. Like @code{last-command}, it is normally a symbol
with a function definition.
The command loop sets this variable just before running a command, and
copies its value into @code{last-command} when the command finishes
(unless the command specified a prefix argument for the following
command).
@cindex kill command repetition
Some commands set this variable during their execution, as a flag for
whatever command runs next. In particular, the functions for killing text
set @code{this-command} to @code{kill-region} so that any kill commands
immediately following will know to append the killed text to the
previous kill.
@end defvar
If you do not want a particular command to be recognized as the previous
command in the case where it got an error, you must code that command to
prevent this. One way is to set @code{this-command} to @code{t} at the
beginning of the command, and set @code{this-command} back to its proper
value at the end, like this:
@example
(defun foo (args@dots{})
(interactive @dots{})
(let ((old-this-command this-command))
(setq this-command t)
@r{@dots{}do the work@dots{}}
(setq this-command old-this-command)))
@end example
@noindent
We do not bind @code{this-command} with @code{let} because that would
restore the old value in case of error---a feature of @code{let} which
in this case does precisely what we want to avoid.
@defvar this-original-command
This has the same value as @code{this-command} except when command
remapping occurs (@pxref{Remapping Commands}). In that case,
@code{this-command} gives the command actually run (the result of
remapping), and @code{this-original-command} gives the command that
was specified to run but remapped into another command.
@end defvar
@defvar current-minibuffer-command
This has the same value as @code{this-command}, but is bound
recursively when entering a minibuffer. This variable can be used
from minibuffer hooks and the like to determine what command opened
the current minibuffer session.
@end defvar
@defun this-command-keys
This function returns a string or vector containing the key sequence
that invoked the present command. Any events read by the command
using @code{read-event} without a timeout get tacked on to the end.
However, if the command has called @code{read-key-sequence}, it
returns the last read key sequence. @xref{Key Sequence Input}. The
value is a string if all events in the sequence were characters that
fit in a string. @xref{Input Events}.
@example
@group
(this-command-keys)
;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
@result{} "^X^E"
@end group
@end example
@end defun
@defun this-command-keys-vector
@anchor{Definition of this-command-keys-vector}
Like @code{this-command-keys}, except that it always returns the events
in a vector, so you don't need to deal with the complexities of storing
input events in a string (@pxref{Strings of Events}).
@end defun
@defun clear-this-command-keys &optional keep-record
This function empties out the table of events for
@code{this-command-keys} to return. Unless @var{keep-record} is
non-@code{nil}, it also empties the records that the function
@code{recent-keys} (@pxref{Recording Input}) will subsequently return.
This is useful after reading a password, to prevent the password from
echoing inadvertently as part of the next command in certain cases.
@end defun
@defvar last-nonmenu-event
This variable holds the last input event read as part of a key sequence,
not counting events resulting from mouse menus.
One use of this variable is for telling @code{x-popup-menu} where to pop
up a menu. It is also used internally by @code{y-or-n-p}
(@pxref{Yes-or-No Queries}).
@end defvar
@defvar last-command-event
This variable is set to the last input event that was read by the
command loop as part of a command. The principal use of this variable
is in @code{self-insert-command}, which uses it to decide which
character to insert, and in @code{post-self-insert-hook}
(@pxref{Commands for Insertion}), which uses it to access the
character that was just inserted.
@example
@group
last-command-event
;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
@result{} 5
@end group
@end example
@noindent
The value is 5 because that is the @acronym{ASCII} code for @kbd{C-e}.
@end defvar
@defvar last-event-frame
This variable records which frame the last input event was directed to.
Usually this is the frame that was selected when the event was
generated, but if that frame has redirected input focus to another
frame, the value is the frame to which the event was redirected.
@xref{Input Focus}.
If the last event came from a keyboard macro, the value is @code{macro}.
@end defvar
@cindex input devices
@cindex device names
Input events must come from somewhere; sometimes, that is a keyboard
macro, a signal, or `unread-command-events', but it is usually a
physical input device connected to a computer that is controlled by
the user. Those devices are referred to as @dfn{input devices}, and
Emacs associates each input event with the input device from which it
originated. They are identified by a name that is unique to each
input device.
The ability to determine the precise input device used depends on the
details of each system. When that information is unavailable, Emacs
reports keyboard events as originating from the @samp{"Virtual core
keyboard"}, and other events as originating from the @samp{"Virtual
core pointer"}. (These values are used on every platform because the
X server reports them when detailed device information is not known.)
@defvar last-event-device
This variable records the name of the input device from which the last
input event read was generated. It is @code{nil} if no such device
exists, i.e., the last input event was read from
@code{unread-command-events}, or it came from a keyboard macro.
When the X Input Extension is being used on X Windows, the device name
is a string that is unique to each physical keyboard, pointing device
and touchscreen attached to the X server. Otherwise, it is either the
string @samp{"Virtual core pointer"} or @samp{"Virtual core
keyboard"}, depending on whether the event was generated by a pointing
device (such as a mouse) or a keyboard.
@end defvar
@defun device-class frame name
There are various different types of devices, which can be determined
from their names. This function can be used to determined the correct
type of the device @var{name} for an event originating from
@var{frame}.
The return value is one of the following symbols (``device classes''):
@table @code
@item core-keyboard
The core keyboard; this is means the device is a keyboard-like device,
but no other characteristics are unknown.
@item core-pointer
The core pointer; this means the device is a pointing device, but no
other characteristics are known.
@item mouse
A computer mouse.
@item trackpoint
A trackpoint or joystick (or other similar control.)
@item eraser
The other end of a stylus on a graphics tablet, or a standalone
eraser.
@item pen
The pointed end of a pen on a graphics tablet, a stylus, or some other
similar device.
@item puck
A device that looks like a computer mouse, but reports absolute
coordinates relative to some other surface.
@item power-button
A power button or volume button (or other similar control.)
@item keyboard
A computer keyboard.
@item touchscreen
A computer touchpad.
@item pad
A collection of sensitive buttons, rings, and strips commonly found
around a drawing tablet.
@item touchpad
An indirect touch device such as a touchpad.
@item piano
A musical instrument such as an electronic keyboard.
@item test
A device used by the XTEST extension to report input.
@end table
@end defun
@node Adjusting Point
@section Adjusting Point After Commands
@cindex adjusting point
@cindex invisible/intangible text, and point
@cindex @code{display} property, and point display
@cindex @code{composition} property, and point display
When a sequence of text has the @code{display} or @code{composition}
property, or is invisible, there can be several buffer positions that
result in the cursor being displayed at same place on the screen.
Therefore, after a command finishes and returns to the command loop,
if point is in such a sequence, the command loop normally moves point
to try and make this sequence effectively intangible.
This @emph{point adjustment} follows the following general rules: first, the
adjustment should not change the overall direction of the command;
second if the command moved point, the adjustment tries to ensure the
cursor is also moved; third, Emacs prefers the edges of an intangible
sequence and among those edges it prefers the non sticky ones, such
that newly inserted text is visible.
A command can inhibit this feature by setting the variable
@code{disable-point-adjustment}:
@defvar disable-point-adjustment
If this variable is non-@code{nil} when a command returns to the
command loop, then the command loop does not check for those text
properties, and does not move point out of sequences that have them.
The command loop sets this variable to @code{nil} before each command,
so if a command sets it, the effect applies only to that command.
@end defvar
@defvar global-disable-point-adjustment
If you set this variable to a non-@code{nil} value, the feature of
moving point out of these sequences is completely turned off.
@end defvar
@node Input Events
@section Input Events
@cindex events
@cindex input events
The Emacs command loop reads a sequence of @dfn{input events} that
represent keyboard or mouse activity, or system events sent to Emacs.
The events for keyboard activity are characters or symbols; other
events are always lists. This section describes the representation
and meaning of input events in detail.
@defun eventp object
This function returns non-@code{nil} if @var{object} is an input event
or event type.
Note that any non-@code{nil} symbol might be used as an event or an
event type; @code{eventp} cannot distinguish whether a symbol is
intended by Lisp code to be used as an event.
@end defun
@menu
* Keyboard Events:: Ordinary characters -- keys with symbols on them.
* Function Keys:: Function keys -- keys with names, not symbols.
* Mouse Events:: Overview of mouse events.
* Click Events:: Pushing and releasing a mouse button.
* Drag Events:: Moving the mouse before releasing the button.
* Button-Down Events:: A button was pushed and not yet released.
* Repeat Events:: Double and triple click (or drag, or down).
* Motion Events:: Just moving the mouse, not pushing a button.
* Touchscreen Events:: Tapping and moving fingers on a touchscreen.
* Focus Events:: Moving the mouse between frames.
* Xwidget Events:: Events generated by xwidgets.
* Misc Events:: Other events the system can generate.
* Event Examples:: Examples of the lists for mouse events.
* Classifying Events:: Finding the modifier keys in an event symbol.
Event types.
* Accessing Mouse:: Functions to extract info from mouse events.
* Accessing Scroll:: Functions to get info from scroll bar events.
* Strings of Events:: Special considerations for putting
keyboard character events in a string.
@end menu
@node Keyboard Events
@subsection Keyboard Events
@cindex keyboard events
@cindex character event
There are two kinds of input you can get from the keyboard: ordinary
keys, and function keys. Ordinary keys correspond to (possibly
modified) characters; the events they generate are represented in Lisp
as characters. The event type of a @dfn{character event} is the
character itself (an integer), which might have some modifier bits
set; see @ref{Classifying Events}.
@cindex modifier bits (of input character)
@cindex basic code (of input character)
An input character event consists of a @dfn{basic code} between 0 and
524287, plus any or all of these @dfn{modifier bits}:
@table @asis
@item meta
The
@tex
@math{2^{27}}
@end tex
@ifnottex
2**27
@end ifnottex
bit in the character code indicates a character
typed with the meta key held down.
@item control
The
@tex
@math{2^{26}}
@end tex
@ifnottex
2**26
@end ifnottex
bit in the character code indicates a non-@acronym{ASCII}
control character.
@sc{ascii} control characters such as @kbd{C-a} have special basic
codes of their own, so Emacs needs no special bit to indicate them.
Thus, the code for @kbd{C-a} is just 1.
But if you type a control combination not in @acronym{ASCII}, such as
@kbd{%} with the control key, the numeric value you get is the code
for @kbd{%} plus
@tex
@math{2^{26}}
@end tex
@ifnottex
2**26
@end ifnottex
(assuming the terminal supports non-@acronym{ASCII}
control characters), i.e.@: with the 27th bit set.
@item shift
The
@tex
@math{2^{25}}
@end tex
@ifnottex
2**25
@end ifnottex
bit (the 26th bit) in the character event code indicates an
@acronym{ASCII} control character typed with the shift key held down.
For letters, the basic code itself indicates upper versus lower case;
for digits and punctuation, the shift key selects an entirely different
character with a different basic code. In order to keep within the
@acronym{ASCII} character set whenever possible, Emacs avoids using the
@tex
@math{2^{25}}
@end tex
@ifnottex
2**25
@end ifnottex
bit for those character events.
However, @acronym{ASCII} provides no way to distinguish @kbd{C-A} from
@kbd{C-a}, so Emacs uses the
@tex
@math{2^{25}}
@end tex
@ifnottex
2**25
@end ifnottex
bit in @kbd{C-A} and not in
@kbd{C-a}.
@item hyper
The
@tex
@math{2^{24}}
@end tex
@ifnottex
2**24
@end ifnottex
bit in the character event code indicates a character
typed with the hyper key held down.
@item super
The
@tex
@math{2^{23}}
@end tex
@ifnottex
2**23
@end ifnottex
bit in the character event code indicates a character
typed with the super key held down.
@item alt
The
@tex
@math{2^{22}}
@end tex
@ifnottex
2**22
@end ifnottex
bit in the character event code indicates a character typed with the
alt key held down. (The key labeled @key{Alt} on most keyboards is
actually treated as the meta key, not this.)
@end table
It is best to avoid mentioning specific bit numbers in your program.
To test the modifier bits of a character, use the function
@code{event-modifiers} (@pxref{Classifying Events}). When making key
bindings with @code{keymap-set}, you specify these events using
strings like @samp{C-H-x} instead (for ``control hyper x'')
(@pxref{Changing Key Bindings}).
@node Function Keys
@subsection Function Keys
@cindex function keys
Most keyboards also have @dfn{function keys}---keys that have names or
symbols that are not characters. Function keys are represented in
Emacs Lisp as symbols; the symbol's name is the function key's label,
in lower case. For example, pressing a key labeled @key{F1} generates
an input event represented by the symbol @code{f1}.
The event type of a function key event is the event symbol itself.
@xref{Classifying Events}.
Here are a few special cases in the symbol-naming convention for
function keys:
@table @asis
@item @code{backspace}, @code{tab}, @code{newline}, @code{return}, @code{delete}
These keys correspond to common @acronym{ASCII} control characters that have
special keys on most keyboards.
In @acronym{ASCII}, @kbd{C-i} and @key{TAB} are the same character. If the
terminal can distinguish between them, Emacs conveys the distinction to
Lisp programs by representing the former as the integer 9, and the
latter as the symbol @code{tab}.
Most of the time, it's not useful to distinguish the two. So normally
@code{local-function-key-map} (@pxref{Translation Keymaps}) is set up
to map @code{tab} into 9. Thus, a key binding for character code 9
(the character @kbd{C-i}) also applies to @code{tab}. Likewise for
the other symbols in this group. The function @code{read-char}
likewise converts these events into characters.
In @acronym{ASCII}, @key{BS} is really @kbd{C-h}. But @code{backspace}
converts into the character code 127 (@key{DEL}), not into code 8
(@key{BS}). This is what most users prefer.
@item @code{left}, @code{up}, @code{right}, @code{down}
Cursor arrow keys
@item @code{kp-add}, @code{kp-decimal}, @code{kp-divide}, @dots{}
Keypad keys (to the right of the regular keyboard).
@item @code{kp-0}, @code{kp-1}, @dots{}
Keypad keys with digits.
@item @code{kp-f1}, @code{kp-f2}, @code{kp-f3}, @code{kp-f4}
Keypad PF keys.
@item @code{kp-home}, @code{kp-left}, @code{kp-up}, @code{kp-right}, @code{kp-down}
Keypad arrow keys. Emacs normally translates these into the
corresponding non-keypad keys @code{home}, @code{left}, @dots{}
@item @code{kp-prior}, @code{kp-next}, @code{kp-end}, @code{kp-begin}, @code{kp-insert}, @code{kp-delete}
Additional keypad duplicates of keys ordinarily found elsewhere. Emacs
normally translates these into the like-named non-keypad keys.
@end table
You can use the modifier keys @key{ALT}, @key{CTRL}, @key{HYPER},
@key{META}, @key{SHIFT}, and @key{SUPER} with function keys. The way to
represent them is with prefixes in the symbol name:
@table @samp
@item A-
The alt modifier.
@item C-
The control modifier.
@item H-
The hyper modifier.
@item M-
The meta modifier.
@item S-
The shift modifier.
@item s-
The super modifier.
@end table
Thus, the symbol for the key @key{F3} with @key{META} held down is
@code{M-f3}. When you use more than one prefix, we recommend you
write them in alphabetical order; but the order does not matter in
arguments to the key-binding lookup and modification functions.
@node Mouse Events
@subsection Mouse Events
Emacs supports four kinds of mouse events: click events, drag events,
button-down events, and motion events. All mouse events are represented
as lists. The @sc{car} of the list is the event type; this says which
mouse button was involved, and which modifier keys were used with it.
The event type can also distinguish double or triple button presses
(@pxref{Repeat Events}). The rest of the list elements give position
and time information.
For key lookup, only the event type matters: two events of the same type
necessarily run the same command. The command can access the full
values of these events using the @samp{e} interactive code.
@xref{Interactive Codes}.
A key sequence that starts with a mouse event is read using the keymaps
of the buffer in the window that the mouse was in, not the current
buffer. This does not imply that clicking in a window selects that
window or its buffer---that is entirely under the control of the command
binding of the key sequence.
@node Click Events
@subsection Click Events
@cindex click event
@cindex mouse click event
@cindex mouse wheel event
When the user presses a mouse button and releases it at the same
location, that generates a @dfn{click} event. Depending on how your
window-system reports mouse-wheel events, turning the mouse wheel can
generate either a mouse click or a mouse-wheel event. All mouse event
share the same format:
@example
(@var{event-type} @var{position} @var{click-count})
@end example
@table @asis
@item @var{event-type}
This is a symbol that indicates which mouse button was used. It is
one of the symbols @code{mouse-1}, @code{mouse-2}, @dots{}, where the
buttons are numbered left to right. For mouse-wheel event, it can be
@code{wheel-up} or @code{wheel-down}.
You can also use prefixes @samp{A-}, @samp{C-}, @samp{H-}, @samp{M-},
@samp{S-} and @samp{s-} for modifiers alt, control, hyper, meta, shift
and super, just as you would with function keys.
This symbol also serves as the event type of the event. Key bindings
describe events by their types; thus, if there is a key binding for
@code{mouse-1}, that binding would apply to all events whose
@var{event-type} is @code{mouse-1}.
@item @var{position}
@cindex mouse position list
This is a @dfn{mouse position list} specifying where the mouse event
occurred; see below for details.
@item @var{click-count}
This is the number of rapid repeated presses so far of the same mouse
button or the number of repeated turns of the wheel. @xref{Repeat
Events}.
@end table
To access the contents of a mouse position list in the
@var{position} slot of a mouse event, you should typically use the
functions documented in @ref{Accessing Mouse}.
The explicit format of the list depends on where the event occurred.
For clicks in the text area, mode line, header line, tab line, or in
the fringe or marginal areas, the mouse position list has the form
@example
(@var{window} @var{pos-or-area} (@var{x} . @var{y}) @var{timestamp}
@var{object} @var{text-pos} (@var{col} . @var{row})
@var{image} (@var{dx} . @var{dy}) (@var{width} . @var{height}))
@end example
@noindent
The meanings of these list elements are as follows:
@table @asis
@item @var{window}
The window in which the mouse event occurred.
@item @var{pos-or-area}
The buffer position of the character clicked on in the text area; or,
if the event was outside the text area, the window area where it
occurred. It is one of the symbols @code{mode-line},
@code{header-line}, @code{tab-line}, @code{vertical-line},
@code{left-margin}, @code{right-margin}, @code{left-fringe}, or
@code{right-fringe}.
In one special case, @var{pos-or-area} is a list containing a symbol
(one of the symbols listed above) instead of just the symbol. This
happens after the imaginary prefix keys for the event are registered
by Emacs. @xref{Key Sequence Input}.
@item @var{x}, @var{y}
The relative pixel coordinates of the event. For events in the text
area of a window, the coordinate origin @code{(0 . 0)} is taken to be
the top left corner of the text area. @xref{Window Sizes}. For
events in a mode line, header line or tab line, the coordinate origin
is the top left corner of the window itself. For fringes, margins,
and the vertical border, @var{x} does not have meaningful data.
For fringes and margins, @var{y} is relative to the bottom edge of the
header line. In all cases, the @var{x} and @var{y} coordinates
increase rightward and downward respectively.
@item @var{timestamp}
The time at which the event occurred, as an integer number of
milliseconds since a system-dependent initial time.
@item @var{object}
Either @code{nil}, which means the event occurred on buffer text, or a
cons cell of the form @w{(@var{string} . @var{string-pos})} if there
is a string from a text property or an overlay at the event position.
@table @asis
@item @var{string}
The string which was clicked on, including any properties.
@item @var{string-pos}
The position in the string where the click occurred.
@end table
@item @var{text-pos}
For clicks on a marginal area or on a fringe, this is the buffer
position of the first visible character in the corresponding line in
the window. For clicks on the mode line, the header line or the tab
line, this is @code{nil}. For other events, it is the buffer position
closest to the click.
@item @var{col}, @var{row}
These are the actual column and row coordinate numbers of the glyph
under the @var{x}, @var{y} position. If @var{x} lies beyond the last
column of actual text on its line, @var{col} is reported by adding
fictional extra columns that have the default character width.
Row 0 is taken to be the header line if the window has one, or Row 1
if the window also has the tab line, or the topmost row of
the text area otherwise. Column 0 is taken to be the leftmost
column of the text area for clicks on a window text area, or the
leftmost mode line or header line column for clicks there. For clicks
on fringes or vertical borders, these have no meaningful data. For
clicks on margins, @var{col} is measured from the left edge of the
margin area and @var{row} is measured from the top of the margin area.
@item @var{image}
If there is an image at the click location, this is the image object
as returned by @code{find-image} (@pxref{Defining Images}); otherwise
this is @code{nil}.
@item @var{dx}, @var{dy}
These are the pixel offsets of the click relative to the top left
corner of the @var{object}'s glyph that is the nearest one to the
click. The relevant @var{object}s can be either a buffer, or a string,
or an image, see above. If @var{object} is @code{nil} or a string,
the coordinates are relative to the top left corner of the character
glyph clicked on. Note that the offsets are always zero on text-mode
frames, when @var{object} is @code{nil}, since each glyph there is
considered to have exactly 1x1 pixel dimensions.
@item @var{width}, @var{height}
If the click is on a character, either from buffer text or from
overlay or display string, these are the pixel width and height of
that character's glyph; otherwise they are dimensions of @var{object}
clicked on.
@end table
For clicks on a scroll bar, @var{position} has this form:
@example
(@var{window} @var{area} (@var{portion} . @var{whole}) @var{timestamp} @var{part})
@end example
@table @asis
@item @var{window}
The window whose scroll bar was clicked on.
@item @var{area}
This is the symbol @code{vertical-scroll-bar}.
@item @var{portion}
The number of pixels from the top of the scroll bar to the click
position. On some toolkits, including GTK+, Emacs cannot extract this
data, so the value is always @code{0}.
@item @var{whole}
The total length, in pixels, of the scroll bar. On some toolkits,
including GTK+, Emacs cannot extract this data, so the value is always
@code{0}.
@item @var{timestamp}
The time at which the event occurred, in milliseconds. On some
toolkits, including GTK+, Emacs cannot extract this data, so the value
is always @code{0}.
@item @var{part}
The part of the scroll bar on which the click occurred. It is one of
the symbols @code{handle} (the scroll bar handle), @code{above-handle}
(the area above the handle), @code{below-handle} (the area below the
handle), @code{up} (the up arrow at one end of the scroll bar), or
@code{down} (the down arrow at one end of the scroll bar).
@c The 'top', 'bottom', and 'end-scroll' codes don't seem to be used.
@end table
For clicks on the frame's internal border (@pxref{Frame Layout}),
the frame's tool bar (@pxref{Tool Bar}) or tab bar, @var{position}
has this form:
@example
(@var{frame} @var{part} (@var{X} . @var{Y}) @var{timestamp})
@end example
@table @asis
@item @var{frame}
The frame whose internal border or tool bar or tab bar was clicked on.
@item @var{part}
The part of the frame which was clicked on. This can be one
of the following:
@table @code
@cindex tool-bar mouse events
@item tool-bar
The frame has a tool bar, and the event was in the tool-bar area.
@cindex tab-bar mouse events
@item tab-bar
The frame has a tab bar, and the event was in the tab-bar area.
@item left-edge
@itemx top-edge
@itemx right-edge
@itemx bottom-edge
The click was on the corresponding border at an offset of at least one
canonical character from the border's nearest corner.
@item top-left-corner
@itemx top-right-corner
@itemx bottom-right-corner
@itemx bottom-left-corner
The click was on the corresponding corner of the internal border.
@item nil
The frame does not have an internal border, and the event was not on
the tab bar or the tool bar. This usually happens on text-mode
frames. This can also happen on GUI frames with internal border if
the frame doesn't have its @code{drag-internal-border} parameter
(@pxref{Mouse Dragging Parameters}) set to a non-@code{nil} value.
@end table
@end table
@node Drag Events
@subsection Drag Events
@cindex drag event
@cindex mouse drag event
With Emacs, you can have a drag event without even changing your
clothes. A @dfn{drag event} happens every time the user presses a mouse
button and then moves the mouse to a different character position before
releasing the button. Like all mouse events, drag events are
represented in Lisp as lists. The lists record both the starting mouse
position and the final position, like this:
@example
(@var{event-type} @var{start-position} @var{end-position})
@end example
For a drag event, the name of the symbol @var{event-type} contains the
prefix @samp{drag-}. For example, dragging the mouse with button 2
held down generates a @code{drag-mouse-2} event. The second and third
elements of the event, @var{start-position} and @var{end-position} in
the foregoing illustration, are set to the start and end positions of
the drag as mouse position lists (@pxref{Click Events}). You can
access the second element of any mouse event in the same way.
However, the drag event may end outside the boundaries of the frame
that was initially selected. In that case, the third element's
position list contains that frame in place of a window.
The @samp{drag-} prefix follows the modifier key prefixes such as
@samp{C-} and @samp{M-}.
If @code{read-key-sequence} receives a drag event that has no key
binding, and the corresponding click event does have a binding, it
changes the drag event into a click event at the drag's starting
position. This means that you don't have to distinguish between click
and drag events unless you want to.
@node Button-Down Events
@subsection Button-Down Events
@cindex button-down event
Click and drag events happen when the user releases a mouse button.
They cannot happen earlier, because there is no way to distinguish a
click from a drag until the button is released.
If you want to take action as soon as a button is pressed, you need to
handle @dfn{button-down} events.@footnote{Button-down is the
conservative antithesis of drag.} These occur as soon as a button is
pressed. They are represented by lists that look exactly like click
events (@pxref{Click Events}), except that the @var{event-type} symbol
name contains the prefix @samp{down-}. The @samp{down-} prefix follows
modifier key prefixes such as @samp{C-} and @samp{M-}.
The function @code{read-key-sequence} ignores any button-down events
that don't have command bindings; therefore, the Emacs command loop
ignores them too. This means that you need not worry about defining
button-down events unless you want them to do something. The usual
reason to define a button-down event is so that you can track mouse
motion (by reading motion events) until the button is released.
@xref{Motion Events}.
@node Repeat Events
@subsection Repeat Events
@cindex repeat events
@cindex double-click events
@cindex triple-click events
@cindex mouse events, repeated
If you press the same mouse button more than once in quick succession
without moving the mouse, Emacs generates special @dfn{repeat} mouse
events for the second and subsequent presses.
The most common repeat events are @dfn{double-click} events. Emacs
generates a double-click event when you click a button twice; the event
happens when you release the button (as is normal for all click
events).
The event type of a double-click event contains the prefix
@samp{double-}. Thus, a double click on the second mouse button with
@key{meta} held down comes to the Lisp program as
@code{M-double-mouse-2}. If a double-click event has no binding, the
binding of the corresponding ordinary click event is used to execute
it. Thus, you need not pay attention to the double click feature
unless you really want to.
When the user performs a double click, Emacs generates first an ordinary
click event, and then a double-click event. Therefore, you must design
the command binding of the double click event to assume that the
single-click command has already run. It must produce the desired
results of a double click, starting from the results of a single click.
This is convenient, if the meaning of a double click somehow builds
on the meaning of a single click---which is recommended user interface
design practice for double clicks.
If you click a button, then press it down again and start moving the
mouse with the button held down, then you get a @dfn{double-drag} event
when you ultimately release the button. Its event type contains
@samp{double-drag} instead of just @samp{drag}. If a double-drag event
has no binding, Emacs looks for an alternate binding as if the event
were an ordinary drag.
Before the double-click or double-drag event, Emacs generates a
@dfn{double-down} event when the user presses the button down for the
second time. Its event type contains @samp{double-down} instead of just
@samp{down}. If a double-down event has no binding, Emacs looks for an
alternate binding as if the event were an ordinary button-down event.
If it finds no binding that way either, the double-down event is
ignored.
To summarize, when you click a button and then press it again right
away, Emacs generates a down event and a click event for the first
click, a double-down event when you press the button again, and finally
either a double-click or a double-drag event.
If you click a button twice and then press it again, all in quick
succession, Emacs generates a @dfn{triple-down} event, followed by
either a @dfn{triple-click} or a @dfn{triple-drag}. The event types of
these events contain @samp{triple} instead of @samp{double}. If any
triple event has no binding, Emacs uses the binding that it would use
for the corresponding double event.
If you click a button three or more times and then press it again, the
events for the presses beyond the third are all triple events. Emacs
does not have separate event types for quadruple, quintuple, etc.@:
events. However, you can look at the event list to find out precisely
how many times the button was pressed.
@defun event-click-count event
This function returns the number of consecutive button presses that led
up to @var{event}. If @var{event} is a double-down, double-click or
double-drag event, the value is 2. If @var{event} is a triple event,
the value is 3 or greater. If @var{event} is an ordinary mouse event
(not a repeat event), the value is 1.
@end defun
@defopt double-click-fuzz
To generate repeat events, successive mouse button presses must be at
approximately the same screen position. The value of
@code{double-click-fuzz} specifies the maximum number of pixels the
mouse may be moved (horizontally or vertically) between two successive
clicks to make a double-click.
This variable is also the threshold for motion of the mouse to count
as a drag.
@end defopt
@defopt double-click-time
To generate repeat events, the number of milliseconds between
successive button presses must be less than the value of
@code{double-click-time}. Setting @code{double-click-time} to
@code{nil} disables multi-click detection entirely. Setting it to
@code{t} removes the time limit; Emacs then detects multi-clicks by
position only.
@end defopt
@node Motion Events
@subsection Motion Events
@cindex motion event
@cindex mouse motion events
Emacs sometimes generates @dfn{mouse motion} events to describe motion
of the mouse without any button activity. Mouse motion events are
represented by lists that look like this:
@example
(mouse-movement POSITION)
@end example
@noindent
@var{position} is a mouse position list (@pxref{Click Events}),
specifying the current position of the mouse cursor. As with the
end-position of a drag event, this position list may represent a
location outside the boundaries of the initially selected frame, in
which case the list contains that frame in place of a window.
The @code{track-mouse} macro enables generation of motion
events within its body. Outside of @code{track-mouse} body, Emacs
does not generate events for mere motion of the mouse, and these
events do not appear. @xref{Mouse Tracking}.
@defvar mouse-fine-grained-tracking
When non-@code{nil}, mouse motion events are generated even for very
small movements. Otherwise, motion events are not generated as long
as the mouse cursor remains pointing to the same glyph in the text.
@end defvar
@node Touchscreen Events
@subsection Touchscreen Events
@cindex touchscreen events
@cindex support for touchscreens
Some window systems provide support for input devices that react to
the user's touching the screen and moving fingers while touching the
screen. These input devices are known as touchscreens, and Emacs
reports the events they generate as @dfn{touchscreen events}.
Most individual events generated by a touchscreen only have meaning as
part of a larger sequence of other events: for instance, the simple
operation of tapping the touchscreen involves the user placing and
raising a finger on the touchscreen, and swiping the display to
scroll it involves placing a finger, moving it many times upwards or
downwards, and then raising the finger.
@cindex touch point, in touchscreen events
While a simplistic model consisting of one finger is adequate for taps
and scrolling, more complicated gestures require support for keeping
track of multiple fingers, where the position of each finger is
represented by a @dfn{touch point}. For example, a ``pinch to zoom''
gesture might consist of the user placing two fingers and moving them
individually in opposite directions, where the distance between the
positions of their individual points determine the amount by which to
zoom the display, and the center of an imaginary line between those
positions determines where to pan the display after zooming.
The low-level touchscreen events described below can be used to
implement all the touch sequences described above. In those events,
each point is represented by a cons of an arbitrary number identifying
the point and a mouse position list (@pxref{Click Events}) specifying
the position of the finger when the event occurred.
@table @code
@cindex @code{touchscreen-begin} event
@item (touchscreen-begin @var{point})
This event is sent when @var{point} is created by the user pressing a
finger against the touchscreen.
Imaginary prefix keys are also affixed to these events
@code{read-key-sequence} when they originate on top of a special part
of a frame or window. @xref{Key Sequence Input}.
@cindex @code{touchscreen-update} event
@item (touchscreen-update @var{points})
This event is sent when a point on the touchscreen has changed
position. @var{points} is a list of touch points containing the
up-to-date positions of each touch point currently on the touchscreen.
@cindex @code{touchscreen-end} event
@item (touchscreen-end @var{point} @var{canceled})
This event is sent when @var{point} is no longer present on the
display, because another program took the grab, or because the user
raised the finger from the touchscreen.
@var{canceled} is non-@code{nil} if the touch sequence has been
intercepted by another program (such as the window manager), and Emacs
should undo or avoid any editing commands that would otherwise result
from the touch sequence.
Imaginary prefix keys are also affixed to these events
@code{read-key-sequence} when they originate on top of a special part
of a frame or window.
@end table
If a touchpoint is pressed against the menu bar, then Emacs will not
generate any corresponding @code{touchscreen-begin} or
@code{touchscreen-end} events; instead, the menu bar may be displayed
after @code{touchscreen-end} would have been delivered under other
circumstances.
@cindex mouse emulation from touch screen events
When no command is bound to @code{touchscreen-begin},
@code{touchscreen-end} or @code{touchscreen-update}, Emacs calls a
``key translation function'' (@pxref{Translation Keymaps}) to
translate key sequences containing touch screen events into ordinary
mouse events (@pxref{Mouse Events}.) Since Emacs doesn't support
distinguishing events originating from separate mouse devices, it
assumes that a maximum of two touchpoints are active while translation
takes place, and does not place any guarantees on the results of event
translation when that restriction is overstepped.
Emacs applies two different strategies for translating touch events
into mouse events, contingent on factors such as the commands bound to
keymaps that are active at the location of the
@code{touchscreen-begin} event. If a command is bound to
@code{down-mouse-1} at that location, the initial translation consists
of a single @code{down-mouse-1} event, with subsequent
@code{touchscreen-update} events translated to mouse motion events
(@pxref{Motion Events}), and a final @code{touchscreen-end} event
translated to a @code{mouse-1} or @code{drag-mouse-1} event (unless
the @code{touchscreen-end} event indicates that the touch sequence has
been intercepted by another program.) This is dubbed ``simple
translation'', and produces a simple correspondence between touchpoint
motion and mouse motion.
@cindex @code{ignored-mouse-command}, a symbol property
However, some commands bound to
@code{down-mouse-1}--@code{mouse-drag-region}, for example--either
conflict with defined touch screen gestures (such as ``long-press to
drag''), or with user expectations for touch input, and shouldn't
subject the touch sequence to simple translation. If a command whose
name contains the property (@pxref{Symbol Properties})
@code{ignored-mouse-command} is encountered or there is no command
bound to @code{down-mouse-1}, a more irregular form of translation
takes place: here, Emacs processes touch screen gestures
(@pxref{Touchscreens,,, emacs, The GNU Emacs Manual}) first, and
finally attempts to translate touch screen events into mouse events if
no gesture was detected prior to a closing @code{touchscreen-end}
event (with its @var{canceled} parameter @code{nil}, as with simple
translation) and a command is bound to @code{mouse-1} at the location
of that event. Before generating the @code{mouse-1} event, point is
also set to the location of the @code{touchscreen-end} event, and the
window containing the position of that event is selected, as a
compromise for packages which assume @code{mouse-drag-region} has
already set point to the location of any mouse click and selected the
window where it took place.
To prevent unwanted @code{mouse-1} events arriving after a mouse menu
is dismissed (@pxref{Mouse Menus}), Emacs also avoids simple
translation if @code{down-mouse-1} is bound to a keymap, making it a
prefix key. In lieu of simple translation, it translates the closing
@code{touchscreen-end} to a @code{down-mouse-1} event with the
starting position of the touch sequence, consequently displaying
the mouse menu.
@cindex @code{mouse-1-menu-command}, a symbol property
Since certain commands are also bound to @code{down-mouse-1} for the
purpose of displaying pop-up menus, Emacs additionally behaves as
illustrated in the last paragraph if @code{down-mouse-1} is bound to a
command whose name has the property @code{mouse-1-menu-command}.
@cindex pinch-to-zoom touchscreen gesture translation
When a second touch point is registered as a touch point is already
being translated, gesture translation is terminated, and the distance
from the second touch point (the @dfn{ancillary tool}) to the first is
measured. Subsequent motion from either of those touch points will
yield @code{touchscreen-pinch} events incorporating the ratio formed
by the distance between their new positions and the distance measured
at the outset, as illustrated in the following table.
@cindex touchscreen gesture events
If touch gestures are detected during translation, one of the
following input events may be generated:
@table @code
@cindex @code{touchscreen-scroll} event
@item (touchscreen-scroll @var{window} @var{dx} @var{dy})
If a ``scrolling'' gesture is detected during the translation process,
each subsequent @code{touchscreen-update} event is translated to a
@code{touchscreen-scroll} event, where @var{dx} and @var{dy} specify,
in pixels, the relative motion of the touchpoint from the position of
the @code{touchscreen-begin} event that started the sequence or the
last @code{touchscreen-scroll} event, whichever came later.
@cindex @code{touchscreen-hold} event
@item (touchscreen-hold @var{posn})
If the single active touchpoint remains stationary for more than
@code{touch-screen-delay} seconds after a @code{touchscreen-begin} is
generated, a ``long-press'' gesture is detected during the translation
process, and a @code{touchscreen-hold} event is sent, with @var{posn}
set to a mouse position list containing the position of the
@code{touchscreen-begin} event.
@cindex @code{touchscreen-drag} event
@item (touchscreen-drag @var{posn})
If a ``long-press'' gesture is detected while translating the current
touch sequence or ``drag-to-select'' is being resumed as a result of
the @code{touch-screen-extend-selection} user option, a
@code{touchscreen-drag} event is sent upon each subsequent
@code{touchscreen-update} event with @var{posn} set to the new
position of the touchpoint.
@cindex @code{touchscreen-restart-drag} event
@item (touchscreen-restart-drag @var{posn})
This event is sent upon the start of a touch sequence resulting in the
continuation of a ``drag-to-select'' gesture (subject to the
aforementioned user option) with @var{posn} set to the position list of
the initial @code{touchscreen-begin} event within that touch sequence.
@cindex @code{touchscreen-pinch} event
@item (touchscreen-pinch @var{posn} @var{ratio} @var{pan-x} @var{pan-y} @var{ratio-diff})
This event is delivered upon significant changes to the positions of
either active touch point when an ancillary tool is active.
@var{posn} is a mouse position list for the midpoint of a line drawn
from the ancillary tool to the other touch point being observed.
@var{ratio} is the distance between both touch points being observed
divided by that distance when the ancillary point was first
registered; which is to say, the scale of the ``pinch'' gesture.
@var{pan-x} and @var{pan-y} are the difference between the pixel
position of @var{posn} and this position within the last event
delivered appertaining to this series of touch events, or in the case
that no such event exists, the centerpoint between both touch points
when the ancillary tool was first registered.
@var{ratio-diff} is the difference between this event's ratio and
@var{ratio} in the last event delivered; it is @var{ratio} if no such
event exists.
Such events are sent when the magnitude of the changes they represent
will yield a @var{ratio} which differs by more than @code{0.2} from
that in the previous event, or the sum of @var{pan-x} and @var{pan-y}
will surpass half the frame's character width in pixels (@pxref{Frame
Font}).
@end table
@cindex handling touch screen events
@cindex tap and drag, touch screen gestures
Several functions are provided for Lisp programs that handle touch
screen events. The intended use of the first two functions described
below is from commands bound directly to @code{touchscreen-begin}
events; they allow responding to commonly used touch screen gestures
separately from mouse event translation.
@defun touch-screen-track-tap event &optional update data threshold
This function is used to track a single ``tap'' gesture originating
from the @code{touchscreen-begin} event @var{event}, often used to
set the point or to activate a button. It waits for a
@code{touchscreen-end} event with the same touch identifier to arrive,
at which point it returns @code{t}, signifying the end of the gesture.
If a @code{touchscreen-update} event arrives in the mean time and
contains at least one touchpoint with the same identifier as in
@var{event}, the function @var{update} is called with two arguments,
the list of touchpoints in that @code{touchscreen-update} event, and
@var{data}.
If @var{threshold} is non-@code{nil} and such an event indicates that
the touchpoint represented by @var{event} has moved beyond a threshold
of either @var{threshold} or 10 pixels if it is not a number from the
position of @var{event}, @code{nil} is returned and mouse event
translation is resumed for that touchpoint, so as not to impede the
recognition of any subsequent touchscreen gesture arising from its
sequence.
If any other event arrives in the mean time, @code{nil} is returned.
The caller should not perform any action in that case.
@end defun
@defun touch-screen-track-drag event update &optional data
This function is used to track a single ``drag'' gesture originating
from the @code{touchscreen-begin} event @code{event}.
It behaves like @code{touch-screen-track-tap}, except that it returns
@code{no-drag} and refrains from calling @var{update} if the
touchpoint in @code{event} did not move far enough (by default, 5
pixels from its position in @code{event}) to qualify as an actual
drag.
@end defun
In addition to those two functions, a function is provided for
commands bound to some types of events generated through mouse event
translation to prevent unwanted events from being generated after it
is called.
@defun touch-screen-inhibit-drag
This function inhibits the generation of @code{touchscreen-drag}
events during mouse event translation for the duration of the touch
sequence being translated after it is called. It must be called from
a command which is bound to a @code{touchscreen-hold} or
@code{touchscreen-drag} event, and signals an error otherwise.
Since this function can only be called after a gesture is already
recognized during mouse event translation, no mouse events will be
generated from touch events constituting the previously mentioned
touch sequence after it is called.
@end defun
@node Focus Events
@subsection Focus Events
@cindex focus event
This section talks about both window systems and Emacs frames. When
talking about just ``frames'' or ``windows'', it refers to Emacs frames
and Emacs windows. When talking about window system windows, which are
also Emacs frames, this section always says ``window system window''.
@noindent
Window systems provide general ways for the user to control which window
system window, or Emacs frame, gets keyboard input. This choice of
window system window is called the @dfn{focus}. When the user does
something to switch between Emacs frames, that generates a @dfn{focus
event}. Emacs also generates focus events when using
@var{mouse-autoselect-window} to switch between Emacs windows within
Emacs frames.
A focus event in the middle of a key sequence would garble the
sequence. So Emacs never generates a focus event in the middle of a key
sequence. If the user changes focus in the middle of a key
sequence---that is, after a prefix key---then Emacs reorders the events
so that the focus event comes either before or after the multi-event key
sequence, and not within it.
@subsubheading Focus events for frames
The normal definition of a focus event that switches frames, in the
global keymap, is to select that new frame within Emacs, as the user
would expect. @xref{Input Focus}, which also describes hooks related to
focus events for frames. Focus events for frames are represented in
Lisp as lists that look like this:
@example
(switch-frame @var{new-frame})
@end example
@noindent
where @var{new-frame} is the frame switched to.
Some X window managers are set up so that just moving the mouse into a
frame is enough to set the focus there. Usually, there is no need for a
Lisp program to know about the focus change until some other kind of
input arrives. Emacs generates a focus event only when the user
actually types a keyboard key or presses a mouse button in the new
frame; just moving the mouse between frames does not generate a focus
event.
@subsubheading Focus events for windows
When @var{mouse-autoselect-window} is set, moving the mouse over a new
window within a frame can also switch the selected window. @xref{Mouse
Window Auto-selection}, which describes the behavior for different
values. When the mouse is moved over a new window, a focus event for
switching windows is generated. Focus events for windows are
represented in Lisp as lists that look like this:
@example
(select-window @var{new-window})
@end example
@noindent
where @var{new-window} is the window switched to.
@node Xwidget Events
@subsection Xwidget events
Xwidgets (@pxref{Xwidgets}) can send events to update Lisp programs on
their status. These events are dubbed @code{xwidget-events}, and
contain various data describing the nature of the change.
@table @code
@cindex @code{xwidget-event} event
@item (xwidget-event @var{kind} @var{xwidget} @var{arg})
This event is sent whenever some kind of update occurs in
@var{xwidget}. There are several types of updates, identified by
their @var{kind}.
@cindex xwidget callbacks
It is a special event (@pxref{Special Events}), which should be
handled by adding a callback to an xwidget that is called whenever an
xwidget event for @var{xwidget} is received.
You can add a callback by setting the @code{callback} of an xwidget's
property list, which should be a function that accepts @var{xwidget}
and @var{kind} as arguments.
@table @code
@cindex @code{load-changed} xwidget event
@item load-changed
This xwidget event indicates that the @var{xwidget} has reached a
particular point of the page-loading process. When these events are
sent, @var{arg} will contain a string that further describes the status
of the widget:
@table @samp
@cindex @samp{load-started} in xwidgets
@item load-started
This means the widget has begun a page-loading operation.
@cindex @samp{load-finished} in xwidgets
@item load-finished
This means the @var{xwidget} has finished processing whatever
page-loading operation that it was previously performing.
@cindex @samp{load-redirected} in xwidgets
@item load-redirected
This means the @var{xwidget} has encountered and followed a redirect
during the page-loading operation.
@cindex @samp{load-committed} in xwidgets
@item load-committed
This means the @var{xwidget} has committed to a given URL during the
page-loading operation, i.e.@: the URL is the final URL that will be
rendered by @var{xwidget} during the current page-loading operation.
@end table
@cindex @code{download-callback} xwidget events
@item download-callback
This event indicates that a download of some kind has been completed.
@end table
In the above events, there can be arguments after @var{arg}, which
itself indicates the URL from which the download file was retrieved:
the first argument after @var{arg} indicates the MIME type of the
download, as a string, while the second argument contains the full
file name of the downloaded file.
@table @code
@cindex @code{download-started} xwidget events
@item download-started
This event indicates that a download has been started. In these
events, @var{arg} contains the URL of the file that is currently being
downloaded.
@cindex @code{javascript-callback} xwidget events
@item javascript-callback
This event contains JavaScript callback data. These events are used
internally by @code{xwidget-webkit-execute-script}.
@end table
@cindex @code{xwidget-display-event} event
@item (xwidget-display-event @var{xwidget} @var{source})
This event is sent whenever an xwidget requests that another xwidget
be displayed. @var{xwidget} is the xwidget that should be displayed,
and @var{source} is the xwidget that asked to display @var{xwidget}.
It is also a special event which should be handled through callbacks.
You can add such a callback by setting the @code{display-callback} of
@var{source}'s property list, which should be a function that accepts
@var{xwidget} and @var{source} as arguments.
@var{xwidget}'s buffer will be set to a temporary buffer. When
displaying the widget, care should be taken to replace the buffer with
the buffer in which the xwidget will be displayed, using
@code{set-xwidget-buffer} (@pxref{Xwidgets}).
@end table
@node Misc Events
@subsection Miscellaneous System Events
A few other event types represent occurrences within the system.
@table @code
@cindex @code{text-conversion} event
@item text-conversion
This kind of event is sent @strong{after} a system-wide input method
performs an edit to one or more buffers.
@vindex text-conversion-edits
Once the event is sent, the input method may already have made changes
to multiple buffers inside many different frames. To determine which
buffers have been changed, and what edits have been made to them, use
the variable @code{text-conversion-edits}, which is set prior to each
@code{text-conversion} event being sent; it is a list of the form:
@example
@w{@code{((@var{buffer} @var{beg} @var{end} @var{ephemeral}) ...)}}
@end example
Where @var{ephemeral} is the buffer which was modified, @var{beg} and
@var{end} are markers set to the positions of the edit at the time it
was completed, and @var{ephemeral} is either a string, containing any
text which was inserted (or any text before point which was deleted),
@code{t}, meaning that the edit is a temporary edit made by the input
method, or @code{nil}, meaning that some text was deleted after point.
@vindex text-conversion-style
Whether or not this event is sent depends on the value of the
buffer-local variable @code{text-conversion-style}, which determines
how an input method that wishes to make edits to buffer contents will
behave.
This variable can have one of four values:
@table @code
@item nil
This means that the input method will be disabled entirely, and key
events will be sent instead of text conversion events.
@item action
This means that the input method will be enabled, but @key{RET} will
be sent whenever the input method wants to insert a new line.
@item password
This is largely identical to @code{action}, but also requests an input
method capable of inserting ASCII characters, and instructs it not to
save input in locations from which it might be subsequently retrieved
by features of the input method that cannot handle sensitive
information, such as text suggestions.
@item t
This, or any other value, means that the input method will be enabled
and make edits followed by @code{text-conversion} events.
@end table
@findex set-text-conversion-style
Changes to the value of this variable will only take effect upon the
next redisplay after the buffer becomes the selected buffer of a
frame. If you need to disable text conversion in a way that takes
immediate effect, call the function @code{set-text-conversion-style}
instead. This has the potential to lock up the input method for a
significant amount of time, and should be used with care.
@vindex disable-inhibit-text-conversion
In addition, text conversion is automatically disabled after a prefix
key is read by the command loop or @code{read-key-sequence}. This can
be disabled by setting or binding the variable
@code{disable-inhibit-text-conversion} to a non-@code{nil} value.
@cindex @code{delete-frame} event
@item (delete-frame (@var{frame}))
This kind of event indicates that the user gave the window manager
a command to delete a particular window, which happens to be an Emacs frame.
The standard definition of the @code{delete-frame} event is to delete @var{frame}.
@cindex @code{iconify-frame} event
@item (iconify-frame (@var{frame}))
This kind of event indicates that the user iconified @var{frame} using
the window manager. Its standard definition is @code{ignore}; since the
frame has already been iconified, Emacs has no work to do. The purpose
of this event type is so that you can keep track of such events if you
want to.
@cindex @code{make-frame-visible} event
@item (make-frame-visible (@var{frame}))
This kind of event indicates that the user deiconified @var{frame} using
the window manager. Its standard definition is @code{ignore}; since the
frame has already been made visible, Emacs has no work to do.
@cindex @code{touch-end} event
@item (touch-end (@var{position}))
This kind of event indicates that the user's finger moved off the
mouse wheel or the touchpad. The @var{position} element is a mouse
position list (@pxref{Click Events}), specifying the position of the
mouse cursor when the finger moved off the mouse wheel.
@cindex @code{wheel-up} event
@cindex @code{wheel-down} event
@item (wheel-up @var{position} @var{clicks} @var{lines} @var{pixel-delta})
@itemx (wheel-down @var{position} @var{clicks} @var{lines} @var{pixel-delta})
These events are generated by moving a mouse wheel. The
@var{position} element is a mouse position list (@pxref{Click
Events}), specifying the position of the mouse cursor when the event
occurred.
@vindex mwheel-coalesce-scroll-events
@var{clicks}, if present, is the number of times that the wheel was
moved in quick succession. @xref{Repeat Events}. @var{lines}, if
present and not @code{nil}, is the positive number of screen lines
that should be scrolled (either up, when the event is @code{wheel-up},
or down when the event is @code{wheel-down}). @var{pixel-delta}, if
present, is a cons cell of the form @w{@code{(@var{x} . @var{y})}},
where @var{x} and @var{y} are the numbers of pixels by which to scroll
in each axis, a.k.a.@: @dfn{pixelwise deltas}. Usually, only one of
the two will be non-zero, the other will be either zero or very close
to zero; the larger number indicates the axis to scroll the window.
When the variable @code{mwheel-coalesce-scroll-events} is @code{nil},
the scroll commands ignore the @var{lines} element, even if it's
non-@code{nil}, and use the @var{pixel-delta} data instead; in that
case, the direction of scrolling is determined by the sign of the
pixelwise deltas, and the direction (up or down) implied by the event
kind is ignored.
@cindex pixel-resolution wheel events
You can use these @var{x} and @var{y} pixelwise deltas to determine
how much the mouse wheel has actually moved at pixel resolution. For
example, the pixelwise deltas could be used to scroll the display at
pixel resolution, exactly according to the user's turning the mouse
wheel. This pixelwise scrolling is possible only when
@code{mwheel-coalesce-scroll-events} is @code{nil}, and in general the
@var{pixel-delta} data is not generated when that variable is
non-@code{nil}.
@vindex mouse-wheel-up-event
@vindex mouse-wheel-down-event
The @code{wheel-up} and @code{wheel-down} events are generated only on
some kinds of systems. On other systems, other events like @code{mouse-4} and
@code{mouse-5} are used instead. Portable code should handle both
@code{wheel-up} and @code{wheel-down} events as well as the events
specified in the variables @code{mouse-wheel-up-event} and
@code{mouse-wheel-down-event}, defined in @file{mwheel.el}.
Beware that for historical reasons the @code{mouse-wheel-@emph{up}-event}
is the variable that holds an event that should be handled similarly to
@code{wheel-@emph{down}} and vice versa.
@vindex mouse-wheel-left-event
@vindex mouse-wheel-right-event
The same holds for the horizontal wheel movements which are usually
represented by @code{wheel-left} and @code{wheel-right} events, but
for which portable code should also obey the variables
@code{mouse-wheel-left-event} and @code{mouse-wheel-right-event},
defined in @file{mwheel.el}.
However, some mice also generate other events at the same time as
they're generating these scroll events which may get in the way.
The way to fix this is generally to unbind these events (for instance,
@code{mouse-6} or @code{mouse-7}, but this is very hardware and
operating system dependent).
@cindex @code{pinch} event
@item (pinch @var{position} @var{dx} @var{dy} @var{scale} @var{angle})
This kind of event is generated by the user performing a ``pinch''
gesture by placing two fingers on a touchpad and moving them towards
or away from each other. @var{position} is a mouse position list
(@pxref{Click Events}) that provides the position of the mouse pointer
when the event occurred, @var{dx} is the change in the horizontal
distance between the fingers since the last event in the same sequence,
@var{dy} is the vertical movement of the fingers since the last event
in the same sequence, @var{scale} is the ratio of the current distance
between the fingers to that distance at the start of the sequence, and
@var{angle} is the angular difference in degrees between the direction
of the line connecting the fingers in this event and the direction of
that line in the last event of the same sequence.
As pinch events are only sent at the beginning or during a pinch
sequence, they do not report gestures where the user moves two fingers
on a touchpad in a rotating fashion without pinching the fingers.
All arguments after @var{position} are floating point numbers.
This event is usually sent as part of a sequence, which begins with
the user placing two fingers on the touchpad, and ends with the user
removing those fingers. @var{dx}, @var{dy}, and @var{angle} will be
@code{0.0} in the first event of a sequence; subsequent events will
report non-zero values for these members of the event structure.
@var{dx} and @var{dy} are reported in imaginary relative units, in
which @code{1.0} is the width and height of the touchpad
respectively. They are usually interpreted as being relative to the
size of the object beneath the gesture: image, window, etc.
@cindex @code{preedit-text} event
@item (preedit-text @var{arg})
This event is sent when a system input method tells Emacs to display
some text to indicate to the user what will be inserted. The contents
of @var{arg} are dependent on the window system being used.
On X, @var{arg} is a string describing some text to place behind the
cursor. It can be @code{nil}, which means to remove any text
previously displayed.
On PGTK frames (@pxref{Frames}), @var{arg} is a list of strings with
information about their color and underline attributes. It has the
following form:
@example
@group
((@var{string1}
(ul . @var{underline-color})
(bg . @var{background-color})
(fg . @var{foreground-color}))
(@var{string2}
(ul . @var{underline-color})
(bg . @var{background-color})
(fg . @var{foreground-color}))
@dots{}
)
@end group
@end example
Color information can be omitted, leaving just the text of the
strings. @var{underline-color} can be @code{t}, meaning underlined
text with default underline color, or it can be a string, the name of
the color to draw the underline.
This is a special event (@pxref{Special Events}), which normally
should not be bound by the user to any command. Emacs will typically
display the text contained in the event in an overlay behind point
when it is received.
@cindex @code{drag-n-drop} event
@item (drag-n-drop @var{position} @var{files})
This kind of event is generated when a group of files is
selected in an application outside of Emacs, and then dragged and
dropped onto an Emacs frame.
The element @var{position} is a list describing the position of the
event, in the same format as used in a mouse-click event (@pxref{Click
Events}), and @var{files} is the list of file names that were dragged
and dropped. The usual way to handle this event is by visiting these
files.
This kind of event is generated, at present, only on some kinds of
systems.
@cindex @code{help-echo} event
@item help-echo
This kind of event is generated when a mouse pointer moves onto a
portion of buffer text which has a @code{help-echo} text property.
The generated event has this form:
@example
(help-echo @var{frame} @var{help} @var{window} @var{object} @var{pos})
@end example
@noindent
The precise meaning of the event parameters and the way these
parameters are used to display the help-echo text are described in
@ref{Text help-echo}.
@cindex @code{sigusr1} event
@cindex @code{sigusr2} event
@cindex user signals
@item sigusr1
@itemx sigusr2
These events are generated when the Emacs process receives
the signals @code{SIGUSR1} and @code{SIGUSR2}. They contain no
additional data because signals do not carry additional information.
They can be useful for debugging (@pxref{Error Debugging}).
To catch a user signal, bind the corresponding event to an interactive
command in the @code{special-event-map} (@pxref{Controlling Active Maps}).
The command is called with no arguments, and the specific signal event is
available in @code{last-input-event} (@pxref{Event Input Misc}. For
example:
@smallexample
(defun sigusr-handler ()
(interactive)
(message "Caught signal %S" last-input-event))
(keymap-set special-event-map "<sigusr1>" 'sigusr-handler)
@end smallexample
To test the signal handler, you can make Emacs send a signal to itself:
@smallexample
(signal-process (emacs-pid) 'sigusr1)
@end smallexample
@cindex @code{language-change} event
@item language-change
This kind of event is generated on MS-Windows when the input language
has changed. This typically means that the keyboard keys will send to
Emacs characters from a different language. The generated event has
this form:
@smallexample
(language-change @var{frame} @var{codepage} @var{language-id})
@end smallexample
@noindent
Here @var{frame} is the frame which was current when the input
language changed; @var{codepage} is the new codepage number; and
@var{language-id} is the numerical ID of the new input language. The
coding-system (@pxref{Coding Systems}) that corresponds to
@var{codepage} is @code{cp@var{codepage}} or
@code{windows-@var{codepage}}. To convert @var{language-id} to a
string (e.g., to use it for various language-dependent features, such
as @code{set-language-environment}), use the
@code{w32-get-locale-info} function, like this:
@smallexample
;; Get the abbreviated language name, such as "ENU" for English
(w32-get-locale-info language-id)
;; Get the full English name of the language,
;; such as "English (United States)"
(w32-get-locale-info language-id 4097)
;; Get the full localized name of the language
(w32-get-locale-info language-id t)
@end smallexample
@cindex @code{end-session} event
@item end-session
This event is generated on MS-Windows when the operating system
informs Emacs that the user terminated the interactive session, or
that the system is shutting down. The standard definition of this
event is to invoke the @code{kill-emacs} command (@pxref{Killing
Emacs}) so as to shut down Emacs in an orderly fashion; if there are
unsaved changes, this will produce auto-save files
(@pxref{Auto-Saving}) that the user can use after restarting the
session to restore the unsaved edits.
@end table
If one of these events arrives in the middle of a key sequence---that
is, after a prefix key---then Emacs reorders the events so that this
event comes either before or after the multi-event key sequence, not
within it.
Some of these special events, such as @code{delete-frame}, invoke
Emacs commands by default; others are not bound. If you want to
arrange for a special event to invoke a command, you can do that via
@code{special-event-map}. The command you bind to a function key in
that map can then examine the full event which invoked it in
@code{last-input-event}. @xref{Special Events}.
@node Event Examples
@subsection Event Examples
If the user presses and releases the left mouse button over the same
location, that generates a sequence of events like this:
@smallexample
(down-mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864320))
(mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864180))
@end smallexample
While holding the control key down, the user might hold down the
second mouse button, and drag the mouse from one line to the next.
That produces two events, as shown here:
@smallexample
(C-down-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219))
(C-drag-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219)
(#<window 18 on NEWS> 3510 (0 . 28) -729648))
@end smallexample
While holding down the meta and shift keys, the user might press the
second mouse button on the window's mode line, and then drag the mouse
into another window. That produces a pair of events like these:
@smallexample
(M-S-down-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844))
(M-S-drag-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844)
(#<window 20 on carlton-sanskrit.tex> 161 (33 . 3)
-453816))
@end smallexample
The frame with input focus might not take up the entire screen, and
the user might move the mouse outside the scope of the frame. Inside
the @code{track-mouse} macro, that produces an event like this:
@smallexample
(mouse-movement (#<frame *ielm* 0x102849a30> nil (563 . 205) 532301936))
@end smallexample
@node Classifying Events
@subsection Classifying Events
@cindex event type
@cindex classifying events
Every event has an @dfn{event type}, which classifies the event for
key binding purposes. For a keyboard event, the event type equals the
event value; thus, the event type for a character is the character, and
the event type for a function key symbol is the symbol itself. For
events that are lists, the event type is the symbol in the @sc{car} of
the list. Thus, the event type is always a symbol or a character.
Two events of the same type are equivalent where key bindings are
concerned; thus, they always run the same command. That does not
necessarily mean they do the same things, however, as some commands look
at the whole event to decide what to do. For example, some commands use
the location of a mouse event to decide where in the buffer to act.
Sometimes broader classifications of events are useful. For example,
you might want to ask whether an event involved the @key{META} key,
regardless of which other key or mouse button was used.
The functions @code{event-modifiers} and @code{event-basic-type} are
provided to get such information conveniently.
@defun event-modifiers event
This function returns a list of the modifiers that @var{event} has. The
modifiers are symbols; they include @code{shift}, @code{control},
@code{meta}, @code{alt}, @code{hyper} and @code{super}. In addition,
the modifiers list of a mouse event symbol always contains one of
@code{click}, @code{drag}, and @code{down}. For double or triple
events, it also contains @code{double} or @code{triple}.
The argument @var{event} may be an entire event object, or just an
event type. If @var{event} is a symbol that has never been used in an
event that has been read as input in the current Emacs session, then
@code{event-modifiers} can return @code{nil}, even when @var{event}
actually has modifiers.
Here are some examples:
@example
(event-modifiers ?a)
@result{} nil
(event-modifiers ?A)
@result{} (shift)
(event-modifiers ?\C-a)
@result{} (control)
(event-modifiers ?\C-%)
@result{} (control)
(event-modifiers ?\C-\S-a)
@result{} (control shift)
(event-modifiers 'f5)
@result{} nil
(event-modifiers 's-f5)
@result{} (super)
(event-modifiers 'M-S-f5)
@result{} (meta shift)
(event-modifiers 'mouse-1)
@result{} (click)
(event-modifiers 'down-mouse-1)
@result{} (down)
@end example
The modifiers list for a click event explicitly contains @code{click},
but the event symbol name itself does not contain @samp{click}.
Similarly, the modifiers list for an @acronym{ASCII} control
character, such as @samp{C-a}, contains @code{control}, even though
reading such an event via @code{read-char} will return the value 1
with the control modifier bit removed.
@end defun
@defun event-basic-type event
This function returns the key or mouse button that @var{event}
describes, with all modifiers removed. The @var{event} argument is as
in @code{event-modifiers}. For example:
@example
(event-basic-type ?a)
@result{} 97
(event-basic-type ?A)
@result{} 97
(event-basic-type ?\C-a)
@result{} 97
(event-basic-type ?\C-\S-a)
@result{} 97
(event-basic-type 'f5)
@result{} f5
(event-basic-type 's-f5)
@result{} f5
(event-basic-type 'M-S-f5)
@result{} f5
(event-basic-type 'down-mouse-1)
@result{} mouse-1
@end example
@end defun
@defun mouse-movement-p object
This function returns non-@code{nil} if @var{object} is a mouse movement
event. @xref{Motion Events}.
@end defun
@node Accessing Mouse
@subsection Accessing Mouse Events
@cindex mouse events, data in
@cindex keyboard events, data in
This section describes convenient functions for accessing the data in
a mouse button or motion event. Keyboard event data can be accessed
using the same functions, but data elements that aren't applicable to
keyboard events are zero or @code{nil}.
The following two functions return a mouse position list
(@pxref{Click Events}), specifying the position of a mouse event.
@defun event-start event
This returns the starting position of @var{event}.
If @var{event} is a click or button-down event, this returns the
location of the event. If @var{event} is a drag event, this returns the
drag's starting position.
@end defun
@defun event-end event
This returns the ending position of @var{event}.
If @var{event} is a drag event, this returns the position where the user
released the mouse button. If @var{event} is a click or button-down
event, the value is actually the starting position, which is the only
position such events have.
@end defun
@defun posnp object
This function returns non-@code{nil} if @var{object} is a mouse
position list, in the format documented in @ref{Click Events}); and
@code{nil} otherwise.
@end defun
@cindex mouse position list, accessing
These functions take a mouse position list as argument, and return
various parts of it:
@defun posn-window position
Return the window that @var{position} is in. If @var{position}
represents a location outside the frame where the event was initiated,
return that frame instead.
@end defun
@defun posn-area position
Return the window area recorded in @var{position}. It returns @code{nil}
when the event occurred in the text area of the window; otherwise, it
is a symbol identifying the area in which the event occurred.
@end defun
@defun posn-point position
Return the buffer position in @var{position}. When the event occurred
in the text area of the window, in a marginal area, or on a fringe,
this is an integer specifying a buffer position. Otherwise, the value
is undefined.
@end defun
@defun posn-x-y position
Return the pixel-based x and y coordinates in @var{position}, as a
cons cell @w{@code{(@var{x} . @var{y})}}. These coordinates are
relative to the window given by @code{posn-window}.
This example shows how to convert the window-relative coordinates in
the text area of a window into frame-relative coordinates:
@example
(defun frame-relative-coordinates (position)
"Return frame-relative coordinates from POSITION.
POSITION is assumed to lie in a window text area."
(let* ((x-y (posn-x-y position))
(window (posn-window position))
(edges (window-inside-pixel-edges window)))
(cons (+ (car x-y) (car edges))
(+ (cdr x-y) (cadr edges)))))
@end example
@end defun
@defun posn-col-row position &optional use-window
This function returns a cons cell @w{@code{(@var{col} . @var{row})}},
containing the estimated column and row corresponding to buffer
position described by @var{position}. The return value is given in
units of the frame's default character width and default line height
(including spacing), as computed from the @var{x} and @var{y} values
corresponding to @var{position}. (So, if the actual characters have
non-default sizes, the actual row and column may differ from these
computed values.) If the optional @var{window} argument is
non-@code{nil}, use the default character width in the window
indicated by @var{position} instead of the frame. (This makes a
difference if that window is showing a buffer with a non-default
zooming level, for instance.)
Note that @var{row} is counted from the top of the text area. If the
window given by @var{position} possesses a header line (@pxref{Header
Lines}) or a tab line, they are @emph{not} included in the @var{row}
count.
@end defun
@defun posn-actual-col-row position
Return the actual row and column in @var{position}, as a cons cell
@w{@code{(@var{col} . @var{row})}}. The values are the actual row and
column numbers in the window given by @var{position}. @xref{Click
Events}, for details. The function returns @code{nil} if
@var{position} does not include actual position values; in that case
@code{posn-col-row} can be used to get approximate values.
Note that this function doesn't account for the visual width of
characters on display, like the number of visual columns taken by a
tab character or an image. If you need the coordinates in canonical
character units, use @code{posn-col-row} instead.
@end defun
@defun posn-string position
Return the string object described by @var{position}, either
@code{nil} (which means @var{position} describes buffer text), or a
cons cell @w{@code{(@var{string} . @var{string-pos})}}.
@end defun
@defun posn-image position
Return the image object in @var{position}, either @code{nil} (if
there's no image at @var{position}), or an image spec @w{@code{(image
@dots{})}}.
@end defun
@defun posn-object position
Return the image or string object described by @var{position}, either
@code{nil} (which means @var{position} describes buffer text), an
image @w{@code{(image @dots{})}}, or a cons cell
@w{@code{(@var{string} . @var{string-pos})}}.
@end defun
@defun posn-object-x-y position
Return the pixel-based x and y coordinates relative to the upper left
corner of the object described by @var{position}, as a cons cell
@w{@code{(@var{dx} . @var{dy})}}. If the @var{position} describes
buffer text, return the relative coordinates of the buffer-text character
closest to that position.
@end defun
@defun posn-object-width-height position
Return the pixel width and height of the object described by
@var{position}, as a cons cell @code{(@var{width} . @var{height})}.
If the @var{position} describes a buffer position, return the size of
the character at that position.
@end defun
@cindex timestamp of a mouse event
@defun posn-timestamp position
Return the timestamp in @var{position}. This is the time at which the
event occurred, in milliseconds. Such a timestamp is reported
relative to an arbitrary starting time that varies according to the
window system in use. On the X Window System, for example, it is the
number of milliseconds since the X server was started.
@end defun
These functions compute a position list given particular buffer
position or screen position. You can access the data in this position
list with the functions described above.
@defun posn-at-point &optional pos window
This function returns a position list for position @var{pos} in
@var{window}. @var{pos} defaults to point in @var{window};
@var{window} defaults to the selected window.
@code{posn-at-point} returns @code{nil} if @var{pos} is not visible in
@var{window}.
@end defun
@defun posn-at-x-y x y &optional frame-or-window whole
This function returns position information corresponding to pixel
coordinates @var{x} and @var{y} in a specified frame or window,
@var{frame-or-window}, which defaults to the selected window.
The coordinates @var{x} and @var{y} are relative to the
text area of the selected window.
If @var{whole} is non-@code{nil}, the @var{x} coordinate is relative
to the entire window area including scroll bars, margins and fringes.
@end defun
@defopt mouse-prefer-closest-glyph
If this variable is non-@code{nil}, the @code{posn-point} of a mouse
position list will be set to the position of the glyph whose leftmost
edge is the closest to the mouse click, as opposed to the position of
the glyph underneath the mouse pointer itself. For example, if
@code{posn-at-x-y} is called with @var{x} set to @code{9}, which is
contained within a character of width 10 displayed at column 0, the
point saved within the mouse position list will be @emph{after} that
character, not @emph{before} it.
@end defopt
@node Accessing Scroll
@subsection Accessing Scroll Bar Events
@cindex scroll bar events, data in
These functions are useful for decoding scroll bar events.
@defun scroll-bar-event-ratio event
This function returns the fractional vertical position of a scroll bar
event within the scroll bar. The value is a cons cell
@code{(@var{portion} . @var{whole})} containing two integers whose ratio
is the fractional position.
@end defun
@defun scroll-bar-scale ratio total
This function multiplies (in effect) @var{ratio} by @var{total},
rounding the result to an integer. The argument @var{ratio} is not a
number, but rather a pair @code{(@var{num} . @var{denom})}---typically a
value returned by @code{scroll-bar-event-ratio}.
This function is handy for scaling a position on a scroll bar into a
buffer position. Here's how to do that:
@example
(+ (point-min)
(scroll-bar-scale
(posn-x-y (event-start event))
(- (point-max) (point-min))))
@end example
Recall that scroll bar events have two integers forming a ratio, in place
of a pair of x and y coordinates.
@end defun
@node Strings of Events
@subsection Putting Keyboard Events in Strings
@cindex keyboard events in strings
@cindex strings with keyboard events
In most of the places where strings are used, we conceptualize the
string as containing text characters---the same kind of characters found
in buffers or files. Occasionally Lisp programs use strings that
conceptually contain keyboard characters; for example, they may be key
sequences or keyboard macro definitions. However, storing keyboard
characters in a string is a complex matter, for reasons of historical
compatibility, and it is not always possible.
We recommend that new programs avoid dealing with these complexities
by not storing keyboard events in strings containing control
characters or the like, but instead store them in the common Emacs
format as understood by @code{key-valid-p}.
If you read a key sequence with @code{read-key-sequence-vector} (or
@code{read-key-sequence}), or access a key sequence with
@code{this-command-keys-vector} (or @code{this-command-keys}), you can
transform this to the recommended format by using @code{key-description}.
The complexities stem from the modifier bits that keyboard input
characters can include. Aside from the Meta modifier, none of these
modifier bits can be included in a string, and the Meta modifier is
allowed only in special cases.
The earliest GNU Emacs versions represented meta characters as codes
in the range of 128 to 255. At that time, the basic character codes
ranged from 0 to 127, so all keyboard character codes did fit in a
string. Many Lisp programs used @samp{\M-} in string constants to stand
for meta characters, especially in arguments to @code{define-key} and
similar functions, and key sequences and sequences of events were always
represented as strings.
When we added support for larger basic character codes beyond 127, and
additional modifier bits, we had to change the representation of meta
characters. Now the flag that represents the Meta modifier in a
character is
@tex
@math{2^{27}}
@end tex
@ifnottex
2**27
@end ifnottex
and such numbers cannot be included in a string.
To support programs with @samp{\M-} in string constants, there are
special rules for including certain meta characters in a string.
Here are the rules for interpreting a string as a sequence of input
characters:
@itemize @bullet
@item
If the keyboard character value is in the range of 0 to 127, it can go
in the string unchanged.
@item
The meta variants of those characters, with codes in the range of
@tex
@math{2^{27}}
@end tex
@ifnottex
2**27
@end ifnottex
to
@tex
@math{2^{27} + 127},
@end tex
@ifnottex
2**27+127,
@end ifnottex
can also go in the string, but you must change their
numeric values. You must set the
@tex
@math{2^{7}}
@end tex
@ifnottex
2**7
@end ifnottex
bit instead of the
@tex
@math{2^{27}}
@end tex
@ifnottex
2**27
@end ifnottex
bit, resulting in a value between 128 and 255. Only a unibyte string
can include these codes.
@item
Non-@acronym{ASCII} characters above 256 can be included in a multibyte string.
@item
Other keyboard character events cannot fit in a string. This includes
keyboard events in the range of 128 to 255.
@end itemize
Functions such as @code{read-key-sequence} that construct strings of
keyboard input characters follow these rules: they construct vectors
instead of strings, when the events won't fit in a string.
When you use the read syntax @samp{\M-} in a string, it produces a
code in the range of 128 to 255---the same code that you get if you
modify the corresponding keyboard event to put it in the string. Thus,
meta events in strings work consistently regardless of how they get into
the strings.
However, most programs would do well to avoid these issues by
following the recommendations at the beginning of this section.
@node Reading Input
@section Reading Input
@cindex read input
@cindex keyboard input
The editor command loop reads key sequences using the function
@code{read-key-sequence}, which uses @code{read-event}. These and other
functions for event input are also available for use in Lisp programs.
See also @code{momentary-string-display} in @ref{Temporary Displays},
and @code{sit-for} in @ref{Waiting}. @xref{Terminal Input}, for
functions and variables for controlling terminal input modes and
debugging terminal input.
For higher-level input facilities, see @ref{Minibuffers}.
@menu
* Key Sequence Input:: How to read one key sequence.
* Reading One Event:: How to read just one event.
* Event Mod:: How Emacs modifies events as they are read.
* Invoking the Input Method:: How reading an event uses the input method.
* Quoted Character Input:: Asking the user to specify a character.
* Event Input Misc:: How to reread or throw away input events.
@end menu
@node Key Sequence Input
@subsection Key Sequence Input
@cindex key sequence input
The command loop reads input a key sequence at a time, by calling
@code{read-key-sequence}. Lisp programs can also call this function;
for example, @code{describe-key} uses it to read the key to describe.
@defun read-key-sequence prompt &optional continue-echo dont-downcase-last switch-frame-ok command-loop disable-text-conversion
This function reads a key sequence and returns it as a string or
vector. It keeps reading events until it has accumulated a complete key
sequence; that is, enough to specify a non-prefix command using the
currently active keymaps. (Remember that a key sequence that starts
with a mouse event is read using the keymaps of the buffer in the
window that the mouse was in, not the current buffer.)
If the events are all characters and all can fit in a string, then
@code{read-key-sequence} returns a string (@pxref{Strings of Events}).
Otherwise, it returns a vector, since a vector can hold all kinds of
events---characters, symbols, and lists. The elements of the string or
vector are the events in the key sequence.
Reading a key sequence includes translating the events in various
ways. @xref{Translation Keymaps}.
The argument @var{prompt} is either a string to be displayed in the
echo area as a prompt, or @code{nil}, meaning not to display a prompt.
The argument @var{continue-echo}, if non-@code{nil}, means to echo
this key as a continuation of the previous key.
Normally any upper case event is converted to lower case if the
original event is undefined and the lower case equivalent is defined.
The argument @var{dont-downcase-last}, if non-@code{nil}, means do not
convert the last event to lower case. This is appropriate for reading
a key sequence to be defined.
The argument @var{switch-frame-ok}, if non-@code{nil}, means that this
function should process a @code{switch-frame} event if the user
switches frames before typing anything. If the user switches frames
in the middle of a key sequence, or at the start of the sequence but
@var{switch-frame-ok} is @code{nil}, then the event will be put off
until after the current key sequence.
The argument @var{command-loop}, if non-@code{nil}, means that this
key sequence is being read by something that will read commands one
after another. It should be @code{nil} if the caller will read just
one key sequence.
The argument @var{disable-text-conversion}, if non-@code{nil}, means
that system input methods will not directly perform edits to buffer
text while this key sequence is being read; user input will always
generated individual key events instead. @xref{Misc Events}, for more
about text conversion.
In the following example, Emacs displays the prompt @samp{?} in the
echo area, and then the user types @kbd{C-x C-f}.
@example
(read-key-sequence "?")
@group
---------- Echo Area ----------
?@kbd{C-x C-f}
---------- Echo Area ----------
@result{} "^X^F"
@end group
@end example
The function @code{read-key-sequence} suppresses quitting: @kbd{C-g}
typed while reading with this function works like any other character,
and does not set @code{quit-flag}. @xref{Quitting}.
@end defun
@defun read-key-sequence-vector prompt &optional continue-echo dont-downcase-last switch-frame-ok command-loop disable-text-conversion
This is like @code{read-key-sequence} except that it always
returns the key sequence as a vector, never as a string.
@xref{Strings of Events}.
@end defun
@cindex upper case key sequence
@cindex downcasing in @code{lookup-key}
@cindex shift-translation
@vindex translate-upper-case-key-bindings
If an input character is upper-case (or has the shift modifier) and
has no key binding, but its lower-case equivalent has one, then
@code{read-key-sequence} converts the character to lower case. (This
behavior can be disabled by setting the
@code{translate-upper-case-key-bindings} user option to @code{nil}.)
Note that @code{lookup-key} does not perform case conversion in this
way.
@vindex this-command-keys-shift-translated
When reading input results in such a @dfn{shift-translation}, Emacs
sets the variable @code{this-command-keys-shift-translated} to a
non-@code{nil} value. Lisp programs can examine this variable if they
need to modify their behavior when invoked by shift-translated keys.
For example, the function @code{handle-shift-selection} examines the
value of this variable to determine how to activate or deactivate the
region (@pxref{The Mark, handle-shift-selection}).
The function @code{read-key-sequence} also transforms some mouse events.
It converts unbound drag events into click events, and discards unbound
button-down events entirely. It also reshuffles focus events and
miscellaneous window events so that they never appear in a key sequence
with any other events.
@cindex @code{tab-line}, prefix key
@cindex @code{header-line}, prefix key
@cindex @code{mode-line}, prefix key
@cindex @code{vertical-line}, prefix key
@cindex @code{horizontal-scroll-bar}, prefix key
@cindex @code{vertical-scroll-bar}, prefix key
@cindex @code{menu-bar}, prefix key
@cindex @code{tab-bar}, prefix key
@cindex @code{left-margin}, prefix key
@cindex @code{right-margin}, prefix key
@cindex @code{left-fringe}, prefix key
@cindex @code{right-fringe}, prefix key
@cindex @code{right-divider}, prefix key
@cindex @code{bottom-divider}, prefix key
@cindex mouse events, in special parts of window or frame
@cindex touch screen events, in special parts of window or frame
When mouse or @code{touchscreen-begin} and @code{touchscreen-end}
events occur in special parts of a window or frame, such as a mode
line or a scroll bar, the event type shows nothing special---it is the
same symbol that would normally represent that combination of mouse
button and modifier keys. The information about the window part is
kept elsewhere in the event---in the coordinates. But
@code{read-key-sequence} translates this information into imaginary
prefix keys, all of which are symbols: @code{tab-line},
@code{header-line}, @code{horizontal-scroll-bar}, @code{menu-bar},
@code{tab-bar}, @code{mode-line}, @code{vertical-line},
@code{vertical-scroll-bar}, @code{left-margin}, @code{right-margin},
@code{left-fringe}, @code{right-fringe}, @code{right-divider}, and
@code{bottom-divider}. You can define meanings for mouse clicks in
special window parts by defining key sequences using these imaginary
prefix keys.
For example, if you call @code{read-key-sequence} and then click the
mouse on the window's mode line, you get two events, like this:
@example
(read-key-sequence "Click on the mode line: ")
@result{} [mode-line
(mouse-1
(#<window 6 on NEWS> mode-line
(40 . 63) 5959987))]
@end example
@defvar num-input-keys
This variable's value is the number of key sequences processed so far in
this Emacs session. This includes key sequences read from the terminal
and key sequences read from keyboard macros being executed.
@end defvar
@node Reading One Event
@subsection Reading One Event
@cindex reading a single event
@cindex event, reading only one
The lowest level functions for command input are @code{read-event},
@code{read-char}, and @code{read-char-exclusive}.
If you need a function to read a character using the minibuffer, use
@code{read-char-from-minibuffer} (@pxref{Multiple Queries}).
@defun read-event &optional prompt inherit-input-method seconds
This function reads and returns the next event of command input,
waiting if necessary until an event is available.
The returned event may come directly from the user, or from a keyboard
macro. It is not decoded by the keyboard's input coding system
(@pxref{Terminal I/O Encoding}).
If the optional argument @var{prompt} is non-@code{nil}, it should be
a string to display in the echo area as a prompt. If @var{prompt} is
@code{nil} or the string @samp{""}, @code{read-event} does not display
any message to indicate it is waiting for input; instead, it prompts
by echoing: it displays descriptions of the events that led to or were
read by the current command. @xref{The Echo Area}.
If @var{inherit-input-method} is non-@code{nil}, then the current input
method (if any) is employed to make it possible to enter a
non-@acronym{ASCII} character. Otherwise, input method handling is disabled
for reading this event.
If @code{cursor-in-echo-area} is non-@code{nil}, then @code{read-event}
moves the cursor temporarily to the echo area, to the end of any message
displayed there. Otherwise @code{read-event} does not move the cursor.
If @var{seconds} is non-@code{nil}, it should be a number specifying
the maximum time to wait for input, in seconds. If no input arrives
within that time, @code{read-event} stops waiting and returns
@code{nil}. A floating point @var{seconds} means to wait
for a fractional number of seconds. Some systems support only a whole
number of seconds; on these systems, @var{seconds} is rounded down.
If @var{seconds} is @code{nil}, @code{read-event} waits as long as
necessary for input to arrive.
If @var{seconds} is @code{nil}, Emacs is considered idle while waiting
for user input to arrive. Idle timers---those created with
@code{run-with-idle-timer} (@pxref{Idle Timers})---can run during this
period. However, if @var{seconds} is non-@code{nil}, the state of
idleness remains unchanged. If Emacs is non-idle when
@code{read-event} is called, it remains non-idle throughout the
operation of @code{read-event}; if Emacs is idle (which can happen if
the call happens inside an idle timer), it remains idle.
If @code{read-event} gets an event that is defined as a help character,
then in some cases @code{read-event} processes the event directly without
returning. @xref{Help Functions}. Certain other events, called
@dfn{special events}, are also processed directly within
@code{read-event} (@pxref{Special Events}).
Here is what happens if you call @code{read-event} and then press the
right-arrow function key:
@example
@group
(read-event)
@result{} right
@end group
@end example
@end defun
@defun read-char &optional prompt inherit-input-method seconds
This function reads and returns a character input event. If the
user generates an event which is not a character (i.e., a mouse click or
function key event), @code{read-char} signals an error. The arguments
work as in @code{read-event}.
If the event has modifiers, Emacs attempts to resolve them and return
the code of the corresponding character. For example, if the user
types @kbd{C-a}, the function returns 1, which is the @acronym{ASCII}
code of the @samp{C-a} character. If some of the modifiers cannot be
reflected in the character code, @code{read-char} leaves the
unresolved modifier bits set in the returned event. For example, if
the user types @kbd{C-M-a}, the function returns 134217729, 8000001 in
hex, i.e.@: @samp{C-a} with the Meta modifier bit set. This value is
not a valid character code: it fails the @code{characterp} test
(@pxref{Character Codes}). Use @code{event-basic-type}
(@pxref{Classifying Events}) to recover the character code with the
modifier bits removed; use @code{event-modifiers} to test for
modifiers in the character event returned by @code{read-char}.
In the first example below, the user types the character @kbd{1}
(@acronym{ASCII} code 49). The second example shows a keyboard macro
definition that calls @code{read-char} from the minibuffer using
@code{eval-expression}. @code{read-char} reads the keyboard macro's
very next character, which is @kbd{1}. Then @code{eval-expression}
displays its return value in the echo area.
@example
@group
(read-char)
@result{} 49
@end group
@group
;; @r{We assume here you use @kbd{M-:} to evaluate this.}
(symbol-function 'foo)
@result{} "^[:(read-char)^M1"
@end group
@group
(execute-kbd-macro 'foo)
@print{} 49
@result{} nil
@end group
@end example
@end defun
@defun read-char-exclusive &optional prompt inherit-input-method seconds
This function reads and returns a character input event. If the
user generates an event which is not a character event,
@code{read-char-exclusive} ignores it and reads another event, until it
gets a character. The arguments work as in @code{read-event}. The
returned value may include modifier bits, as with @code{read-char}.
@end defun
None of the above functions suppress quitting.
@defvar num-nonmacro-input-events
This variable holds the total number of input events received so far
from the terminal---not counting those generated by keyboard macros.
@end defvar
We emphasize that, unlike @code{read-key-sequence}, the functions
@code{read-event}, @code{read-char}, and @code{read-char-exclusive} do
not perform the translations described in @ref{Translation Keymaps}.
If you wish to read a single key taking these translations into
account (for example, to read @ref{Function Keys} in a terminal or
@ref{Mouse Events} from @code{xterm-mouse-mode}), use the function
@code{read-key}:
@defun read-key &optional prompt disable-fallbacks
This function reads a single key. It is intermediate between
@code{read-key-sequence} and @code{read-event}. Unlike the former, it
reads a single key, not a key sequence. Unlike the latter, it does
not return a raw event, but decodes and translates the user input
according to @code{input-decode-map}, @code{local-function-key-map},
and @code{key-translation-map} (@pxref{Translation Keymaps}).
The argument @var{prompt} is either a string to be displayed in the
echo area as a prompt, or @code{nil}, meaning not to display a prompt.
If argument @var{disable-fallbacks} is non-@code{nil} then the usual
fallback logic for unbound keys in @code{read-key-sequence} is not
applied. This means that mouse button-down and multi-click events
will not be discarded and @code{local-function-key-map} and
@code{key-translation-map} will not get applied. If @code{nil} or
unspecified, the only fallback disabled is downcasing of the last
event.
@end defun
@vindex read-char-choice-use-read-key
@defun read-char-choice prompt chars &optional inhibit-quit
This function uses @code{read-from-minibuffer} to read and return a
single character that is a member of @var{chars}, which should be a
list of single characters. It discards any input characters that are
not members of @var{chars}, and shows a message to that effect.
The optional argument @var{inhibit-quit} is by default ignored, but if
the variable @code{read-char-choice-use-read-key} is non-@code{nil},
this function uses @code{read-key} instead of
@code{read-from-minibuffer}, and in that case @var{inhibit-quit}
non-@code{nil} means ignore keyboard-quit events while waiting for
valid input. In addition, if @code{read-char-choice-use-read-key} is
non-@code{nil}, binding @code{help-form} (@pxref{Help Functions}) to a
non-@code{nil} value while calling this function causes it to evaluate
@code{help-form} and display the result when the user presses
@code{help-char}; it then continues to wait for a valid input
character, or for keyboard-quit.
@end defun
@defun read-multiple-choice prompt choices &optional help-string show-help long-form
Ask user a multiple choice question. @var{prompt} should be a string
that will be displayed as the prompt.
@var{choices} is an alist where the first element in each entry is a
character to be entered, the second element is a short name for the
entry to be displayed while prompting (if there's room, it might be
shortened), and the third, optional entry is a longer explanation that
will be displayed in a help buffer if the user requests more help.
If optional argument @var{help-string} is non-@code{nil}, it should be
a string with a more detailed description of all choices. It will be
displayed in a help buffer instead of the default auto-generated
description when the user types @kbd{?}.
If optional argument @var{show-help} is non-@code{nil}, the help
buffer will be displayed immediately, before any user input. If it is
a string, use it as the name of the help buffer.
If optional argument @var{long-form} is non-@code{nil}, the user
will have to type in long-form answers (using @code{completing-read})
instead of hitting a single key. The answers must be among the second
elements of the values in the @var{choices} list.
The return value is the matching value from @var{choices}.
@lisp
(read-multiple-choice
"Continue connecting?"
'((?a "always" "Accept certificate for this and future sessions.")
(?s "session only" "Accept certificate this session only.")
(?n "no" "Refuse to use certificate, close connection.")))
@end lisp
The @code{read-multiple-choice-face} face is used to highlight the
matching characters in the name string on graphical terminals.
@end defun
@node Event Mod
@subsection Modifying and Translating Input Events
@cindex modifiers of events
@cindex translating input events
@cindex event translation
Emacs modifies every event it reads according to
@code{extra-keyboard-modifiers}, then translates it through
@code{keyboard-translate-table} (if applicable), before returning it
from @code{read-event}.
@defvar extra-keyboard-modifiers
This variable lets Lisp programs ``press'' the modifier keys on the
keyboard. The value is a character. Only the modifiers of the
character matter. Each time the user types a keyboard key, it is
altered as if those modifier keys were held down. For instance, if
you bind @code{extra-keyboard-modifiers} to @code{?\C-\M-a}, then all
keyboard input characters typed during the scope of the binding will
have the control and meta modifiers applied to them. The character
@code{?\C-@@}, equivalent to the integer 0, does not count as a control
character for this purpose, but as a character with no modifiers.
Thus, setting @code{extra-keyboard-modifiers} to zero cancels any
modification.
When using a window system, the program can press any of the
modifier keys in this way. Otherwise, only the @key{CTL} and @key{META}
keys can be virtually pressed.
Note that this variable applies only to events that really come from
the keyboard, and has no effect on mouse events or any other events.
@end defvar
@defvar keyboard-translate-table
This terminal-local variable is the translate table for keyboard
characters. It lets you reshuffle the keys on the keyboard without
changing any command bindings. Its value is normally a char-table, or
else @code{nil}. (It can also be a string or vector, but this is
considered obsolete.)
If @code{keyboard-translate-table} is a char-table
(@pxref{Char-Tables}), then each character read from the keyboard is
looked up in this char-table. If the value found there is
non-@code{nil}, then it is used instead of the actual input character.
Note that this translation is the first thing that happens to a
character after it is read from the terminal. Record-keeping features
such as @code{recent-keys} and dribble files record the characters after
translation.
Note also that this translation is done before the characters are
supplied to input methods (@pxref{Input Methods}). Use
@code{translation-table-for-input} (@pxref{Translation of Characters}),
if you want to translate characters after input methods operate.
@end defvar
@defun key-translate from to
This function modifies @code{keyboard-translate-table} to translate
character code @var{from} into character code @var{to}. It creates the
keyboard translate table if necessary. Both @var{from} and @var{to}
should be strings that satisfy @code{key-valid-p} (@pxref{Key
Sequences}). If @var{to} is @code{nil}, the function removes any
existing translation for @var{from}.
@end defun
Here's an example of using the @code{keyboard-translate-table} to
make @kbd{C-x}, @kbd{C-c} and @kbd{C-v} perform the cut, copy and paste
operations:
@example
(key-translate "C-x" "<control-x>")
(key-translate "C-c" "<control-c>")
(key-translate "C-v" "<control-v>")
(keymap-global-set "<control-x>" 'kill-region)
(keymap-global-set "<control-c>" 'kill-ring-save)
(keymap-global-set "<control-v>" 'yank)
@end example
@noindent
On a graphical terminal that supports extended @acronym{ASCII} input,
you can still get the standard Emacs meanings of one of those
characters by typing it with the shift key. That makes it a different
character as far as keyboard translation is concerned, but it has the
same usual meaning.
@xref{Translation Keymaps}, for mechanisms that translate event sequences
at the level of @code{read-key-sequence}. If you need to translate
input events that are not characters (i.e., @code{characterp} returns
@code{nil} for them), you must use the event translation mechanism
described there.
@node Invoking the Input Method
@subsection Invoking the Input Method
@cindex invoking input method
The event-reading functions invoke the current input method, if any
(@pxref{Input Methods}). If the value of @code{input-method-function}
is non-@code{nil}, it should be a function; when @code{read-event} reads
a printing character (including @key{SPC}) with no modifier bits, it
calls that function, passing the character as an argument.
@defvar input-method-function
If this is non-@code{nil}, its value specifies the current input method
function.
@strong{Warning:} don't bind this variable with @code{let}. It is often
buffer-local, and if you bind it around reading input (which is exactly
when you @emph{would} bind it), switching buffers asynchronously while
Emacs is waiting will cause the value to be restored in the wrong
buffer.
@end defvar
The input method function should return a list of events which should
be used as input. (If the list is @code{nil}, that means there is no
input, so @code{read-event} waits for another event.) These events are
processed before the events in @code{unread-command-events}
(@pxref{Event Input Misc}). Events
returned by the input method function are not passed to the input method
function again, even if they are printing characters with no modifier
bits.
If the input method function calls @code{read-event} or
@code{read-key-sequence}, it should bind @code{input-method-function} to
@code{nil} first, to prevent recursion.
The input method function is not called when reading the second and
subsequent events of a key sequence. Thus, these characters are not
subject to input method processing. The input method function should
test the values of @code{overriding-local-map} and
@code{overriding-terminal-local-map}; if either of these variables is
non-@code{nil}, the input method should put its argument into a list and
return that list with no further processing.
@node Quoted Character Input
@subsection Quoted Character Input
@cindex quoted character input
You can use the function @code{read-quoted-char} to ask the user to
specify a character, and allow the user to specify a control or meta
character conveniently, either literally or as an octal character code.
The command @code{quoted-insert} uses this function.
@defun read-quoted-char &optional prompt
@cindex octal character input
@cindex control characters, reading
@cindex nonprinting characters, reading
This function is like @code{read-char}, except that if the first
character read is an octal digit (0--7), it reads any number of octal
digits (but stopping if a non-octal digit is found), and returns the
character represented by that numeric character code. If the
character that terminates the sequence of octal digits is @key{RET},
it is discarded. Any other terminating character is used as input
after this function returns.
Quitting is suppressed when the first character is read, so that the
user can enter a @kbd{C-g}. @xref{Quitting}.
If @var{prompt} is supplied, it specifies a string for prompting the
user. The prompt string is always displayed in the echo area, followed
by a single @samp{-}.
In the following example, the user types in the octal number 177 (which
is 127 in decimal).
@example
(read-quoted-char "What character")
@group
---------- Echo Area ----------
What character @kbd{1 7 7}-
---------- Echo Area ----------
@result{} 127
@end group
@end example
@end defun
@need 2000
@node Event Input Misc
@subsection Miscellaneous Event Input Features
This section describes how to peek ahead at events without using
them up, how to check for pending input, and how to discard pending
input. See also the function @code{read-passwd} (@pxref{Reading a
Password}).
@defvar unread-command-events
@cindex next input
@cindex peeking at input
This variable holds a list of events waiting to be read as command
input. The events are used in the order they appear in the list, and
removed one by one as they are used.
The variable is needed because in some cases a function reads an event
and then decides not to use it. Storing the event in this variable
causes it to be processed normally, by the command loop or by the
functions to read command input.
@cindex prefix argument unreading
For example, the function that implements numeric prefix arguments reads
any number of digits. When it finds a non-digit event, it must unread
the event so that it can be read normally by the command loop.
Likewise, incremental search uses this feature to unread events with no
special meaning in a search, because these events should exit the search
and then execute normally.
The reliable and easy way to extract events from a key sequence so as
to put them in @code{unread-command-events} is to use
@code{listify-key-sequence} (see below).
Normally you add events to the front of this list, so that the events
most recently unread will be reread first.
Events read from this list are not normally added to the current
command's key sequence (as returned by, e.g., @code{this-command-keys}),
as the events will already have been added once as they were read for
the first time. An element of the form @w{@code{(t . @var{event})}}
forces @var{event} to be added to the current command's key sequence.
@cindex not recording input events
@cindex input events, prevent recording
Elements read from this list are normally recorded by the
record-keeping features (@pxref{Recording Input}) and while defining a
keyboard macro (@pxref{Keyboard Macros}). However, an element of the
form @w{@code{(no-record . @var{event})}} causes @var{event} to be
processed normally without recording it.
@end defvar
@defun listify-key-sequence key
This function converts the string or vector @var{key} to a list of
individual events, which you can put in @code{unread-command-events}.
@end defun
@defun input-pending-p &optional check-timers
@cindex waiting for command key input
This function determines whether any command input is currently
available to be read. It returns immediately, with value @code{t} if
there is available input, @code{nil} otherwise. On rare occasions it
may return @code{t} when no input is available.
If the optional argument @var{check-timers} is non-@code{nil}, then if
no input is available, Emacs runs any timers which are ready.
@xref{Timers}.
@end defun
@defvar last-input-event
This variable records the last terminal input event read, whether
as part of a command or explicitly by a Lisp program.
In the example below, the Lisp program reads the character @kbd{1},
@acronym{ASCII} code 49. It becomes the value of @code{last-input-event},
while @kbd{C-e} (we assume @kbd{C-x C-e} command is used to evaluate
this expression) remains the value of @code{last-command-event}.
@example
@group
(progn (print (read-char))
(print last-command-event)
last-input-event)
@print{} 49
@print{} 5
@result{} 49
@end group
@end example
@end defvar
@defmac while-no-input body@dots{}
This construct runs the @var{body} forms and returns the value of the
last one---but only if no input arrives. If any input arrives during
the execution of the @var{body} forms, it aborts them (working much
like a quit). The @code{while-no-input} form returns @code{nil} if
aborted by a real quit, and returns @code{t} if aborted by arrival of
other input.
If a part of @var{body} binds @code{inhibit-quit} to non-@code{nil},
arrival of input during those parts won't cause an abort until
the end of that part.
If you want to be able to distinguish all possible values computed
by @var{body} from both kinds of abort conditions, write the code
like this:
@example
(while-no-input
(list
(progn . @var{body})))
@end example
@end defmac
@defvar while-no-input-ignore-events
This variable allow setting which special events @code{while-no-input}
should ignore. It is a list of event symbols (@pxref{Event Examples}).
@end defvar
@defun discard-input
@cindex flushing input
@cindex discarding input
@cindex keyboard macro, terminating
This function discards the contents of the terminal input buffer and
cancels any keyboard macro that might be in the process of definition.
It returns @code{nil}.
In the following example, the user may type a number of characters right
after starting the evaluation of the form. After the @code{sleep-for}
finishes sleeping, @code{discard-input} discards any characters typed
during the sleep.
@example
(progn (sleep-for 2)
(discard-input))
@result{} nil
@end example
@end defun
@node Special Events
@section Special Events
@cindex special events
Certain @dfn{special events} are handled at a very low level---as soon
as they are read. The @code{read-event} function processes these
events itself, and never returns them. Instead, it keeps waiting for
the first event that is not special and returns that one.
Special events do not echo, they are never grouped into key
sequences, and they never appear in the value of
@code{last-command-event} or @code{(this-command-keys)}. They do not
discard a numeric argument, they cannot be unread with
@code{unread-command-events}, they may not appear in a keyboard macro,
and they are not recorded in a keyboard macro while you are defining
one.
Special events do, however, appear in @code{last-input-event}
immediately after they are read, and this is the way for the event's
definition to find the actual event.
The events types @code{iconify-frame}, @code{make-frame-visible},
@code{delete-frame}, @code{drag-n-drop}, @code{language-change}, and
user signals like @code{sigusr1} are normally handled in this way.
The keymap which defines how to handle special events---and which
events are special---is in the variable @code{special-event-map}
(@pxref{Controlling Active Maps}).
@node Waiting
@section Waiting for Elapsed Time or Input
@cindex waiting
The wait functions are designed to wait for a certain amount of time
to pass or until there is input. For example, you may wish to pause in
the middle of a computation to allow the user time to view the display.
@code{sit-for} pauses and updates the screen, and returns immediately if
input comes in, while @code{sleep-for} pauses without updating the
screen.
@defun sit-for seconds &optional nodisp
This function performs redisplay (provided there is no pending input
from the user), then waits @var{seconds} seconds, or until input is
available. The usual purpose of @code{sit-for} is to give the user
time to read text that you display. The value is @code{t} if
@code{sit-for} waited the full time with no input arriving
(@pxref{Event Input Misc}). Otherwise, the value is @code{nil}.
The argument @var{seconds} need not be an integer. If it is floating
point, @code{sit-for} waits for a fractional number of seconds.
Some systems support only a whole number of seconds; on these systems,
@var{seconds} is rounded down.
The expression @code{(sit-for 0)} is equivalent to @code{(redisplay)},
i.e., it requests a redisplay, without any delay, if there is no pending input.
@xref{Forcing Redisplay}.
If @var{nodisp} is non-@code{nil}, then @code{sit-for} does not
redisplay, but it still returns as soon as input is available (or when
the timeout elapses).
In batch mode (@pxref{Batch Mode}), @code{sit-for} cannot be
interrupted, even by input from the standard input descriptor. It is
thus equivalent to @code{sleep-for}, which is described below.
@end defun
@defun sleep-for seconds
This function simply pauses for @var{seconds} seconds without updating
the display. It pays no attention to available input. It returns
@code{nil}.
The argument @var{seconds} need not be an integer. If it is floating
point, @code{sleep-for} waits for a fractional number of seconds.
It is also possible to call @code{sleep-for} with two arguments,
as @code{(sleep-for @var{seconds} @var{millisec})},
but that is considered obsolete and will be removed in the future.
Use @code{sleep-for} when you wish to guarantee a delay.
@end defun
@xref{Time of Day}, for functions to get the current time.
@node Quitting
@section Quitting
@cindex @kbd{C-g}
@cindex quitting
@cindex interrupt Lisp functions
Typing @kbd{C-g} while a Lisp function is running causes Emacs to
@dfn{quit} whatever it is doing. This means that control returns to the
innermost active command loop.
Typing @kbd{C-g} while the command loop is waiting for keyboard input
does not cause a quit; it acts as an ordinary input character. In the
simplest case, you cannot tell the difference, because @kbd{C-g}
normally runs the command @code{keyboard-quit}, whose effect is to quit.
However, when @kbd{C-g} follows a prefix key, they combine to form an
undefined key. The effect is to cancel the prefix key as well as any
prefix argument.
In the minibuffer, @kbd{C-g} has a different definition: it aborts out
of the minibuffer. This means, in effect, that it exits the minibuffer
and then quits. (Simply quitting would return to the command loop
@emph{within} the minibuffer.) The reason why @kbd{C-g} does not quit
directly when the command reader is reading input is so that its meaning
can be redefined in the minibuffer in this way. @kbd{C-g} following a
prefix key is not redefined in the minibuffer, and it has its normal
effect of canceling the prefix key and prefix argument. This too
would not be possible if @kbd{C-g} always quit directly.
When @kbd{C-g} does directly quit, it does so by setting the variable
@code{quit-flag} to @code{t}. Emacs checks this variable at appropriate
times and quits if it is not @code{nil}. Setting @code{quit-flag}
non-@code{nil} in any way thus causes a quit.
At the level of C code, quitting cannot happen just anywhere; only at the
special places that check @code{quit-flag}. The reason for this is
that quitting at other places might leave an inconsistency in Emacs's
internal state. Because quitting is delayed until a safe place, quitting
cannot make Emacs crash.
Certain functions such as @code{read-key-sequence} or
@code{read-quoted-char} prevent quitting entirely even though they wait
for input. Instead of quitting, @kbd{C-g} serves as the requested
input. In the case of @code{read-key-sequence}, this serves to bring
about the special behavior of @kbd{C-g} in the command loop. In the
case of @code{read-quoted-char}, this is so that @kbd{C-q} can be used
to quote a @kbd{C-g}.
@cindex preventing quitting
You can prevent quitting for a portion of a Lisp function by binding
the variable @code{inhibit-quit} to a non-@code{nil} value. Then,
although @kbd{C-g} still sets @code{quit-flag} to @code{t} as usual, the
usual result of this---a quit---is prevented. Eventually,
@code{inhibit-quit} will become @code{nil} again, such as when its
binding is unwound at the end of a @code{let} form. At that time, if
@code{quit-flag} is still non-@code{nil}, the requested quit happens
immediately. This behavior is ideal when you wish to make sure that
quitting does not happen within a critical section of the program.
@cindex @code{read-quoted-char} quitting
In some functions (such as @code{read-quoted-char}), @kbd{C-g} is
handled in a special way that does not involve quitting. This is done
by reading the input with @code{inhibit-quit} bound to @code{t}, and
setting @code{quit-flag} to @code{nil} before @code{inhibit-quit}
becomes @code{nil} again. This excerpt from the definition of
@code{read-quoted-char} shows how this is done; it also shows that
normal quitting is permitted after the first character of input.
@example
(defun read-quoted-char (&optional prompt)
"@dots{}@var{documentation}@dots{}"
(let ((message-log-max nil) done (first t) (code 0) char)
(while (not done)
(let ((inhibit-quit first)
@dots{})
(and prompt (message "%s-" prompt))
(setq char (read-event))
(if inhibit-quit (setq quit-flag nil)))
@r{@dots{}set the variable @code{code}@dots{}})
code))
@end example
@defvar quit-flag
If this variable is non-@code{nil}, then Emacs quits immediately, unless
@code{inhibit-quit} is non-@code{nil}. Typing @kbd{C-g} ordinarily sets
@code{quit-flag} non-@code{nil}, regardless of @code{inhibit-quit}.
@end defvar
@defvar inhibit-quit
This variable determines whether Emacs should quit when @code{quit-flag}
is set to a value other than @code{nil}. If @code{inhibit-quit} is
non-@code{nil}, then @code{quit-flag} has no special effect.
@end defvar
@defmac with-local-quit body@dots{}
This macro executes @var{body} forms in sequence, but allows quitting, at
least locally, within @var{body} even if @code{inhibit-quit} was
non-@code{nil} outside this construct. It returns the value of the
last form in @var{body}, unless exited by quitting, in which case
it returns @code{nil}.
If @code{inhibit-quit} is @code{nil} on entry to @code{with-local-quit},
it only executes the @var{body}, and setting @code{quit-flag} causes
a normal quit. However, if @code{inhibit-quit} is non-@code{nil} so
that ordinary quitting is delayed, a non-@code{nil} @code{quit-flag}
triggers a special kind of local quit. This ends the execution of
@var{body} and exits the @code{with-local-quit} body with
@code{quit-flag} still non-@code{nil}, so that another (ordinary) quit
will happen as soon as that is allowed. If @code{quit-flag} is
already non-@code{nil} at the beginning of @var{body}, the local quit
happens immediately and the body doesn't execute at all.
This macro is mainly useful in functions that can be called from
timers, process filters, process sentinels, @code{pre-command-hook},
@code{post-command-hook}, and other places where @code{inhibit-quit} is
normally bound to @code{t}.
@end defmac
@deffn Command keyboard-quit
This function signals the @code{quit} condition with @code{(signal 'quit
nil)}. This is the same thing that quitting does. (See @code{signal}
in @ref{Errors}.)
@end deffn
To quit without aborting a keyboard macro definition or execution,
you can signal the @code{minibuffer-quit} condition. This has almost
the same effect as the @code{quit} condition except that the error
handling in the command loop handles it without exiting keyboard macro
definition or execution.
You can specify a character other than @kbd{C-g} to use for quitting.
See the function @code{set-input-mode} in @ref{Input Modes}.
@node Prefix Command Arguments
@section Prefix Command Arguments
@cindex prefix argument
@cindex raw prefix argument
@cindex numeric prefix argument
Most Emacs commands can use a @dfn{prefix argument}, a number
specified before the command itself. (Don't confuse prefix arguments
with prefix keys.) The prefix argument is at all times represented by a
value, which may be @code{nil}, meaning there is currently no prefix
argument. Each command may use the prefix argument or ignore it.
There are two representations of the prefix argument: @dfn{raw} and
@dfn{numeric}. The editor command loop uses the raw representation
internally, and so do the Lisp variables that store the information, but
commands can request either representation.
Here are the possible values of a raw prefix argument:
@itemize @bullet
@item
@code{nil}, meaning there is no prefix argument. Its numeric value is
1, but numerous commands make a distinction between @code{nil} and the
integer 1.
@item
An integer, which stands for itself.
@item
A list of one element, which is an integer. This form of prefix
argument results from one or a succession of @kbd{C-u}s with no
digits. The numeric value is the integer in the list, but some
commands make a distinction between such a list and an integer alone.
@item
The symbol @code{-}. This indicates that @kbd{M--} or @kbd{C-u -} was
typed, without following digits. The equivalent numeric value is
@minus{}1, but some commands make a distinction between the integer
@minus{}1 and the symbol @code{-}.
@end itemize
We illustrate these possibilities by calling the following function with
various prefixes:
@example
@group
(defun display-prefix (arg)
"Display the value of the raw prefix arg."
(interactive "P")
(message "%s" arg))
@end group
@end example
@noindent
Here are the results of calling @code{display-prefix} with various
raw prefix arguments:
@example
M-x display-prefix @print{} nil
C-u M-x display-prefix @print{} (4)
C-u C-u M-x display-prefix @print{} (16)
C-u 3 M-x display-prefix @print{} 3
M-3 M-x display-prefix @print{} 3 ; @r{(Same as @code{C-u 3}.)}
C-u - M-x display-prefix @print{} -
M-- M-x display-prefix @print{} - ; @r{(Same as @code{C-u -}.)}
C-u - 7 M-x display-prefix @print{} -7
M-- 7 M-x display-prefix @print{} -7 ; @r{(Same as @code{C-u -7}.)}
@end example
Emacs uses two variables to store the prefix argument:
@code{prefix-arg} and @code{current-prefix-arg}. Commands such as
@code{universal-argument} that set up prefix arguments for other
commands store them in @code{prefix-arg}. In contrast,
@code{current-prefix-arg} conveys the prefix argument to the current
command, so setting it has no effect on the prefix arguments for future
commands.
Normally, commands specify which representation to use for the prefix
argument, either numeric or raw, in the @code{interactive} specification.
(@xref{Using Interactive}.) Alternatively, functions may look at the
value of the prefix argument directly in the variable
@code{current-prefix-arg}, but this is less clean.
@defun prefix-numeric-value arg
This function returns the numeric meaning of a valid raw prefix argument
value, @var{arg}. The argument may be a symbol, a number, or a list.
If it is @code{nil}, the value 1 is returned; if it is @code{-}, the
value @minus{}1 is returned; if it is a number, that number is returned;
if it is a list, the @sc{car} of that list (which should be a number) is
returned.
@end defun
@defvar current-prefix-arg
This variable holds the raw prefix argument for the @emph{current}
command. Commands may examine it directly, but the usual method for
accessing it is with @code{(interactive "P")}.
@end defvar
@defvar prefix-arg
The value of this variable is the raw prefix argument for the
@emph{next} editing command. Commands such as @code{universal-argument}
that specify prefix arguments for the following command work by setting
this variable.
@end defvar
@defvar last-prefix-arg
The raw prefix argument value used by the previous command.
@end defvar
The following commands exist to set up prefix arguments for the
following command. Do not call them for any other reason.
@deffn Command universal-argument
This command reads input and specifies a prefix argument for the
following command. Don't call this command yourself unless you know
what you are doing.
@end deffn
@deffn Command digit-argument arg
This command adds to the prefix argument for the following command. The
argument @var{arg} is the raw prefix argument as it was before this
command; it is used to compute the updated prefix argument. Don't call
this command yourself unless you know what you are doing.
@end deffn
@deffn Command negative-argument arg
This command adds to the numeric argument for the next command. The
argument @var{arg} is the raw prefix argument as it was before this
command; its value is negated to form the new prefix argument. Don't
call this command yourself unless you know what you are doing.
@end deffn
@node Recursive Editing
@section Recursive Editing
@cindex recursive command loop
@cindex recursive editing level
@cindex command loop, recursive
The Emacs command loop is entered automatically when Emacs starts up.
This top-level invocation of the command loop never exits; it keeps
running as long as Emacs does. Lisp programs can also invoke the
command loop. Since this makes more than one activation of the command
loop, we call it @dfn{recursive editing}. A recursive editing level has
the effect of suspending whatever command invoked it and permitting the
user to do arbitrary editing before resuming that command.
The commands available during recursive editing are the same ones
available in the top-level editing loop and defined in the keymaps.
Only a few special commands exit the recursive editing level; the others
return to the recursive editing level when they finish. (The special
commands for exiting are always available, but they do nothing when
recursive editing is not in progress.)
All command loops, including recursive ones, set up all-purpose error
handlers so that an error in a command run from the command loop will
not exit the loop.
@cindex minibuffer input
Minibuffer input is a special kind of recursive editing. It has a few
special wrinkles, such as enabling display of the minibuffer and the
minibuffer window, but fewer than you might suppose. Certain keys
behave differently in the minibuffer, but that is only because of the
minibuffer's local map; if you switch windows, you get the usual Emacs
commands.
@cindex @code{throw} example
@kindex exit
@cindex exit recursive editing
@cindex aborting
To invoke a recursive editing level, call the function
@code{recursive-edit}. This function contains the command loop; it
also contains a call to @code{catch} with tag @code{exit}, which makes
it possible to exit the recursive editing level by throwing to
@code{exit} (@pxref{Catch and Throw}). Throwing a @code{t} value
causes @code{recursive-edit} to quit, so that control returns to the
command loop one level up. This is called @dfn{aborting}, and is done
by @kbd{C-]} (@code{abort-recursive-edit}). Similarly, you can throw
a string value to make @code{recursive-edit} signal an error, printing
this string as the message. If you throw a function,
@code{recursive-edit} will call it without arguments before returning.
Throwing any other value, will make @code{recursive-edit} return
normally to the function that called it. The command @kbd{C-M-c}
(@code{exit-recursive-edit}) does this.
Most applications should not use recursive editing, except as part of
using the minibuffer. Usually it is more convenient for the user if you
change the major mode of the current buffer temporarily to a special
major mode, which should have a command to go back to the previous mode.
(The @kbd{e} command in Rmail uses this technique.) Or, if you wish to
give the user different text to edit recursively, create and select
a new buffer in a special mode. In this mode, define a command to
complete the processing and go back to the previous buffer. (The
@kbd{m} command in Rmail does this.)
Recursive edits are useful in debugging. You can insert a call to
@code{debug} into a function definition as a sort of breakpoint, so that
you can look around when the function gets there. @code{debug} invokes
a recursive edit but also provides the other features of the debugger.
Recursive editing levels are also used when you type @kbd{C-r} in
@code{query-replace} or use @kbd{C-x q} (@code{kbd-macro-query}).
@deffn Command recursive-edit
@cindex suspend evaluation
This function invokes the editor command loop. It is called
automatically by the initialization of Emacs, to let the user begin
editing. When called from a Lisp program, it enters a recursive editing
level.
If the current buffer is not the same as the selected window's buffer,
@code{recursive-edit} saves and restores the current buffer. Otherwise,
if you switch buffers, the buffer you switched to is current after
@code{recursive-edit} returns.
In the following example, the function @code{simple-rec} first
advances point one word, then enters a recursive edit, printing out a
message in the echo area. The user can then do any editing desired, and
then type @kbd{C-M-c} to exit and continue executing @code{simple-rec}.
@example
(defun simple-rec ()
(forward-word 1)
(message "Recursive edit in progress")
(recursive-edit)
(forward-word 1))
@result{} simple-rec
(simple-rec)
@result{} nil
@end example
@end deffn
@deffn Command exit-recursive-edit
This function exits from the innermost recursive edit (including
minibuffer input). Its definition is effectively @code{(throw 'exit
nil)}.
@end deffn
@deffn Command abort-recursive-edit
This function aborts the command that requested the innermost recursive
edit (including minibuffer input), by signaling @code{quit}
after exiting the recursive edit. Its definition is effectively
@code{(throw 'exit t)}. @xref{Quitting}.
@end deffn
@deffn Command top-level
This function exits all recursive editing levels; it does not return a
value, as it jumps completely out of any computation directly back to
the main command loop.
@end deffn
@defun recursion-depth
This function returns the current depth of recursive edits. When no
recursive edit is active, it returns 0.
@end defun
@node Disabling Commands
@section Disabling Commands
@cindex disabled command
@dfn{Disabling a command} marks the command as requiring user
confirmation before it can be executed. Disabling is used for commands
which might be confusing to beginning users, to prevent them from using
the commands by accident.
@kindex disabled
The low-level mechanism for disabling a command is to put a
non-@code{nil} @code{disabled} property on the Lisp symbol for the
command. These properties are normally set up by the user's
init file (@pxref{Init File}) with Lisp expressions such as this:
@example
(put 'upcase-region 'disabled t)
@end example
@noindent
For a few commands, these properties are present by default (you can
remove them in your init file if you wish).
If the value of the @code{disabled} property is a string, the message
saying the command is disabled includes that string. For example:
@example
(put 'delete-region 'disabled
"Text deleted this way cannot be yanked back!\n")
@end example
@xref{Disabling,,, emacs, The GNU Emacs Manual}, for the details on
what happens when a disabled command is invoked interactively.
Disabling a command has no effect on calling it as a function from Lisp
programs.
@findex command-query
The value of the @code{disabled} property can also be a list where
the first element is the symbol @code{query}. In that case, the user
will be queried whether to execute the command. The second element in
the list should be @code{nil} or non-@code{nil} to say whether to use
@code{y-or-n-p} or @code{yes-or-no-p}, respectively, and the third
element is the question to use. The @code{command-query} convenience
function should be used to enable querying for a command.
@deffn Command enable-command command
Allow @var{command} (a symbol) to be executed without special
confirmation from now on, and alter the user's init file (@pxref{Init
File}) so that this will apply to future sessions.
@end deffn
@deffn Command disable-command command
Require special confirmation to execute @var{command} from now on, and
alter the user's init file so that this will apply to future sessions.
@end deffn
@defvar disabled-command-function
The value of this variable should be a function. When the user
invokes a disabled command interactively, this function is called
instead of the disabled command. It can use @code{this-command-keys}
to determine what the user typed to run the command, and thus find the
command itself.
The value may also be @code{nil}. Then all commands work normally,
even disabled ones.
By default, the value is a function that asks the user whether to
proceed.
@end defvar
@node Command History
@section Command History
@cindex command history
@cindex complex command
@cindex history of commands
The command loop keeps a history of the complex commands that have
been executed, to make it convenient to repeat these commands. A
@dfn{complex command} is one for which the interactive argument reading
uses the minibuffer. This includes any @kbd{M-x} command, any
@kbd{M-:} command, and any command whose @code{interactive}
specification reads an argument from the minibuffer. Explicit use of
the minibuffer during the execution of the command itself does not cause
the command to be considered complex.
@defvar command-history
This variable's value is a list of recent complex commands, each
represented as a form to evaluate. It continues to accumulate all
complex commands for the duration of the editing session, but when it
reaches the maximum size (@pxref{Minibuffer History}), the oldest
elements are deleted as new ones are added.
@example
@group
command-history
@result{} ((switch-to-buffer "chistory.texi")
(describe-key "^X^[")
(visit-tags-table "~/emacs/src/")
(find-tag "repeat-complex-command"))
@end group
@end example
@end defvar
This history list is actually a special case of minibuffer history
(@pxref{Minibuffer History}), with one special twist: the elements are
expressions rather than strings.
There are a number of commands devoted to the editing and recall of
previous commands. The commands @code{repeat-complex-command}, and
@code{list-command-history} are described in the user manual
(@pxref{Repetition,,, emacs, The GNU Emacs Manual}). Within the
minibuffer, the usual minibuffer history commands are available.
@node Keyboard Macros
@section Keyboard Macros
@cindex keyboard macros
A @dfn{keyboard macro} is a canned sequence of input events that can
be considered a command and made the definition of a key. The Lisp
representation of a keyboard macro is a string or vector containing the
events. Don't confuse keyboard macros with Lisp macros
(@pxref{Macros}).
@defun execute-kbd-macro kbdmacro &optional count loopfunc
This function executes @var{kbdmacro} as a sequence of events. If
@var{kbdmacro} is a string or vector, then the events in it are executed
exactly as if they had been input by the user. The sequence is
@emph{not} expected to be a single key sequence; normally a keyboard
macro definition consists of several key sequences concatenated.
If @var{kbdmacro} is a symbol, then its function definition is used in
place of @var{kbdmacro}. If that is another symbol, this process repeats.
Eventually the result should be a string or vector. If the result is
not a symbol, string, or vector, an error is signaled.
The argument @var{count} is a repeat count; @var{kbdmacro} is executed that
many times. If @var{count} is omitted or @code{nil}, @var{kbdmacro} is
executed once. If it is 0, @var{kbdmacro} is executed over and over until it
encounters an error or a failing search.
If @var{loopfunc} is non-@code{nil}, it is a function that is called,
without arguments, prior to each iteration of the macro. If
@var{loopfunc} returns @code{nil}, then this stops execution of the macro.
@xref{Reading One Event}, for an example of using @code{execute-kbd-macro}.
@end defun
@defvar executing-kbd-macro
This variable contains the string or vector that defines the keyboard
macro that is currently executing. It is @code{nil} if no macro is
currently executing. A command can test this variable so as to behave
differently when run from an executing macro. Do not set this variable
yourself.
@end defvar
@defvar defining-kbd-macro
This variable is non-@code{nil} if and only if a keyboard macro is
being defined. A command can test this variable so as to behave
differently while a macro is being defined. The value is
@code{append} while appending to the definition of an existing macro.
The commands @code{start-kbd-macro}, @code{kmacro-start-macro} and
@code{end-kbd-macro} set this variable---do not set it yourself.
The variable is always local to the current terminal and cannot be
buffer-local. @xref{Multiple Terminals}.
@end defvar
@defvar last-kbd-macro
This variable is the definition of the most recently defined keyboard
macro. Its value is a string or vector, or @code{nil}.
The variable is always local to the current terminal and cannot be
buffer-local. @xref{Multiple Terminals}.
@end defvar
@defvar kbd-macro-termination-hook
This normal hook is run when a keyboard macro terminates, regardless
of what caused it to terminate (reaching the macro end or an error
which ended the macro prematurely).
@end defvar
|