aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Kangas2021-01-13 18:54:09 +0100
committerStefan Kangas2021-01-13 18:54:09 +0100
commita9658cd5b07e88a5d413cbb4dfd8f9d9d0c8bbf5 (patch)
tree6c4703b44fd8f46052abce1dddb3ab26741dc5e3
parentbe9b7e83bc4191aff01c692be0f7a156ec4056da (diff)
downloademacs-a9658cd5b07e88a5d413cbb4dfd8f9d9d0c8bbf5.tar.gz
emacs-a9658cd5b07e88a5d413cbb4dfd8f9d9d0c8bbf5.zip
Lift {global,local}-key-binding to Lisp
* lisp/subr.el (local-key-binding, global-key-binding): New defuns. * src/keymap.c (Flocal_key_binding, Fglobal_key_binding): Remove DEFUNs. (syms_of_keymap): Remove defsubrs for above DEFUNs. * test/lisp/subr-tests.el (subr-test-local-key-binding) (subr-test-global-key-binding): New tests.
-rw-r--r--lisp/subr.el24
-rw-r--r--src/keymap.c35
-rw-r--r--test/lisp/subr-tests.el11
3 files changed, 35 insertions, 35 deletions
diff --git a/lisp/subr.el b/lisp/subr.el
index 6d3ea45c1ab..9b89e493702 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -1178,6 +1178,30 @@ KEY is a string or vector representing a sequence of keystrokes."
1178 (if (current-local-map) 1178 (if (current-local-map)
1179 (local-set-key key nil)) 1179 (local-set-key key nil))
1180 nil) 1180 nil)
1181
1182(defun local-key-binding (keys &optional accept-default)
1183 "Return the binding for command KEYS in current local keymap only.
1184KEYS is a string or vector, a sequence of keystrokes.
1185The binding is probably a symbol with a function definition.
1186
1187If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1188bindings; see the description of `lookup-key' for more details
1189about this."
1190 (let ((map (current-local-map)))
1191 (when map (lookup-key map keys accept-default))))
1192
1193(defun global-key-binding (keys &optional accept-default)
1194 "Return the binding for command KEYS in current global keymap only.
1195KEYS is a string or vector, a sequence of keystrokes.
1196The binding is probably a symbol with a function definition.
1197This function's return values are the same as those of `lookup-key'
1198\(which see).
1199
1200If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1201bindings; see the description of `lookup-key' for more details
1202about this."
1203 (lookup-key (current-global-map) keys accept-default))
1204
1181 1205
1182;;;; substitute-key-definition and its subroutines. 1206;;;; substitute-key-definition and its subroutines.
1183 1207
diff --git a/src/keymap.c b/src/keymap.c
index 1197f6fd4a5..de9b2b58c5e 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -1646,39 +1646,6 @@ specified buffer position instead of point are used.
1646 1646
1647/* GC is possible in this function if it autoloads a keymap. */ 1647/* GC is possible in this function if it autoloads a keymap. */
1648 1648
1649DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
1650 doc: /* Return the binding for command KEYS in current local keymap only.
1651KEYS is a string or vector, a sequence of keystrokes.
1652The binding is probably a symbol with a function definition.
1653
1654If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1655bindings; see the description of `lookup-key' for more details about this. */)
1656 (Lisp_Object keys, Lisp_Object accept_default)
1657{
1658 register Lisp_Object map = BVAR (current_buffer, keymap);
1659 if (NILP (map))
1660 return Qnil;
1661 return Flookup_key (map, keys, accept_default);
1662}
1663
1664/* GC is possible in this function if it autoloads a keymap. */
1665
1666DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
1667 doc: /* Return the binding for command KEYS in current global keymap only.
1668KEYS is a string or vector, a sequence of keystrokes.
1669The binding is probably a symbol with a function definition.
1670This function's return values are the same as those of `lookup-key'
1671\(which see).
1672
1673If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1674bindings; see the description of `lookup-key' for more details about this. */)
1675 (Lisp_Object keys, Lisp_Object accept_default)
1676{
1677 return Flookup_key (current_global_map, keys, accept_default);
1678}
1679
1680/* GC is possible in this function if it autoloads a keymap. */
1681
1682DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0, 1649DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
1683 doc: /* Find the visible minor mode bindings of KEY. 1650 doc: /* Find the visible minor mode bindings of KEY.
1684Return an alist of pairs (MODENAME . BINDING), where MODENAME is 1651Return an alist of pairs (MODENAME . BINDING), where MODENAME is
@@ -3253,8 +3220,6 @@ be preferred. */);
3253 defsubr (&Scopy_keymap); 3220 defsubr (&Scopy_keymap);
3254 defsubr (&Scommand_remapping); 3221 defsubr (&Scommand_remapping);
3255 defsubr (&Skey_binding); 3222 defsubr (&Skey_binding);
3256 defsubr (&Slocal_key_binding);
3257 defsubr (&Sglobal_key_binding);
3258 defsubr (&Sminor_mode_key_binding); 3223 defsubr (&Sminor_mode_key_binding);
3259 defsubr (&Sdefine_key); 3224 defsubr (&Sdefine_key);
3260 defsubr (&Slookup_key); 3225 defsubr (&Slookup_key);
diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el
index e0826208b60..fc5a1eba6d8 100644
--- a/test/lisp/subr-tests.el
+++ b/test/lisp/subr-tests.el
@@ -87,6 +87,17 @@
87 ;; Returns the symbol. 87 ;; Returns the symbol.
88 (should (eq (define-prefix-command 'foo-bar) 'foo-bar))) 88 (should (eq (define-prefix-command 'foo-bar) 'foo-bar)))
89 89
90(ert-deftest subr-test-local-key-binding ()
91 (with-temp-buffer
92 (emacs-lisp-mode)
93 (should (keymapp (local-key-binding [menu-bar])))
94 (should-not (local-key-binding [f12]))))
95
96(ert-deftest subr-test-global-key-binding ()
97 (should (eq (global-key-binding [f1]) 'help-command))
98 (should (eq (global-key-binding "x") 'self-insert-command))
99 (should-not (global-key-binding [f12])))
100
90 101
91;;;; Mode hooks. 102;;;; Mode hooks.
92 103