Subject: bug#54074: 29.0.50; Feature request emacs keymap-set and minor-modes

From: Juri Linkov <[email-masked]>
Date: Mon, 21 Feb 2022 10:28:07 +0200

> 1)
>
> With that then could be trivial to implemented something like
> use-packages :bind, so
>
> (define-minor-mode my-mode
>     :global t
>     :keymap (my-mode-map
>              :map isearch-mode-map my-mode-isearch-map))
>
>
> or similar... I am wondering how nobody have requested this before, when
> it is a very common issue I find in many packages around.

You might be interested in how the composite keymaps are defined here:

```
(define-minor-mode outline-minor-mode
  :keymap (easy-mmode-define-keymap
           `(([menu-bar] . ,outline-minor-mode-menu-bar-map)
             (,outline-minor-mode-prefix . ,outline-mode-prefix-map))
           :inherit outline-minor-mode-cycle-map)
```

Maybe such keymap definitions could be simplified.

> 2)
>
> The best approach for this one is to save the original value in an alist
> and assign with a wrapper around setq...  and use a loop when disabling
> the mode to restore the original values.... it is not perfect, but works
> in many cases...
> [...]
> Does it makes sense??

Yes, this makes sense.  We used the functions copied below
until you proposed to move keybindings to context-menu-mode-map:

```
(defvar context-menu--saved-bindings nil
  "Alist of bindings to restore when `context-menu-mode' is disabled.")

(defun context-menu--bind-mouse (click-sym down-sym)
  "Enable `context-menu-mode' mouse bindings.
CLICK-SYM and DOWN-SYM are the mouse click and down key symbols to use."
  (let ((click (vector click-sym))
        (down (vector down-sym)))
    (push (cons click-sym (global-key-binding click))
          context-menu--saved-bindings)
    (global-unset-key click)
    (push (cons down-sym (global-key-binding down))
          context-menu--saved-bindings)
    (global-set-key down context-menu-entry)))

(defun context-menu--restore-bindings ()
  "Restore saved `context-menu-mode' bindings."
  (pcase-dolist (`(,sym . ,binding) context-menu--saved-bindings)
    (let ((key (vector sym)))
      (if binding
          (global-set-key key binding)
        (global-unset-key key)))))
```