unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#58075: Mapping file types to major modes
@ 2022-09-25 15:38 Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-09-26 11:26 ` Lars Ingebrigtsen
  2022-09-28  8:15 ` Ikumi Keita
  0 siblings, 2 replies; 10+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-09-25 15:38 UTC (permalink / raw)
  To: 58075; +Cc: Juri Linkov, Ikumi Keita

[-- Attachment #1: Type: text/plain, Size: 2701 bytes --]

Tags: patch

[ I now see that I had sent this to `emacs-devel` rather than as a bug
  report, so here is the second try.  To make up for it, I include
  a suggested patch this time.  ]

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), ...

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,
which is particularly problematic if we want the "fancy/external" mode to
be a derived mode of the other.
(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 (a bit like `/etc/mime.types`).
B) choose the mode to use for that type (like `/etc/mailcap`).

We don't current have "types" 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 consider the current settings as being our
implementation of (A), and add (B) as some "mode remapping" layer to
decide which major mode to actually for a given "type/mode".
So instead of

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

We could have

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


-- Stefan


 In GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.16.0, Xaw3d scroll bars) of 2022-09-16 built on pastel
Repository revision: c15a53877a7db7aa3205cff3c943321ed81a1412
Repository branch: work
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Debian GNU/Linux 11 (bullseye)

Configured using:
 'configure -C --enable-checking --enable-check-lisp-object-type --with-modules --with-cairo --with-tiff=ifavailable
 'CFLAGS=-Wall -g3 -Og -Wno-pointer-sign'
 PKG_CONFIG_PATH=/home/monnier/lib/pkgconfig'


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: major-mode-remap.patch --]
[-- Type: text/patch, Size: 5628 bytes --]

diff --git a/doc/emacs/modes.texi b/doc/emacs/modes.texi
index c348130807c..56b779f8de6 100644
--- a/doc/emacs/modes.texi
+++ b/doc/emacs/modes.texi
@@ -454,6 +454,13 @@ Choosing Modes
 files, HTML/XML/SGML files, PostScript files, and Unix style Conf
 files.
 
+@vindex major-mode-remap-alist
+  Once a major mode is found, Emacs does a final check to see if the
+mode has been remapped by @code{major-mode-remap-alist}, in which case
+it uses the remapped mode instead.  This is used when several
+different major modes can be used for the same file type, so you can
+specify which mode you prefer.
+
 @findex normal-mode
   If you have changed the major mode of a buffer, you can return to
 the major mode Emacs would have chosen automatically, by typing
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index 983dfe2ec59..8e34fdf3640 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -2476,11 +2476,11 @@ Declare Form
 expander will call @var{expander} with that form as well as with
 @var{args}@dots{}, and @var{expander} can either return a new expression to use
 instead of the function call, or it can return just the form unchanged,
-to indicate that the function call should be left alone.  @var{expander} can
-be a symbol, or it can be a form @code{(lambda (@var{arg}) @var{body})} in
-which case @var{arg} will hold the original function call expression, and the
-(unevaluated) arguments to the function can be accessed using the function's
-formal arguments.
+to indicate that the function call should be left alone.
+
+To avoid syntactic redundancy, when @var{expander} is of the form
+@code{(lambda (@var{arg}) @var{body})} the function's formal arguments
+are automatically added to the lambda's list of arguments.
 
 @item (gv-expander @var{expander})
 Declare @var{expander} to be the function to handle calls to the macro (or
diff --git a/etc/NEWS b/etc/NEWS
index 0a5b7bc29c5..02b4ef97583 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -442,6 +442,15 @@ option) and can be set to nil to disable Just-in-time Lock mode.
 \f
 * Changes in Emacs 29.1
 
++++
+** New variable 'major-mode-remap-alist' to specify your favorite major modes.
+This variable lets you remap the default modes (e.g. 'perl-mode' or
+'latex-mode') to your favorite ones (e.g. 'cperl-mode' or
+'LaTeX-mode') without having to use 'defalias', which can have
+undesirable side effects.
+This applies to all modes specified via 'auto-mode-alist', file-local
+variables, etc...
+
 ---
 ** Emacs now supports Unicode Standard version 15.0.
 
diff --git a/lisp/files.el b/lisp/files.el
index 1e1ec6127de..939d734a83d 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -3331,6 +3331,7 @@ set-auto-mode
 matches the buffer beginning against `magic-mode-alist',
 compares the file name against the entries in `auto-mode-alist',
 then matches the buffer beginning against `magic-fallback-mode-alist'.
+It also obeys `major-mode-remap-alist'.
 
 If `enable-local-variables' is nil, or if the file name matches
 `inhibit-local-variables-regexps', this function does not check
