unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#13815: Wrong mode for files with awkward shebang lines
@ 2013-02-25 21:56 Andrew Pennebaker
  2013-02-26  2:59 ` Stefan Monnier
  2013-02-26  3:02 ` Glenn Morris
  0 siblings, 2 replies; 9+ messages in thread
From: Andrew Pennebaker @ 2013-02-25 21:56 UTC (permalink / raw)
  To: 13815

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

Some of my Scala code opens as Shell script major mode in Emacs, a problem
caused by several factors:

In order to enable dot-slashing for Scala scripts, programmers sometimes
start their code as follows:

#!/bin/sh
exec scala "$0" "$@"
!#

object Hello {
def main(args: Array[String]) {
println("Hello World!")
}
}

This is an awkward shebang line; due to the nature of shebangs, it must be
split across three lines of code, starting with #!/bin/sh.

This creates problems for Emacs, as Emacs uses `file` to detect which major
mode should be used, and `file` returns 'text/x-shellscript' when a file
begins #!/bin/sh, regardless of the file extension or programming language
content the file actually contains.

This could be solved a variety of ways, but I think the simplest is to
prioritize file extension over mimetype when a file extension is available.
We can't trust mimetypes for code that contains these awkward shebangs.

-- 
Cheers,

Andrew Pennebaker
www.yellosoft.us

