Mercurial > emacs
comparison src/keymap.c @ 7998:d4b5f4dd9c51
(Flookup_key): Add gcpro.
(Fkey_binding, Fminor_mode_key_binding): Likewise.
(Fwhere_is_internal): Likewise.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 21 Jun 1994 19:46:07 +0000 |
parents | d385199b26b6 |
children | b4138573c00f |
comparison
equal
deleted
inserted
replaced
7997:dda8e966edf5 | 7998:d4b5f4dd9c51 |
---|---|
186 /* Check that OBJECT is a keymap (after dereferencing through any | 186 /* Check that OBJECT is a keymap (after dereferencing through any |
187 symbols). If it is, return it. | 187 symbols). If it is, return it. |
188 | 188 |
189 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value | 189 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value |
190 is an autoload form, do the autoload and try again. | 190 is an autoload form, do the autoload and try again. |
191 If AUTOLOAD is nonzero, callers must assume GC is possible. | |
191 | 192 |
192 ERROR controls how we respond if OBJECT isn't a keymap. | 193 ERROR controls how we respond if OBJECT isn't a keymap. |
193 If ERROR is non-zero, signal an error; otherwise, just return Qnil. | 194 If ERROR is non-zero, signal an error; otherwise, just return Qnil. |
194 | 195 |
195 Note that most of the time, we don't want to pursue autoloads. | 196 Note that most of the time, we don't want to pursue autoloads. |
549 | 550 |
550 return copy; | 551 return copy; |
551 } | 552 } |
552 | 553 |
553 /* Simple Keymap mutators and accessors. */ | 554 /* Simple Keymap mutators and accessors. */ |
555 | |
556 /* GC is possible in this function if it autoloads a keymap. */ | |
554 | 557 |
555 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0, | 558 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0, |
556 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\ | 559 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\ |
557 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\ | 560 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\ |
558 meaning a sequence of keystrokes and events.\n\ | 561 meaning a sequence of keystrokes and events.\n\ |
644 XSTRING (Fkey_description (key))->data); | 647 XSTRING (Fkey_description (key))->data); |
645 } | 648 } |
646 } | 649 } |
647 | 650 |
648 /* Value is number if KEY is too long; NIL if valid but has no definition. */ | 651 /* Value is number if KEY is too long; NIL if valid but has no definition. */ |
652 /* GC is possible in this function if it autoloads a keymap. */ | |
649 | 653 |
650 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0, | 654 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0, |
651 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\ | 655 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\ |
652 nil means undefined. See doc of `define-key' for kinds of definitions.\n\ | 656 nil means undefined. See doc of `define-key' for kinds of definitions.\n\ |
653 \n\ | 657 \n\ |
673 register Lisp_Object c; | 677 register Lisp_Object c; |
674 int metized = 0; | 678 int metized = 0; |
675 int length; | 679 int length; |
676 int t_ok = ! NILP (accept_default); | 680 int t_ok = ! NILP (accept_default); |
677 int meta_bit; | 681 int meta_bit; |
682 struct gcpro gcpro1; | |
678 | 683 |
679 keymap = get_keymap_1 (keymap, 1, 1); | 684 keymap = get_keymap_1 (keymap, 1, 1); |
680 | 685 |
681 if (XTYPE (key) != Lisp_Vector | 686 if (XTYPE (key) != Lisp_Vector |
682 && XTYPE (key) != Lisp_String) | 687 && XTYPE (key) != Lisp_String) |
689 if (XTYPE (key) == Lisp_Vector) | 694 if (XTYPE (key) == Lisp_Vector) |
690 meta_bit = meta_modifier; | 695 meta_bit = meta_modifier; |
691 else | 696 else |
692 meta_bit = 0x80; | 697 meta_bit = 0x80; |
693 | 698 |
699 GCPRO1 (key); | |
700 | |
694 idx = 0; | 701 idx = 0; |
695 while (1) | 702 while (1) |
696 { | 703 { |
697 c = Faref (key, make_number (idx)); | 704 c = Faref (key, make_number (idx)); |
698 | 705 |
712 idx++; | 719 idx++; |
713 } | 720 } |
714 | 721 |
715 cmd = get_keyelt (access_keymap (keymap, c, t_ok, 0), 1); | 722 cmd = get_keyelt (access_keymap (keymap, c, t_ok, 0), 1); |
716 if (idx == length) | 723 if (idx == length) |
717 return cmd; | 724 RETURN_UNGCPRO (cmd); |
718 | 725 |
719 keymap = get_keymap_1 (cmd, 0, 1); | 726 keymap = get_keymap_1 (cmd, 0, 1); |
720 if (NILP (keymap)) | 727 if (NILP (keymap)) |
721 return make_number (idx); | 728 RETURN_UNGCPRO (make_number (idx)); |
722 | 729 |
723 QUIT; | 730 QUIT; |
724 } | 731 } |
725 } | 732 } |
726 | 733 |
857 if (modeptr) *modeptr = cmm_modes; | 864 if (modeptr) *modeptr = cmm_modes; |
858 if (mapptr) *mapptr = cmm_maps; | 865 if (mapptr) *mapptr = cmm_maps; |
859 return i; | 866 return i; |
860 } | 867 } |
861 | 868 |
869 /* GC is possible in this function if it autoloads a keymap. */ | |
870 | |
862 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0, | 871 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0, |
863 "Return the binding for command KEY in current keymaps.\n\ | 872 "Return the binding for command KEY in current keymaps.\n\ |
864 KEY is a string or vector, a sequence of keystrokes.\n\ | 873 KEY is a string or vector, a sequence of keystrokes.\n\ |
865 The binding is probably a symbol with a function definition.\n\ | 874 The binding is probably a symbol with a function definition.\n\ |
866 \n\ | 875 \n\ |
872 (key, accept_default) | 881 (key, accept_default) |
873 Lisp_Object key, accept_default; | 882 Lisp_Object key, accept_default; |
874 { | 883 { |
875 Lisp_Object *maps, value; | 884 Lisp_Object *maps, value; |
876 int nmaps, i; | 885 int nmaps, i; |
886 struct gcpro gcpro1; | |
887 | |
888 GCPRO1 (key); | |
877 | 889 |
878 if (!NILP (Voverriding_local_map)) | 890 if (!NILP (Voverriding_local_map)) |
879 { | 891 { |
880 value = Flookup_key (Voverriding_local_map, key, accept_default); | 892 value = Flookup_key (Voverriding_local_map, key, accept_default); |
881 if (! NILP (value) && XTYPE (value) != Lisp_Int) | 893 if (! NILP (value) && XTYPE (value) != Lisp_Int) |
882 return value; | 894 RETURN_UNGCPRO (value); |
883 } | 895 } |
884 else | 896 else |
885 { | 897 { |
886 nmaps = current_minor_maps (0, &maps); | 898 nmaps = current_minor_maps (0, &maps); |
899 /* Note that all these maps are GCPRO'd | |
900 in the places where we found them. */ | |
901 | |
887 for (i = 0; i < nmaps; i++) | 902 for (i = 0; i < nmaps; i++) |
888 if (! NILP (maps[i])) | 903 if (! NILP (maps[i])) |
889 { | 904 { |
890 value = Flookup_key (maps[i], key, accept_default); | 905 value = Flookup_key (maps[i], key, accept_default); |
891 if (! NILP (value) && XTYPE (value) != Lisp_Int) | 906 if (! NILP (value) && XTYPE (value) != Lisp_Int) |
892 return value; | 907 RETURN_UNGCPRO (value); |
893 } | 908 } |
894 | 909 |
895 if (! NILP (current_buffer->keymap)) | 910 if (! NILP (current_buffer->keymap)) |
896 { | 911 { |
897 value = Flookup_key (current_buffer->keymap, key, accept_default); | 912 value = Flookup_key (current_buffer->keymap, key, accept_default); |
898 if (! NILP (value) && XTYPE (value) != Lisp_Int) | 913 if (! NILP (value) && XTYPE (value) != Lisp_Int) |
899 return value; | 914 RETURN_UNGCPRO (value); |
900 } | 915 } |
901 } | 916 } |
902 | 917 |
903 value = Flookup_key (current_global_map, key, accept_default); | 918 value = Flookup_key (current_global_map, key, accept_default); |
919 UNGCPRO; | |
904 if (! NILP (value) && XTYPE (value) != Lisp_Int) | 920 if (! NILP (value) && XTYPE (value) != Lisp_Int) |
905 return value; | 921 return value; |
906 | 922 |
907 return Qnil; | 923 return Qnil; |
908 } | 924 } |
925 | |
926 /* GC is possible in this function if it autoloads a keymap. */ | |
909 | 927 |
910 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0, | 928 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0, |
911 "Return the binding for command KEYS in current local keymap only.\n\ | 929 "Return the binding for command KEYS in current local keymap only.\n\ |
912 KEYS is a string, a sequence of keystrokes.\n\ | 930 KEYS is a string, a sequence of keystrokes.\n\ |
913 The binding is probably a symbol with a function definition.\n\ | 931 The binding is probably a symbol with a function definition.\n\ |
922 if (NILP (map)) | 940 if (NILP (map)) |
923 return Qnil; | 941 return Qnil; |
924 return Flookup_key (map, keys, accept_default); | 942 return Flookup_key (map, keys, accept_default); |
925 } | 943 } |
926 | 944 |
945 /* GC is possible in this function if it autoloads a keymap. */ | |
946 | |
927 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0, | 947 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0, |
928 "Return the binding for command KEYS in current global keymap only.\n\ | 948 "Return the binding for command KEYS in current global keymap only.\n\ |
929 KEYS is a string, a sequence of keystrokes.\n\ | 949 KEYS is a string, a sequence of keystrokes.\n\ |
930 The binding is probably a symbol with a function definition.\n\ | 950 The binding is probably a symbol with a function definition.\n\ |
931 This function's return values are the same as those of lookup-key\n\ | 951 This function's return values are the same as those of lookup-key\n\ |
932 (which see).\n\ | 952 \(which see).\n\ |
933 \n\ | 953 \n\ |
934 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\ | 954 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\ |
935 bindings; see the description of `lookup-key' for more details about this.") | 955 bindings; see the description of `lookup-key' for more details about this.") |
936 (keys, accept_default) | 956 (keys, accept_default) |
937 Lisp_Object keys, accept_default; | 957 Lisp_Object keys, accept_default; |
938 { | 958 { |
939 return Flookup_key (current_global_map, keys, accept_default); | 959 return Flookup_key (current_global_map, keys, accept_default); |
940 } | 960 } |
961 | |
962 /* GC is possible in this function if it autoloads a keymap. */ | |
941 | 963 |
942 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0, | 964 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0, |
943 "Find the visible minor mode bindings of KEY.\n\ | 965 "Find the visible minor mode bindings of KEY.\n\ |
944 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\ | 966 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\ |
945 the symbol which names the minor mode binding KEY, and BINDING is\n\ | 967 the symbol which names the minor mode binding KEY, and BINDING is\n\ |
956 { | 978 { |
957 Lisp_Object *modes, *maps; | 979 Lisp_Object *modes, *maps; |
958 int nmaps; | 980 int nmaps; |
959 Lisp_Object binding; | 981 Lisp_Object binding; |
960 int i, j; | 982 int i, j; |
983 struct gcpro gcpro1, gcpro2; | |
961 | 984 |
962 nmaps = current_minor_maps (&modes, &maps); | 985 nmaps = current_minor_maps (&modes, &maps); |
986 /* Note that all these maps are GCPRO'd | |
987 in the places where we found them. */ | |
988 | |
989 binding = Qnil; | |
990 GCPRO2 (key, binding); | |
963 | 991 |
964 for (i = j = 0; i < nmaps; i++) | 992 for (i = j = 0; i < nmaps; i++) |
965 if (! NILP (maps[i]) | 993 if (! NILP (maps[i]) |
966 && ! NILP (binding = Flookup_key (maps[i], key, accept_default)) | 994 && ! NILP (binding = Flookup_key (maps[i], key, accept_default)) |
967 && XTYPE (binding) != Lisp_Int) | 995 && XTYPE (binding) != Lisp_Int) |
968 { | 996 { |
969 if (! NILP (get_keymap (binding))) | 997 if (! NILP (get_keymap (binding))) |
970 maps[j++] = Fcons (modes[i], binding); | 998 maps[j++] = Fcons (modes[i], binding); |
971 else if (j == 0) | 999 else if (j == 0) |
972 return Fcons (Fcons (modes[i], binding), Qnil); | 1000 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil)); |
973 } | 1001 } |
974 | 1002 |
1003 UNGCPRO; | |
975 return Flist (j, maps); | 1004 return Flist (j, maps); |
976 } | 1005 } |
977 | 1006 |
978 DEFUN ("global-set-key", Fglobal_set_key, Sglobal_set_key, 2, 2, | 1007 DEFUN ("global-set-key", Fglobal_set_key, Sglobal_set_key, 2, 2, |
979 "kSet key globally: \nCSet key %s to command: ", | 1008 "kSet key globally: \nCSet key %s to command: ", |
1112 return Flist (nmaps, maps); | 1141 return Flist (nmaps, maps); |
1113 } | 1142 } |
1114 | 1143 |
1115 /* Help functions for describing and documenting keymaps. */ | 1144 /* Help functions for describing and documenting keymaps. */ |
1116 | 1145 |
1146 /* This function cannot GC. */ | |
1147 | |
1117 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps, | 1148 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps, |
1118 1, 2, 0, | 1149 1, 2, 0, |
1119 "Find all keymaps accessible via prefix characters from KEYMAP.\n\ | 1150 "Find all keymaps accessible via prefix characters from KEYMAP.\n\ |
1120 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\ | 1151 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\ |
1121 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\ | 1152 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\ |
1125 (startmap, prefix) | 1156 (startmap, prefix) |
1126 Lisp_Object startmap, prefix; | 1157 Lisp_Object startmap, prefix; |
1127 { | 1158 { |
1128 Lisp_Object maps, good_maps, tail; | 1159 Lisp_Object maps, good_maps, tail; |
1129 int prefixlen = 0; | 1160 int prefixlen = 0; |
1161 | |
1162 /* no need for gcpro because we don't autoload any keymaps. */ | |
1130 | 1163 |
1131 if (!NILP (prefix)) | 1164 if (!NILP (prefix)) |
1132 prefixlen = XINT (Flength (prefix)); | 1165 prefixlen = XINT (Flength (prefix)); |
1133 | 1166 |
1134 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil), | 1167 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil), |
1283 return Fnreverse (good_maps); | 1316 return Fnreverse (good_maps); |
1284 } | 1317 } |
1285 | 1318 |
1286 Lisp_Object Qsingle_key_description, Qkey_description; | 1319 Lisp_Object Qsingle_key_description, Qkey_description; |
1287 | 1320 |
1321 /* This function cannot GC. */ | |
1322 | |
1288 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0, | 1323 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0, |
1289 "Return a pretty description of key-sequence KEYS.\n\ | 1324 "Return a pretty description of key-sequence KEYS.\n\ |
1290 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\ | 1325 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\ |
1291 spaces are put between sequence elements, etc.") | 1326 spaces are put between sequence elements, etc.") |
1292 (keys) | 1327 (keys) |
1437 } | 1472 } |
1438 | 1473 |
1439 return p; | 1474 return p; |
1440 } | 1475 } |
1441 | 1476 |
1477 /* This function cannot GC. */ | |
1478 | |
1442 DEFUN ("single-key-description", Fsingle_key_description, Ssingle_key_description, 1, 1, 0, | 1479 DEFUN ("single-key-description", Fsingle_key_description, Ssingle_key_description, 1, 1, 0, |
1443 "Return a pretty description of command character KEY.\n\ | 1480 "Return a pretty description of command character KEY.\n\ |
1444 Control characters turn into C-whatever, etc.") | 1481 Control characters turn into C-whatever, etc.") |
1445 (key) | 1482 (key) |
1446 Lisp_Object key; | 1483 Lisp_Object key; |
1491 else | 1528 else |
1492 *p++ = c; | 1529 *p++ = c; |
1493 return p; | 1530 return p; |
1494 } | 1531 } |
1495 | 1532 |
1533 /* This function cannot GC. */ | |
1534 | |
1496 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0, | 1535 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0, |
1497 "Return a pretty description of file-character CHAR.\n\ | 1536 "Return a pretty description of file-character CHAR.\n\ |
1498 Control characters turn into \"^char\", etc.") | 1537 Control characters turn into \"^char\", etc.") |
1499 (chr) | 1538 (chr) |
1500 Lisp_Object chr; | 1539 Lisp_Object chr; |
1531 return 1; | 1570 return 1; |
1532 } | 1571 } |
1533 | 1572 |
1534 | 1573 |
1535 /* where-is - finding a command in a set of keymaps. */ | 1574 /* where-is - finding a command in a set of keymaps. */ |
1575 | |
1576 /* This function can GC if Flookup_key autoloads any keymaps. */ | |
1536 | 1577 |
1537 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0, | 1578 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0, |
1538 "Return list of keys that invoke DEFINITION.\n\ | 1579 "Return list of keys that invoke DEFINITION.\n\ |
1539 If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\ | 1580 If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\ |
1540 If KEYMAP is nil, search all the currently active keymaps.\n\ | 1581 If KEYMAP is nil, search all the currently active keymaps.\n\ |
1551 indirect definition itself.") | 1592 indirect definition itself.") |
1552 (definition, keymap, firstonly, noindirect) | 1593 (definition, keymap, firstonly, noindirect) |
1553 Lisp_Object definition, keymap; | 1594 Lisp_Object definition, keymap; |
1554 Lisp_Object firstonly, noindirect; | 1595 Lisp_Object firstonly, noindirect; |
1555 { | 1596 { |
1556 register Lisp_Object maps; | 1597 Lisp_Object maps; |
1557 Lisp_Object found; | 1598 Lisp_Object found, sequence; |
1558 int keymap_specified = !NILP (keymap); | 1599 int keymap_specified = !NILP (keymap); |
1600 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; | |
1559 | 1601 |
1560 if (! keymap_specified) | 1602 if (! keymap_specified) |
1561 { | 1603 { |
1562 #ifdef USE_TEXT_PROPERTIES | 1604 #ifdef USE_TEXT_PROPERTIES |
1563 keymap = get_local_map (PT, current_buffer); | 1605 keymap = get_local_map (PT, current_buffer); |
1585 maps); | 1627 maps); |
1586 minors = XCONS (minors)->cdr; | 1628 minors = XCONS (minors)->cdr; |
1587 } | 1629 } |
1588 } | 1630 } |
1589 | 1631 |
1632 GCPRO5 (definition, keymap, maps, found, sequence); | |
1590 found = Qnil; | 1633 found = Qnil; |
1634 sequence = Qnil; | |
1591 | 1635 |
1592 for (; !NILP (maps); maps = Fcdr (maps)) | 1636 for (; !NILP (maps); maps = Fcdr (maps)) |
1593 { | 1637 { |
1594 /* Key sequence to reach map, and the map that it reaches */ | 1638 /* Key sequence to reach map, and the map that it reaches */ |
1595 register Lisp_Object this, map; | 1639 register Lisp_Object this, map; |
1620 | 1664 |
1621 For this reason, if Fcar (map) is a vector, we don't | 1665 For this reason, if Fcar (map) is a vector, we don't |
1622 advance map to the next element until i indicates that we | 1666 advance map to the next element until i indicates that we |
1623 have finished off the vector. */ | 1667 have finished off the vector. */ |
1624 | 1668 |
1625 Lisp_Object elt, key, binding, sequence; | 1669 Lisp_Object elt, key, binding; |
1626 elt = XCONS (map)->car; | 1670 elt = XCONS (map)->car; |
1627 | 1671 |
1628 QUIT; | 1672 QUIT; |
1629 | 1673 |
1630 /* Set key and binding to the current key and binding, and | 1674 /* Set key and binding to the current key and binding, and |
1729 /* If firstonly is Qnon_ascii, then we can return the first | 1773 /* If firstonly is Qnon_ascii, then we can return the first |
1730 binding we find. If firstonly is not Qnon_ascii but not | 1774 binding we find. If firstonly is not Qnon_ascii but not |
1731 nil, then we should return the first ascii-only binding | 1775 nil, then we should return the first ascii-only binding |
1732 we find. */ | 1776 we find. */ |
1733 if (EQ (firstonly, Qnon_ascii)) | 1777 if (EQ (firstonly, Qnon_ascii)) |
1734 return sequence; | 1778 RETURN_UNGCPRO (sequence); |
1735 else if (! NILP (firstonly) && ascii_sequence_p (sequence)) | 1779 else if (! NILP (firstonly) && ascii_sequence_p (sequence)) |
1736 return sequence; | 1780 RETURN_UNGCPRO (sequence); |
1737 } | 1781 } |
1738 } | 1782 } |
1783 | |
1784 UNGCPRO; | |
1739 | 1785 |
1740 found = Fnreverse (found); | 1786 found = Fnreverse (found); |
1741 | 1787 |
1742 /* firstonly may have been t, but we may have gone all the way through | 1788 /* firstonly may have been t, but we may have gone all the way through |
1743 the keymaps without finding an all-ASCII key sequence. So just | 1789 the keymaps without finding an all-ASCII key sequence. So just |