@@ -3468,6 +3469,17 @@ set-auto-mode
     (unless done
       (set-buffer-major-mode (current-buffer)))))
 
+(defvar-local set-auto-mode--last nil
+  "Remember the mode we have set via `set-auto-mode-0'.")
+
+(defcustom major-mode-remap-alist nil
+  "Alist mapping file-specified mode to actual mode.
+Every entry is of the form (MODE . FUNCTION) which means that in order
+to activate the major mode MODE (specified via something like
+`auto-mode-alist', file-local variables, ...) we should actually call
+FUNCTION instead."
+  :type '(alist (symbol) (function)))
+
 ;; When `keep-mode-if-same' is set, we are working on behalf of
 ;; set-visited-file-name.  In that case, if the major mode specified is the
 ;; same one we already have, don't actually reset it.  We don't want to lose
@@ -3478,10 +3490,15 @@ set-auto-mode-0
 any aliases and compared to current major mode.  If they are the
 same, do nothing and return nil."
   (unless (and keep-mode-if-same
-	       (eq (indirect-function mode)
-		   (indirect-function major-mode)))
+	       (or (eq (indirect-function mode)
+		       (indirect-function major-mode))
+		   (and set-auto-mode--last
+		        (eq mode (car set-auto-mode--last))
+		        (eq major-mode (cdr set-auto-mode--last)))))
     (when mode
-      (funcall mode)
+      (funcall (alist-get mode major-mode-remap-alist mode))
+      (unless (eq mode major-mode)
+        (setq set-auto-mode--last (cons mode major-mode)))
       mode)))
 
 (defvar file-auto-mode-skip "^\\(#!\\|'\\\\\"\\)"
@@ -3511,7 +3528,8 @@ set-auto-mode-1
                             ;; interpreter invocation.  The same holds
                             ;; for '\" in man pages (preprocessor
                             ;; magic for the `man' program).
-                            (and (looking-at file-auto-mode-skip) 2)) t)
+                            (and (looking-at file-auto-mode-skip) 2))
+                     t)
      (progn
        (skip-chars-forward " \t")
        (setq beg (point))
diff --git a/lisp/progmodes/cperl-mode.el b/lisp/progmodes/cperl-mode.el
index fa428642fa4..20a73e238e9 100644
--- a/lisp/progmodes/cperl-mode.el
+++ b/lisp/progmodes/cperl-mode.el
@@ -632,7 +632,7 @@ cperl-tips
 If your Emacs does not default to `cperl-mode' on Perl files, and you
 want it to: put the following into your .emacs file:
 
-  (defalias \\='perl-mode \\='cperl-mode)
+  (add-to-list \\='major-mode-remap-alist \\='(perl-mode . cperl-mode))
 
 Get perl5-info from
   $CPAN/doc/manual/info/perl5-old/perl5-info.tar.gz

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* bug#58075: Mapping file types to major modes
  2022-09-25 15:38 bug#58075: Mapping file types to major modes Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2022-09-26 11:26 ` Lars Ingebrigtsen
  2022-09-26 12:13   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-09-28  8:15 ` Ikumi Keita
  1 sibling, 1 reply; 10+ messages in thread
From: Lars Ingebrigtsen @ 2022-09-26 11:26 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Ikumi Keita, 58075, Juri Linkov

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> 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)

Isn't the usual solution to just make `auto-mode-alist' map .pl to
cperl-mode instead of perl-mode?

Hm, well, that's

     ("\\.\\([pP]\\([Llm]\\|erl\\|od\\)\\|al\\)\\'" . perl-mode)

so it's a bit of a mouthful.

> We could have
>
>     (add-to-list 'major-mode-remap-alist '(perl-mode . cperl-mode))

This area is already pretty complicated, so I worry that this is going
to make things even more confusing.  But perhaps it's worth it?






^ permalink raw reply	[flat|nested] 10+ messages in thread

* bug#58075: Mapping file types to major modes
  2022-09-26 11:26 ` Lars Ingebrigtsen
@ 2022-09-26 12:13   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-09-27 12:54     ` Lars Ingebrigtsen
  0 siblings, 1 reply; 10+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-09-26 12:13 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Ikumi Keita, 58075, Juri Linkov

Lars Ingebrigtsen [2022-09-26 13:26:48] wrote:
> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> 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)
>
> Isn't the usual solution to just make `auto-mode-alist' map .pl to
> cperl-mode instead of perl-mode?
>
> Hm, well, that's
>
>      ("\\.\\([pP]\\([Llm]\\|erl\\|od\\)\\|al\\)\\'" . perl-mode)
>
> so it's a bit of a mouthful.

