Subject: bug#56873: Make `defvar-keymap' warn on conflicting and redundant bindings

From: Robert Pluim <[email-masked]>
Date: Tue, 02 Aug 2022 10:25:46 +0200

>>>>> On Mon, 1 Aug 2022 16:47:10 +0000, Stefan Kangas <[email-masked]> said:

    Stefan> Severity: wishlist
    Stefan> It would be useful if `defvar-keymap' could warn on conflicting
    Stefan> bindings, such as in:

    Stefan>     (defvar-keymap foo
    Stefan>       "a" #'next-line
    Stefan>       "a" #'previous-line)

Is that a common occurence?

    Stefan> It would also be useful to warn about redundant bindings, such as in:

    Stefan>     (defvar-keymap foo
    Stefan>       "a" #'next-line
    Stefan>       "a" #'next-line)

Thatʼs just a special case of conflicting bindings. This will do it,
but I wonder if `warn' is overkill. I put it in `define-keymap', but
it could equally well go in `defvar-keymap'.

diff --git a/lisp/keymap.el b/lisp/keymap.el
index 376a30f106..b44a961d73 100644
--- a/lisp/keymap.el
+++ b/lisp/keymap.el
@@ -530,7 +530,8 @@ define-keymap
                    (keymap keymap)
                    (prefix (define-prefix-command prefix nil name))
                    (full (make-keymap name))
-                   (t (make-sparse-keymap name)))))
+                   (t (make-sparse-keymap name))))
+          seen-keys)
       (when suppress
         (suppress-keymap keymap (eq suppress 'nodigits)))
       (when parent
@@ -544,6 +545,9 @@ define-keymap
           (let ((def (pop definitions)))
             (if (eq key :menu)
                 (easy-menu-define nil keymap "" def)
+              (if (member key seen-keys)
+                  (warn "Duplicate definition for key: %S" key)
+                (push key seen-keys))
               (keymap-set keymap key def)))))
       keymap)))

Robert
--