Lift define-prefix-command to Lisp

* lisp/subr.el (define-prefix-command): New defun.
* src/keymap.c (Fdefine_prefix_command): Remove DEFUN.
(syms_of_keymap): Remove defsubr for Fdefine_prefix_command.
* test/lisp/subr-tests.el (subr-test-define-prefix-command): New
test.
This commit is contained in:
Stefan Kangas 2021-01-08 15:16:02 +01:00
parent f5cfe5a0a9
commit 5ac7b48075
3 changed files with 27 additions and 23 deletions

View file

@ -995,6 +995,22 @@ a menu, so this function is not useful for non-menu keymaps."
(setq inserted t)))
(setq tail (cdr tail)))))
(defun define-prefix-command (command &optional mapvar name)
"Define COMMAND as a prefix command. COMMAND should be a symbol.
A new sparse keymap is stored as COMMAND's function definition and its
value.
This prepares COMMAND for use as a prefix key's binding.
If a second optional argument MAPVAR is given, it should be a symbol.
The map is then stored as MAPVAR's value instead of as COMMAND's
value; but COMMAND is still defined as a function.
The third optional argument NAME, if given, supplies a menu name
string for the map. This is required to use the keymap as a menu.
This function returns COMMAND."
(let ((map (make-sparse-keymap name)))
(fset command map)
(set (or mapvar command) map)
command))
(defun map-keymap-sorted (function keymap)
"Implement `map-keymap' with sorting.
Don't call this function; it is for internal use only."

View file

@ -1712,28 +1712,6 @@ bindings; see the description of `lookup-key' for more details about this. */)
return Flist (j, maps);
}
DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0,
doc: /* Define COMMAND as a prefix command. COMMAND should be a symbol.
A new sparse keymap is stored as COMMAND's function definition and its
value.
This prepares COMMAND for use as a prefix key's binding.
If a second optional argument MAPVAR is given, it should be a symbol.
The map is then stored as MAPVAR's value instead of as COMMAND's
value; but COMMAND is still defined as a function.
The third optional argument NAME, if given, supplies a menu name
string for the map. This is required to use the keymap as a menu.
This function returns COMMAND. */)
(Lisp_Object command, Lisp_Object mapvar, Lisp_Object name)
{
Lisp_Object map = Fmake_sparse_keymap (name);
Ffset (command, map);
if (!NILP (mapvar))
Fset (mapvar, map);
else
Fset (command, map);
return command;
}
DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
doc: /* Select KEYMAP as the global keymap. */)
(Lisp_Object keymap)
@ -3280,7 +3258,6 @@ be preferred. */);
defsubr (&Sminor_mode_key_binding);
defsubr (&Sdefine_key);
defsubr (&Slookup_key);
defsubr (&Sdefine_prefix_command);
defsubr (&Suse_global_map);
defsubr (&Suse_local_map);
defsubr (&Scurrent_local_map);

View file

@ -70,6 +70,17 @@
(should (equal (kbd "RET") "\C-m"))
(should (equal (kbd "C-x a") "\C-xa")))
(ert-deftest subr-test-define-prefix-command ()
(define-prefix-command 'foo-prefix-map)
(should (keymapp foo-prefix-map))
(should (fboundp #'foo-prefix-map))
;; With optional argument.
(define-prefix-command 'bar-prefix 'bar-prefix-map)
(should (keymapp bar-prefix-map))
(should (fboundp #'bar-prefix))
;; Returns the symbol.
(should (eq (define-prefix-command 'foo-bar) 'foo-bar)))
;;;; Mode hooks.