emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Mapping file types to major modes


From: Stefan Monnier
Subject: Mapping file types to major modes
Date: Fri, 23 Sep 2022 11:45:46 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Hi,

Emacs has various facilities to decide which major mode to use for which
file based on its name and/or contents.  All those facilities specify
directly the mode to use.  This is usually exactly what we want because
there's only ever 1 major mode for that type.

But for some file types this is inconvenient because there are various
alternative major modes that can be used for that file type.
I'm thinking of things like Perl files (`perl-mode` vs `cperl-mode`),
LaTeX files (`latex-mode` vs AUCTeX's equivalent (whose name it not
clear, sadly)), HTML files (lots of alternatives), but also XPM
(`c-mode` or `image-mode`), Postscript (`ps-mode` vs `doc-view-mode`),
and we could even include the qualitatively different cases of `.tex`
files (`tex-mode` or `latex-mode`), or `.pl` files (`prolog-mode` vs
`perl-mode`), ...

We have various ways to deal with those different cases, which are
variously convenient/successful, but for things like `cperl-mode` vs
`perl-mode` or `latex-mode` vs AUCTeX the usual solution is not
satisfactory since it looks like:

    (defalias 'perl-mode #'cperl-mode)

or equivalent, which means that the "other" mode becomes unavailable
(AUCTeX now does it via advising which at least makes it reversible,
but it's still not very satisfactory).

The way I tend to think of it is that we'd like to divide the mode
choice into two steps:

A) recognize the file type.
B) choose the mode to use for that type.

But that's not what we have and retro-fitting that might make the common
case (where there's only 1 mode) more painful.  IOW it would take work
and lead to a worse result in the most common case, which doesn't sound
very enticing.

So instead I suggest we *treat* the current mode entries in
`auto-mode-alist` (and `mode:` cookies, `magic-mode-alist`, ...) as
types rather than as mode names.

So all it takes is to add some "mode remapping" layer to decide which
major mode to actually use when the "type/mode".  So instead of

    (defalias 'perl-mode #'cperl-mode)

We could have

    (add-to-list 'major-mode-alist '(perl-mode . cperl-mode))

I'm thinking of using an implementation with:

    (cl-defgeneric major-mode-remap (mode)
      "Choose which mode to use for MODE."
      (or (alist-get mode major-mode-alist) mode))

Such that we can also use dynamic remapping (via `cl-defmethod`) e.g for
`.tex` files.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]