Don't print empty keymaps in `C-h b'

* lisp/help.el (describe-map-tree): Don't print the heading if the
keymap is empty (bug#22334).  Tweak the look to compress it more
vertically.
This commit is contained in:
Lars Ingebrigtsen 2021-11-01 03:18:02 +01:00
parent 686d85c767
commit 1200ecba27
2 changed files with 46 additions and 53 deletions

View file

@ -1275,17 +1275,8 @@ Any inserted text ends in two newlines (used by
result)) result))
amaps)) amaps))
(maps orig-maps) (maps orig-maps)
(print-title (or maps always-title))) (print-title (or maps always-title))
;; Print title. (start-point (point)))
(when print-title
(insert (concat (if title
(concat title
(if prefix
(concat " Starting With "
(help--key-description-fontified prefix)))
":\n"))
"key binding\n"
"--- -------\n")))
;; Describe key bindings. ;; Describe key bindings.
(setq help--keymaps-seen nil) (setq help--keymaps-seen nil)
(while (consp maps) (while (consp maps)
@ -1310,8 +1301,24 @@ Any inserted text ends in two newlines (used by
(describe-map (cdr elt) elt-prefix transl partial (describe-map (cdr elt) elt-prefix transl partial
sub-shadows no-menu mention-shadow))) sub-shadows no-menu mention-shadow)))
(setq maps (cdr maps))) (setq maps (cdr maps)))
(when print-title ;; Print title...
(insert "\n")))) (when (and print-title
;; ... unless the keymap was empty.
(/= (point) start-point))
(save-excursion
(goto-char start-point)
(when (eolp)
(delete-region (point) (1+ (point))))
(insert
(concat
(if title
(concat title
(if prefix
(concat " Starting With "
(help--key-description-fontified prefix)))
":\n"))
"\nkey binding\n"
(make-separator-line)))))))
(defun help--shadow-lookup (keymap key accept-default remap) (defun help--shadow-lookup (keymap key accept-default remap)
"Like `lookup-key', but with command remapping. "Like `lookup-key', but with command remapping.

View file

@ -91,10 +91,9 @@
(ert-deftest help-tests-substitute-command-keys/keymaps () (ert-deftest help-tests-substitute-command-keys/keymaps ()
(with-substitute-command-keys-test (with-substitute-command-keys-test
(test "\\{minibuffer-local-must-match-map}" (test "\\{minibuffer-local-must-match-map}"
"\ "
key binding key binding
--- ------- -------------------------------------------------------------------------------
C-g abort-minibuffers C-g abort-minibuffers
TAB minibuffer-complete TAB minibuffer-complete
C-j minibuffer-complete-and-exit C-j minibuffer-complete-and-exit
@ -122,7 +121,6 @@ M-r previous-matching-history-element
M-s next-matching-history-element M-s next-matching-history-element
M-g M-c switch-to-completions M-g M-c switch-to-completions
"))) ")))
(ert-deftest help-tests-substitute-command-keys/keymap-change () (ert-deftest help-tests-substitute-command-keys/keymap-change ()
@ -250,10 +248,9 @@ M-g M-c switch-to-completions
(with-temp-buffer (with-temp-buffer
(help-tests-major-mode) (help-tests-major-mode)
(test "\\{help-tests-major-mode-map}" (test "\\{help-tests-major-mode-map}"
"\ "
key binding key binding
--- ------- -------------------------------------------------------------------------------
( .. ) short-range ( .. ) short-range
1 .. 4 foo-range 1 .. 4 foo-range
a .. c foo-other-range a .. c foo-other-range
@ -261,7 +258,6 @@ a .. c foo-other-range
C-e foo-something C-e foo-something
x foo-original x foo-original
<F1> foo-function-key1 <F1> foo-function-key1
")))) "))))
(ert-deftest help-tests-substitute-command-keys/shadow () (ert-deftest help-tests-substitute-command-keys/shadow ()
@ -270,10 +266,9 @@ x foo-original
(help-tests-major-mode) (help-tests-major-mode)
(help-tests-minor-mode) (help-tests-minor-mode)
(test "\\{help-tests-major-mode-map}" (test "\\{help-tests-major-mode-map}"
"\ "
key binding key binding
--- ------- -------------------------------------------------------------------------------
( .. ) short-range ( .. ) short-range
1 .. 4 foo-range 1 .. 4 foo-range
a .. c foo-other-range a .. c foo-other-range
@ -283,7 +278,6 @@ C-e foo-something
x foo-original x foo-original
(this binding is currently shadowed) (this binding is currently shadowed)
<F1> foo-function-key1 <F1> foo-function-key1
")))) "))))
(ert-deftest help-tests-substitute-command-keys/command-remap () (ert-deftest help-tests-substitute-command-keys/command-remap ()
@ -293,14 +287,12 @@ x foo-original
(help-tests-major-mode) (help-tests-major-mode)
(define-key help-tests-major-mode-map [remap foo] 'bar) (define-key help-tests-major-mode-map [remap foo] 'bar)
(test "\\{help-tests-major-mode-map}" (test "\\{help-tests-major-mode-map}"
"\ "
key binding key binding
--- ------- -------------------------------------------------------------------------------
<remap> Prefix Command <remap> Prefix Command
<remap> <foo> bar <remap> <foo> bar
"))))) ")))))
(ert-deftest help-tests-describe-map-tree/no-menu-t () (ert-deftest help-tests-describe-map-tree/no-menu-t ()
@ -312,11 +304,10 @@ key binding
:enable mark-active :enable mark-active
:help "Help text")))))) :help "Help text"))))))
(describe-map-tree map nil nil nil nil t nil nil nil) (describe-map-tree map nil nil nil nil t nil nil nil)
(should (equal (buffer-string) "key binding (should (equal (buffer-string) "
--- ------- key binding
-------------------------------------------------------------------------------
C-a foo C-a foo
"))))) ")))))
(ert-deftest help-tests-describe-map-tree/no-menu-nil () (ert-deftest help-tests-describe-map-tree/no-menu-nil ()
@ -328,14 +319,13 @@ C-a foo
:enable mark-active :enable mark-active
:help "Help text")))))) :help "Help text"))))))
(describe-map-tree map nil nil nil nil nil nil nil nil) (describe-map-tree map nil nil nil nil nil nil nil nil)
(should (equal (buffer-string) "key binding (should (equal (buffer-string) "
--- ------- key binding
-------------------------------------------------------------------------------
C-a foo C-a foo
<menu-bar> Prefix Command <menu-bar> Prefix Command
<menu-bar> <foo> foo <menu-bar> <foo> foo
"))))) ")))))
(ert-deftest help-tests-describe-map-tree/mention-shadow-t () (ert-deftest help-tests-describe-map-tree/mention-shadow-t ()
@ -345,13 +335,12 @@ C-a foo
(2 . bar)))) (2 . bar))))
(shadow-maps '((keymap . ((1 . baz)))))) (shadow-maps '((keymap . ((1 . baz))))))
(describe-map-tree map t shadow-maps nil nil t nil nil t) (describe-map-tree map t shadow-maps nil nil t nil nil t)
(should (equal (buffer-string) "key binding (should (equal (buffer-string) "
--- ------- key binding
-------------------------------------------------------------------------------
C-a foo C-a foo
(this binding is currently shadowed) (this binding is currently shadowed)
C-b bar C-b bar
"))))) ")))))
(ert-deftest help-tests-describe-map-tree/mention-shadow-nil () (ert-deftest help-tests-describe-map-tree/mention-shadow-nil ()
@ -361,11 +350,10 @@ C-b bar
(2 . bar)))) (2 . bar))))
(shadow-maps '((keymap . ((1 . baz)))))) (shadow-maps '((keymap . ((1 . baz))))))
(describe-map-tree map t shadow-maps nil nil t nil nil nil) (describe-map-tree map t shadow-maps nil nil t nil nil nil)
(should (equal (buffer-string) "key binding (should (equal (buffer-string) "
--- ------- key binding
-------------------------------------------------------------------------------
C-b bar C-b bar
"))))) ")))))
(ert-deftest help-tests-describe-map-tree/partial-t () (ert-deftest help-tests-describe-map-tree/partial-t ()
@ -374,11 +362,10 @@ C-b bar
(map '(keymap . ((1 . foo) (map '(keymap . ((1 . foo)
(2 . undefined))))) (2 . undefined)))))
(describe-map-tree map t nil nil nil nil nil nil nil) (describe-map-tree map t nil nil nil nil nil nil nil)
(should (equal (buffer-string) "key binding (should (equal (buffer-string) "
--- ------- key binding
-------------------------------------------------------------------------------
C-a foo C-a foo
"))))) ")))))
(ert-deftest help-tests-describe-map-tree/partial-nil () (ert-deftest help-tests-describe-map-tree/partial-nil ()
@ -387,12 +374,11 @@ C-a foo
(map '(keymap . ((1 . foo) (map '(keymap . ((1 . foo)
(2 . undefined))))) (2 . undefined)))))
(describe-map-tree map nil nil nil nil nil nil nil nil) (describe-map-tree map nil nil nil nil nil nil nil nil)
(should (equal (buffer-string) "key binding (should (equal (buffer-string) "
--- ------- key binding
-------------------------------------------------------------------------------
C-a foo C-a foo
C-b undefined C-b undefined
"))))) ")))))
(defvar help-tests--was-in-buffer nil) (defvar help-tests--was-in-buffer nil)