From e427472e6f79007996afd47fd87d1d41bf1dec62 Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Thu, 11 Mar 2021 05:28:58 +0100 Subject: [PATCH 1/2] Add new format for literal keybindings to substitute-command-keys * lisp/help.el (substitute-command-keys): Add new format for literal keybindings. * test/lisp/help-tests.el (help-tests-substitute-command-keys/literal-keybinding): (help-tests-substitute-key-bindings/face-help-key-binding): New tests. --- lisp/help.el | 34 ++++++++++++++++++++++++---------- test/lisp/help-tests.el | 14 ++++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lisp/help.el b/lisp/help.el index 79d8296cfe..d1a817a59d 100644 --- a/lisp/help.el +++ b/lisp/help.el @@ -984,6 +984,9 @@ substitute-command-keys keystroke sequence that invokes COMMAND, or \"M-x COMMAND\" if COMMAND is not on any keys. Keybindings will use the face `help-key-binding'. +Each substring of the form \\\\=[=KEYBINDING] will be replaced by +KEYBINDING and use the `help-key-binding' face. + Each substring of the form \\\\={MAPVAR} is replaced by a summary of the value of MAPVAR as a keymap. This summary is similar to the one produced by ‘describe-bindings’. The summary ends in two newlines @@ -1054,18 +1057,29 @@ substitute-command-keys (setq fun (aref key 1)) (setq key (with-current-buffer orig-buf (where-is-internal fun keymap t)))) - (if (not key) - ;; Function is not on any key. - (let ((op (point))) - (insert "M-x ") - (goto-char (+ end-point 3)) - (add-text-properties op (point) - '( face help-key-binding - font-lock-face help-key-binding)) - (delete-char 1)) + (cond + ((looking-at "=") + ;; Literal replacement. + (let ((op (point))) + (delete-char 1) + (goto-char (- end-point 2)) + (delete-char 1) + (add-text-properties op (point) + '( face help-key-binding + font-lock-face help-key-binding)))) + ((not key) + ;; Function is not on any key. + (let ((op (point))) + (insert "M-x ") + (goto-char (+ end-point 3)) + (add-text-properties op (point) + '( face help-key-binding + font-lock-face help-key-binding)) + (delete-char 1))) + (t ;; Function is on a key. (delete-char (- end-point (point))) - (insert (help--key-description-fontified key))))) + (insert (help--key-description-fontified key)))))) ;; 1D. \{foo} is replaced with a summary of the keymap ;; (symbol-value foo). ;; \ just sets the keymap used for \[cmd]. diff --git a/test/lisp/help-tests.el b/test/lisp/help-tests.el index b2fec5c1bd..09ac01ee64 100644 --- a/test/lisp/help-tests.el +++ b/test/lisp/help-tests.el @@ -88,6 +88,20 @@ help-tests-substitute-command-keys/commands (test "\\[emacs-version]\\[next-line]" "M-x emacs-versionC-n") (test-re "\\[emacs-version]`foo'" "M-x emacs-version[`'‘]foo['’]"))) +(ert-deftest help-tests-substitute-command-keys/literal-keybinding () + "Literal replacement." + (with-substitute-command-keys-test + (test "\\[=C-m]" "C-m") + (test "\\[=C-m]\\[=C-j]" "C-mC-j") + (test "foo\\[=C-m]bar\\[=C-j]baz" "fooC-mbarC-jbaz"))) + +(ert-deftest help-tests-substitute-key-bindings/face-help-key-binding () + (should (eq (get-text-property 0 'face (substitute-command-keys "\\[next-line]")) + 'help-key-binding)) + (should (eq (get-text-property 0 'face (substitute-command-keys "\\[=f]")) + 'help-key-binding))) + + (ert-deftest help-tests-substitute-command-keys/keymaps () (with-substitute-command-keys-test (test "\\{minibuffer-local-must-match-map}" -- 2.30.1