Subject: 30.0.50; Multiple major mode parents
From: Stefan Monnier <[email-masked]>
To: [email-masked]
Date: Thu, 09 Nov 2023 00:38:22 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

Package: Emacs
Version: 30.0.50

While it seems difficult to add support for multiple inheritance to
`define-derived-mode`, it's fairly easy to allow a major mode to declare
itself the spiritual heir of various other major modes beside the one
from which it actually inherits.

We already have some very basic such cases in our own code:
- `locate-mode` inherits from `special-mode` but declares itself
  as a child of `dired-mode`.
- CEDET declares that `c++-mode` is a child of `c-mode`.

I'd been toying with this idea for a while because it has seemed useful
a few times, tho until now there seemed to be good enough alternatives.
But in order to really support AUCTeX modes well, we do need such
a thing: we need `LaTeX-mode` to be able to declare itself as a child
of `latex-mode` (even tho it does not inherit from it) so that
directory-local variables are properly applied to it, which is
a fairly common use case.

I pushed to the branch `feature/derived-mode-add-parents` a bunch of
patches which add support for such limited form of `multiple
inheritance`.

The patch is fairly large because it goes through all the code that
uses the `derived-mode-parent` property and adjusts it to use
a few new functions:

- `derived-mode-set-parent` and `derived-mode-all-parents` cover
  basically all previous uses of the `derived-mode-parent` property.
- `derived-mode-add-parents` to declare additional parents.

The patch also consolidates the code that linearizes the inheritance
hierarchy of CL classes, EIEIO classes, and major modes.

You can see below the corresponding commit log.
There are no doc (or etc/NEWS) changes yet.
Any comment/objection?

        Stefan

commit 19445b6b7bb04e44e39ef2e39a620bd3eadb0acd
Author: Stefan Monnier <[email-masked]>
Date:   Wed Nov 8 11:32:27 2023 -0500

    subr.el: Provide a functional API around `derived-mode-parent`
    
    The `derived-mode-parent` property should be an implementation detail,
    so we can change it more easily.  To that end, add functions to set and
    query it.
    
    * lisp/subr.el (derived-mode-all-parents): New function.
    (provided-mode-derived-p): Use it.
    (derived-mode-set-parent): New function.

commit 9c6b22bb3e2126a1ab355b81ae4268ac53c2b6fe
Author: Stefan Monnier <[email-masked]>
Date:   Wed Nov 8 14:20:09 2023 -0500

    (derived-mode-all-parents): Speed up with a cache
    
    Most uses of the mode hierarchy don't really need to construct the
    list, they just need to iterate over it.  With single inheritance
    we could do it just by jumping up from a mode to its parent,
    but to support the upcoming multiple inheritance we'd need a more
    complex and costly iterator.
    Luckily, the inheritance graph is mostly static so we can cache
    the list of all parents, making `derived-mode-all-parents` cheap
    enough to be the basis of iteration and keeping the API very simple.
    
    * lisp/subr.el (derived-mode-all-parents): Cache the result.
    (derived-mode--flush): New function.
    (derived-mode-set-parent): Use it.

commit 492920dd5b469e18596a49a62fbefd8ad2cc518b
Author: Stefan Monnier <[email-masked]>
Date:   Wed Nov 8 22:53:39 2023 -0500

    Use new `derived-mode-all/set-parents` functions.
    
    Try and avoid using the `derived-mode-parent` property directly
    and use the new API functions instead.
    
    * lisp/emacs-lisp/derived.el (define-derived-mode):
    Use `derived-mode-set-parent`.
    
    * lisp/loadhist.el (unload--set-major-mode):
    * lisp/info-look.el (info-lookup-select-mode):
    * lisp/ibuf-ext.el (ibuffer-list-buffer-modes):
    * lisp/files.el (dir-locals--get-sort-score):
    * lisp/emacs-lisp/cl-generic.el (cl--generic-derived-specializers):
    Use `derived-mode-all-parents`.

commit 5afa55a946a0271c624359e9de5d62bcaf39729b
Author: Stefan Monnier <[email-masked]>
Date:   Mon Nov 6 16:57:05 2023 -0500

    subr.el: Add multiple inheritance to `derived-mode-p`
    
    Add the ability for a major mode to declare "extra parents" in
    addition to the one from which it inherits.
    
    * lisp/subr.el (derived-mode-add-parents): New function.
    (derived-mode-all-parents): Adjust accordingly.

commit 8323394bc801e01dedd95e0ff8d573dd1f5e34ba
Author: Stefan Monnier <[email-masked]>
Date:   Mon Nov 6 19:05:40 2023 -0500

    Use `derived-mode-add-parents` in remaining uses of `derived-mode-parent`
    
    Until now multiple inheritance wasn't really used, but some ad-hoc
    code went a bit beyond the normal uses of the mode hierarchy.
    Use the new multiple inheritance code to replace that ad-hoc code,
    thereby eliminating basically all remaining direct uses of the
    `derived-mode-parent` property.
    
    CEDET had its own notion of mode hierrchy using `derived-mode-parent`
    as well as its own `mode-local-parent` property set via
    `define-child-mode`.
    `derived-mode-add-parents` lets us reimplement `define-child-mode`
    such that CEDET can now use the normal API functions.
    
    * lisp/locate.el (locate-mode): Use `derived-mode-add-parents`.
    
    * lisp/cedet/mode-local.el (get-mode-local-parent): Declare obsolete.
    (mode-local-equivalent-mode-p, mode-local-use-bindings-p): Make them
    obsolete aliases.
    (mode-local--set-parent): Rewrite to use `derived-mode-add-parents`.
    Declare as obsolete.
    (mode-local-map-mode-buffers): Use `derived-mode-p`.
    (mode-local-symbol, mode-local--activate-bindings)
    (mode-local--deactivate-bindings, mode-local-describe-bindings-2):
    Use `derived-mode-all-parents`.
    
    * lisp/cedet/srecode/table.el (srecode-get-mode-table):
    * lisp/cedet/srecode/find.el (srecode-table, srecode-load-tables-for-mode)
    (srecode-all-template-hash): Use `derived-mode-all-parents`.
    
    * lisp/cedet/srecode/map.el (srecode-map-entries-for-mode):
    * lisp/cedet/semantic/db.el (semanticdb-equivalent-mode):
    Use `provided-mode-derived-p` now that it obeys `define-child-mode`.

commit 0939433b63ab45d18fe1a2db706f66efe7307261
Author: Stefan Monnier <[email-masked]>
Date:   Tue Nov 7 18:57:03 2023 -0500

    Move EIEIO's C3 linearization code to `subr.el`
    
    The code was used to linearize the EIEIO class hierarchy, since
    it results in saner results than things like BFS or DFS.
    By moving it to `subr.el` we get to benefit from that same
    advantage both in `cl--class-allparents` and
    in `derived-mode-all-parents`.
    
    * lisp/subr.el (merge-ordered-lists): New function.
    (derived-mode-all-parents): Use it to improve parent ordering.
    
    * lisp/emacs-lisp/eieio-core.el (eieio--c3-candidate)
    (eieio--c3-merge-lists): Delete functions, replaced by
    `merge-ordered-lists`.
    (eieio--class-precedence-c3): Use `merge-ordered-lists`.
    
    * lisp/emacs-lisp/cl-preloaded.el (cl--class-allparents):
    Use `merge-ordered-lists` to improve parent ordering.
    * lisp/emacs-lisp/cl-macs.el (cl--struct-all-parents): Delete function.
    (cl--pcase-mutually-exclusive-p): Use `cl--class-allparents` instead.