From: Lars Ingebrigtsen Date: Tue, 3 May 2022 14:19:50 +0000 (+0200) Subject: Fix key-parse problem with C-x ( ... sequences X-Git-Tag: emacs-29.0.90~1931^2~104 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=41e946f46e23756b7c732efdf3c5152fa8241dde;p=emacs.git Fix key-parse problem with C-x ( ... sequences * lisp/keymap.el (key-parse): Move the read-kbd-macro compat code from here... * lisp/subr.el (kbd): ... to here. (And fix the logic, too.) This allows `key-parse' to have a less puzzling result while maintaining backwards compatibility (bug#38775). --- diff --git a/lisp/keymap.el b/lisp/keymap.el index db37d80b363..71454eba5e5 100644 --- a/lisp/keymap.el +++ b/lisp/keymap.el @@ -281,18 +281,7 @@ See `kbd' for a descripion of KEYS." (when key (dolist (_ (number-sequence 1 times)) (setq res (vconcat res key)))))) - (if (and (>= (length res) 4) - (eq (aref res 0) ?\C-x) - (eq (aref res 1) ?\() - (eq (aref res (- (length res) 2)) ?\C-x) - (eq (aref res (- (length res) 1)) ?\))) - (apply #'vector (let ((lres (append res nil))) - ;; Remove the first and last two elements. - (setq lres (cdr (cdr lres))) - (nreverse lres) - (setq lres (cdr (cdr lres))) - (nreverse lres))) - res)))) + res))) (defun key-valid-p (keys) "Say whether KEYS is a valid key. diff --git a/lisp/subr.el b/lisp/subr.el index cb7572423af..dec3b9190ed 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -941,6 +941,20 @@ Here's some example key sequences: For an approximate inverse of this, see `key-description'." (declare (pure t) (side-effect-free t)) (let ((res (key-parse keys))) + ;; For historical reasons, parse "C-x ( C-d C-x )" as "C-d", since + ;; `kbd' used to be a wrapper around `read-kbd-macro'. + (when (and (>= (length res) 4) + (eq (aref res 0) ?\C-x) + (eq (aref res 1) ?\() + (eq (aref res (- (length res) 2)) ?\C-x) + (eq (aref res (- (length res) 1)) ?\))) + (setq res (apply #'vector (let ((lres (append res nil))) + ;; Remove the first and last two elements. + (setq lres (cddr lres)) + (setq lres (nreverse lres)) + (setq lres (cddr lres)) + (nreverse lres))))) + (if (not (memq nil (mapcar (lambda (ch) (and (numberp ch) (<= 0 ch 127))) diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index 62cf2266d61..a4f531ea4e6 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -1053,5 +1053,9 @@ final or penultimate step during initialization.")) (should (equal (string-lines "foo\n\n\nbar" t t) '("foo\n" "bar")))) +(defun test-keymap-parse-macros () + (should (equal (key-parse "C-x ( C-d C-x )") [24 40 4 24 41])) + (should (equal (kbd "C-x ( C-d C-x )") ""))) + (provide 'subr-tests) ;;; subr-tests.el ends here