* Mapping file types to major modes
@ 2022-09-23 15:45 Stefan Monnier
2022-09-23 16:09 ` Jean Louis
2022-09-24 19:51 ` Juri Linkov
0 siblings, 2 replies; 3+ messages in thread
From: Stefan Monnier @ 2022-09-23 15:45 UTC (permalink / raw)
To: emacs-devel
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Mapping file types to major modes
2022-09-23 15:45 Mapping file types to major modes Stefan Monnier
@ 2022-09-23 16:09 ` Jean Louis
2022-09-24 19:51 ` Juri Linkov
1 sibling, 0 replies; 3+ messages in thread
From: Jean Louis @ 2022-09-23 16:09 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
* Stefan Monnier <monnier@iro.umontreal.ca> [2022-09-23 18:48]:
> 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.
I agree on good idea that it would be helpful that major modes
correspond to file types, though practically it is not so easy to
determine it. Example is asciidoctor that can in some cases look
completely same as markdown, also txt2tags that may look as markdown
and similar lightweight markup languages. Additionally many
applications produce .txt files which are markdown formatted.
I suggest that upon first invocation where mode is unclear that Emacs
asks user about preferred mode and give him option to understand
descriptions and differences of such modes.
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Mapping file types to major modes
2022-09-23 15:45 Mapping file types to major modes Stefan Monnier
2022-09-23 16:09 ` Jean Louis
@ 2022-09-24 19:51 ` Juri Linkov
1 sibling, 0 replies; 3+ messages in thread
From: Juri Linkov @ 2022-09-24 19:51 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
> So all it takes is to add some "mode remapping" layer to decide which
> major mode to actually use when the "type/mode".
Such layer looks like `mime.types` with e.g.
# MIME media types and the extensions that represent them.
text/x-perl pl pm
text/html html htm shtml
...
and `mailcap` with e.g.
text/html; firefox %s; test=test -n "$DISPLAY"
text/html; /usr/bin/sensible-browser %s; nametemplate=%s.html
...
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-09-24 19:51 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-23 15:45 Mapping file types to major modes Stefan Monnier
2022-09-23 16:09 ` Jean Louis
2022-09-24 19:51 ` Juri Linkov
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).