And that doesn't account for the files where the mode is found by
`interpreter-mode-list`, by `mode:` cookies, etc...


        Stefan






^ permalink raw reply	[flat|nested] 10+ messages in thread

* bug#58075: Mapping file types to major modes
  2022-09-26 12:13   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2022-09-27 12:54     ` Lars Ingebrigtsen
  2022-10-03 15:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-10-03 15:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 2 replies; 10+ messages in thread
From: Lars Ingebrigtsen @ 2022-09-27 12:54 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Ikumi Keita, 58075, Juri Linkov

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> And that doesn't account for the files where the mode is found by
> `interpreter-mode-list`, by `mode:` cookies, etc...

Yup.  So I think your proposed patch makes a lot of sense, so feel free
to push.






^ permalink raw reply	[flat|nested] 10+ messages in thread

* bug#58075: Mapping file types to major modes
  2022-09-25 15:38 bug#58075: Mapping file types to major modes Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-09-26 11:26 ` Lars Ingebrigtsen
@ 2022-09-28  8:15 ` Ikumi Keita
  2022-09-28 17:29   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 1 reply; 10+ messages in thread
From: Ikumi Keita @ 2022-09-28  8:15 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 58075, Juri Linkov

Hi Stefan,

>>>>> Stefan Monnier <monnier@iro.umontreal.ca> writes:
> So instead I suggest consider the current settings as being our
> implementation of (A), and add (B) as some "mode remapping" layer to
> decide which major mode to actually for a given "type/mode".
> So instead of

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

> We could have

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

In your proposed patch, `major-mode-remap-alist' is a user option
defined by `defcustom'.

Is it supposed to be legitimate that a site administrator sets up the
default mapping for the site as
>     (add-to-list 'major-mode-remap-alist '(perl-mode . cperl-mode))
in site-start.el?

Currently, a site admin can place AUCTeX activation code
     (load "auctex.el" nil t t)
in site-start.el to prepare AUCTeX to be the site-wide default TeX mode.
In auctex.el, (require 'tex-site) loads tex-site.el, which adds the
:override advice for `latex-mode' etc. An individual user in that site
who prefer built-in modes can get rid of AUCTeX by
     (unload-feature 'tex-site)
in the user init file. That's the way that AUCTeX satisfy the preference
of both site admins and individual users.

I briefly skimmed over info documents of Emacs and Elisp, but could not
find a description whether a site admin is allowed or not to alter the
value of user customize variable in site-start.el (not default.el, of
course).

If it isn't allowed, I'd request to turn defcustom into defconst (or a
similar facility for site admin to set up the site default mapping
between (A) and (B)) so that AUCTeX can continue to satisfy both site
admins and users without :override advice hack.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine





^ permalink raw reply	[flat|nested] 10+ messages in thread

* bug#58075: Mapping file types to major modes
  2022-09-28  8:15 ` Ikumi Keita
@ 2022-09-28 17:29   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-09-29  4:58     ` Ikumi Keita
  0 siblings, 1 reply; 10+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-09-28 17:29 UTC (permalink / raw)
  To: Ikumi Keita; +Cc: 58075, Juri Linkov

Ikumi Keita [2022-09-28 17:15:43] wrote:
> Hi Stefan,
>>>>>> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> So instead I suggest consider the current settings as being our
>> implementation of (A), and add (B) as some "mode remapping" layer to
>> decide which major mode to actually for a given "type/mode".
>> So instead of
>
>>     (defalias 'perl-mode #'cperl-mode)
>
>> We could have
>
>>     (add-to-list 'major-mode-remap-alist '(perl-mode . cperl-mode))
>
> In your proposed patch, `major-mode-remap-alist' is a user option
> defined by `defcustom'.
>
> Is it supposed to be legitimate that a site administrator sets up the
> default mapping for the site as
>>     (add-to-list 'major-mode-remap-alist '(perl-mode . cperl-mode))
> in site-start.el?

That's a somewhat general problem we have, indeed: if the
`site-start.el` file uses `add-to-list` this way, then Custom will think
the variable is "changed outside of Custom" and it will complain about
it if the user then tries to configure the var via Custom.

Using `defvar` instead of `defcustom` wouldn't really help tho: the user
still would not be able to configure it via Custom.

To work "properly", `site-start.el` would need to change the *default*
definition of the var (which Custom stores in the `standard-value`
property), and then ask Custom to re-evaluate the var's value.

Emacs *should* provide some convenient way to do that, but Someone™
needs to code it up.

> I briefly skimmed over info documents of Emacs and Elisp, but could not
> find a description whether a site admin is allowed or not to alter the
> value of user customize variable in site-start.el (not default.el, of
> course).

It's allowed and very common in practice (e.g. to
set `send-mail-function` or `smtpmail-smtp-server`).
But doing it will tend to cause the problems mentioned above.


        Stefan