[-- Attachment #2: Type: text/html, Size: 1569 bytes --]

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

* bug#13815: Wrong mode for files with awkward shebang lines
  2013-02-25 21:56 bug#13815: Wrong mode for files with awkward shebang lines Andrew Pennebaker
@ 2013-02-26  2:59 ` Stefan Monnier
  2013-02-26  8:12   ` Glenn Morris
  2013-02-26  3:02 ` Glenn Morris
  1 sibling, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2013-02-26  2:59 UTC (permalink / raw)
  To: Andrew Pennebaker; +Cc: 13815

> This could be solved a variety of ways, but I think the simplest is to
> prioritize file extension over mimetype when a file extension is available.

IIRC no strict priority will do, because for some files, the content is
more important than the filename.

> We can't trust mimetypes for code that contains these awkward shebangs.

[ FWIW Emacs doesn't actually use "mimtypes" but its own set of
  file-contents-based rules.  Not that it matters to this discussion,
  really.  ]

I think the right way is to check both file content and file name and if
the type they specify do not agree, fall back on a disambiguation
function, which should include various ad-hoc rules, and may ultimately
prompt the user if none of the ad-hoc rules resolved the ambiguity.

But in your specific case, you might get away with tweaking
inhibit-local-variables-regexps.


        Stefan





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

* bug#13815: Wrong mode for files with awkward shebang lines
  2013-02-25 21:56 bug#13815: Wrong mode for files with awkward shebang lines Andrew Pennebaker
  2013-02-26  2:59 ` Stefan Monnier
@ 2013-02-26  3:02 ` Glenn Morris
  2021-08-23  1:13   ` Lars Ingebrigtsen
  1 sibling, 1 reply; 9+ messages in thread
From: Glenn Morris @ 2013-02-26  3:02 UTC (permalink / raw)
  To: Andrew Pennebaker; +Cc: 13815

Andrew Pennebaker wrote:

> #!/bin/sh
> exec scala "$0" "$@"
[...]
> This creates problems for Emacs, as Emacs uses `file` to detect which major
> mode should be used,

Emacs does not use `file', it uses interpreter-mode-alist (amongst other
things).

> This could be solved a variety of ways, but I think the simplest is to
> prioritize file extension over mimetype when a file extension is available.

It used to work that way prior to Emacs 22.1. Then it was changed for
reasons that were never properly explained IMO.

http://debbugs.gnu.org/3354

All you can do is add a mode: cookie.

Or use /usr/bin/env, IIUC
https://github.com/scala/scala/pull/9





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

* bug#13815: Wrong mode for files with awkward shebang lines
  2013-02-26  2:59 ` Stefan Monnier
@ 2013-02-26  8:12   ` Glenn Morris
  0 siblings, 0 replies; 9+ messages in thread
From: Glenn Morris @ 2013-02-26  8:12 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 13815

Stefan Monnier wrote:

> IIRC no strict priority will do, because for some files, the content is
> more important than the filename.

I've never seen a real-world example where the #! line says one thing,
the filename extension says another, and the #! line is correct.
Does anyone have such an example?

(Going back to #3354, I now have 414 *.conf files in /etc, and not one
has a #! line.)





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

* bug#13815: Wrong mode for files with awkward shebang lines
  2013-02-26  3:02 ` Glenn Morris
@ 2021-08-23  1:13   ` Lars Ingebrigtsen
  2021-08-23 15:06     ` Glenn Morris
  0 siblings, 1 reply; 9+ messages in thread
From: Lars Ingebrigtsen @ 2021-08-23  1:13 UTC (permalink / raw)
  To: Glenn Morris; +Cc: 13815

Glenn Morris <rgm@gnu.org> writes:

> It used to work that way prior to Emacs 22.1. Then it was changed for
> reasons that were never properly explained IMO.
>
> http://debbugs.gnu.org/3354
>
> All you can do is add a mode: cookie.
>
> Or use /usr/bin/env, IIUC
> https://github.com/scala/scala/pull/9

So I think the conclusion here is that this is working as designed, and
I'm closing this bug report.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





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

* bug#13815: Wrong mode for files with awkward shebang lines
  2021-08-23  1:13   ` Lars Ingebrigtsen
@ 2021-08-23 15:06     ` Glenn Morris
  2021-08-23 15:10       ` Lars Ingebrigtsen
  0 siblings, 1 reply; 9+ messages in thread
From: Glenn Morris @ 2021-08-23 15:06 UTC (permalink / raw)
  To: 13815


FTR, this issue would be fixed if auto-mode-alist took priority over
interpreter-mode-alist. That's how it was before Emacs 22.1.
I've never seen an explanation for why it was changed (ref bug#3354).






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

* bug#13815: Wrong mode for files with awkward shebang lines
  2021-08-23 15:06     ` Glenn Morris
@ 2021-08-23 15:10       ` Lars Ingebrigtsen
  2021-08-30 15:11         ` Glenn Morris
  0 siblings, 1 reply; 9+ messages in thread
From: Lars Ingebrigtsen @ 2021-08-23 15:10 UTC (permalink / raw)
  To: Glenn Morris; +Cc: 13815

Glenn Morris <rgm@gnu.org> writes:

> FTR, this issue would be fixed if auto-mode-alist took priority over
> interpreter-mode-alist. That's how it was before Emacs 22.1.
> I've never seen an explanation for why it was changed (ref bug#3354).

I thought it was because Debian changed the names of all their conf
files (many of which are shell scripts) to .conf?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





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

* bug#13815: Wrong mode for files with awkward shebang lines
  2021-08-23 15:10       ` Lars Ingebrigtsen
@ 2021-08-30 15:11         ` Glenn Morris
  2021-08-31  1:54           ` Lars Ingebrigtsen
  0 siblings, 1 reply; 9+ messages in thread
From: Glenn Morris @ 2021-08-30 15:11 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 13815

Lars Ingebrigtsen wrote:

> I thought it was because Debian changed the names of all their conf
> files (many of which are shell scripts) to .conf?

I'll repeat my earlier comments in the hope that this time someone will
read them.

I have 468 .conf files in /etc on my Debian system.
A single one (it used to be zero years ago) has a #! line.
It is nftables.conf, which contains "#!/usr/sbin/nft -f".
Emacs has no mode for "nft".

Therefore this provides zero motivation for interpreter-mode-alist to
take precedence over auto-mode-alist.





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

* bug#13815: Wrong mode for files with awkward shebang lines
  2021-08-30 15:11         ` Glenn Morris
@ 2021-08-31  1:54           ` Lars Ingebrigtsen
  0 siblings, 0 replies; 9+ messages in thread
From: Lars Ingebrigtsen @ 2021-08-31  1:54 UTC (permalink / raw)
  To: Glenn Morris; +Cc: 13815

Glenn Morris <rgm@gnu.org> writes:

> I'll repeat my earlier comments in the hope that this time someone will
> read them.

Sorry; I misremembered.  Well -- then that's not a good reason to change
this (i.e., making #! take precedence over .suffix).

Does anybody remember what the rationale was for this change?  It's
somewhat puzzling behaviour. 

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





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

end of thread, other threads:[~2021-08-31  1:54 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-25 21:56 bug#13815: Wrong mode for files with awkward shebang lines Andrew Pennebaker
2013-02-26  2:59 ` Stefan Monnier
2013-02-26  8:12   ` Glenn Morris
2013-02-26  3:02 ` Glenn Morris
2021-08-23  1:13   ` Lars Ingebrigtsen
2021-08-23 15:06     ` Glenn Morris
2021-08-23 15:10       ` Lars Ingebrigtsen
2021-08-30 15:11         ` Glenn Morris
2021-08-31  1:54           ` Lars Ingebrigtsen

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