all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: emacs-devel@gnu.org
Subject: Mapping file types to major modes
Date: Fri, 23 Sep 2022 11:45:46 -0400	[thread overview]
Message-ID: <jwvv8pem804.fsf-monnier+emacs@gnu.org> (raw)

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	other threads:[~2022-09-23 15:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-23 15:45 Stefan Monnier [this message]
2022-09-23 16:09 ` Mapping file types to major modes Jean Louis
2022-09-24 19:51 ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvv8pem804.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.