^ permalink raw reply	[flat|nested] 10+ messages in thread

* bug#58075: Mapping file types to major modes
  2022-09-28 17:29   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2022-09-29  4:58     ` Ikumi Keita
  2022-09-29 12:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 10+ messages in thread
From: Ikumi Keita @ 2022-09-29  4:58 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 58075, Juri Linkov

>>>>> Stefan Monnier <monnier@iro.umontreal.ca> writes:
> To work "properly", `site-start.el` would need to change the *default*
> definition of the var (which Custom stores in the `standard-value`
> property), and then ask Custom to re-evaluate the var's value.

> Emacs *should* provide some convenient way to do that, but Someone™
> needs to code it up.

>> I briefly skimmed over info documents of Emacs and Elisp, but could not
>> find a description whether a site admin is allowed or not to alter the
>> value of user customize variable in site-start.el (not default.el, of
>> course).

> It's allowed and very common in practice (e.g. to
> set `send-mail-function` or `smtpmail-smtp-server`).
> But doing it will tend to cause the problems mentioned above.

Thank you. Then AUCTeX would do just add-to-list at initialize when
`major-mode-remap-alist' is available.

> Using `defvar` instead of `defcustom` wouldn't really help tho: the user
> still would not be able to configure it via Custom.

I personally don't feel that defcustom is much suitable for this kind of
configuration variable, i.e. an alist which may be given site-wide
default values by site admins. Suppose that a user who prefers built-in
TeX modes customized `major-mode-remap-alist' via Custom to invalidate
site-wide default TeX modes and the site admin adds an entry (perl-mode .
cperl-mode) later. Then the user doesn't see the new site-wide default
perl mode.
I have an impression that it's always suitable to use `add-to-list' and
`delete' (or `assq'+`delq') to manipulate such alists.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine





^ permalink raw reply	[flat|nested] 10+ messages in thread

* bug#58075: Mapping file types to major modes
  2022-09-29  4:58     ` Ikumi Keita
@ 2022-09-29 12:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 10+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-09-29 12:18 UTC (permalink / raw)
  To: Ikumi Keita; +Cc: 58075, Juri Linkov

> Suppose that a user who prefers built-in TeX modes customized
> `major-mode-remap-alist' via Custom to invalidate site-wide default
> TeX modes and the site admin adds an entry (perl-mode .  cperl-mode)
> later. Then the user doesn't see the new site-wide default perl mode.

Indeed, it's a long-standing problem that Customize doesn't let the user
say "I want to add/remove <ELEM>" rather than "I want to set this list to
<VAL>".  Actually, strictly speaking it does, but noone bothered to make
that work well.


        Stefan






^ permalink raw reply	[flat|nested] 10+ messages in thread

* bug#58075: Mapping file types to major modes
  2022-09-27 12:54     ` Lars Ingebrigtsen
@ 2022-10-03 15:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-10-03 15:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 0 replies; 10+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-10-03 15:18 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Ikumi Keita, 58075, Juri Linkov

Lars Ingebrigtsen [2022-09-27 14:54:59] wrote:
> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> And that doesn't account for the files where the mode is found by
>> `interpreter-mode-list`, by `mode:` cookies, etc...
> Yup.  So I think your proposed patch makes a lot of sense, so feel free
> to push.

Thanks, installed,


        Stefan






^ permalink raw reply	[flat|nested] 10+ messages in thread

* bug#58075: Mapping file types to major modes
  2022-09-27 12:54     ` Lars Ingebrigtsen
  2022-10-03 15:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2022-10-03 15:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 0 replies; 10+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-10-03 15:18 UTC (permalink / raw)
  To: 58075-done

Closing






^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-10-03 15:18 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-25 15:38 bug#58075: Mapping file types to major modes Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-26 11:26 ` Lars Ingebrigtsen
2022-09-26 12:13   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-27 12:54     ` Lars Ingebrigtsen
2022-10-03 15:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-03 15:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-28  8:15 ` Ikumi Keita
2022-09-28 17:29   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-29  4:58     ` Ikumi Keita
2022-09-29 12:18       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

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).