Subject: Re: bug#74492: 31.0.50; [FR] explicit minor mode precedence

From: Stefan Monnier <[email-masked]>
Date: Sat, 23 Nov 2024 22:35:53 -0500

>> BTW, maybe we should add some notion of minor mode precedence since such
>> problems are actually fairly common.  We could do something similar to
>> what we do with `add-hook`, so `add-minor-mode` takes care of obeying
>> the ordering constraints.
>
> Currently, the last minor mode to be defined takes precedence over
> previously defined minor modes, at least in terms of keybindings.  This
> makes it hard-to-impossible for minor modes that need high precedence to
> "guard" their keybindings from minor modes defined at any later time.
> An explicit mechanism for specifying minor mode precedence could help.

We should first try and figure out what it is that's really needed, tho.
So I took a quick look at Emacs's own code and so far I found:

    ;; Define-minor-mode added our keymap to minor-mode-map-alist, but we want it
    ;; on minor-mode-overriding-map-alist instead.
    (setq minor-mode-map-alist
          (delq (assq 'completion-in-region-mode minor-mode-map-alist)
                minor-mode-map-alist))

in `minibuffer.el` which is related but can't just use a precedence
because we want to change the keymap dynamically anyway.

    ;; Add a custom keymap for `visual-line-mode' so that activating
    ;; this minor mode does not override Org's keybindings.
    ;; FIXME: Probably `visual-line-mode' should take care of this.
    (let ((oldmap (cdr (assoc 'visual-line-mode minor-mode-map-alist)))
          (newmap (make-sparse-keymap)))
      (set-keymap-parent newmap oldmap)
      (define-key newmap [remap move-beginning-of-line] nil)
      (define-key newmap [remap move-end-of-line] nil)
      (define-key newmap [remap kill-line] nil)
      (make-local-variable 'minor-mode-overriding-map-alist)
      (push `(visual-line-mode . ,newmap) minor-mode-overriding-map-alist))

in `org.el` which is related but couldn't just use a precedence either.

    ;; Make sure we are first in minor-mode-map-alist
    (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
      ;; FIXME: maybe it should use emulation-mode-map-alists?
      (and c (setq minor-mode-map-alist
                   (cons c (delq c minor-mode-map-alist)))))

in `org-table.el` should could definitely use a precedence.

and that's it.  There used to be more for things like IDO or Viper, but
IIRC that was solved with the introduction of `emulation-mode-map-alists`.

I know there are more cases of "minor mode precedence" issues.
E.g. I remember seeing recently an issue in VM where the `w3m` minor
mode bindings used when rendering some HTML email body shadowed the
buffer's major mode, but again a case where precedence between minor
modes wouldn't help.

I don't think the examples above justify adding the kind of precedences
I suggested.  Maybe if we find more examples out there in the wild, the
picture might change.  Otherwise, maybe we should look for something
else that would solve the kinds of problems listed here.

        Stefan