From: "Kévin Le Gouguec" <kevin.legouguec@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 66902@debbugs.gnu.org
Subject: bug#66902: 30.0.50; Recognize env -S/--split-string in shebangs
Date: Sat, 18 Nov 2023 18:44:06 +0100 [thread overview]
Message-ID: <87wmufardl.fsf@gmail.com> (raw)
In-Reply-To: <8734x3cpz2.fsf@gmail.com> ("Kévin Le Gouguec"'s message of "Sat, 18 Nov 2023 11:31:29 +0100")
[-- Attachment #1: Type: text/plain, Size: 810 bytes --]
Kévin Le Gouguec <kevin.legouguec@gmail.com> writes:
> Eli Zaretskii <eliz@gnu.org> writes:
>
>> I'd prefer not to have rx required in files.el, so could you please
>> rewrite those parts of your patch and resubmit? Also, please add a
>> NEWS entry about the change.
>
> ACK; will get to it in the coming days.
s/days/hours/
I left a 'concat' in, because (a) it lets us interleave comments (b) the
byte-compiler seems to smartly condense it all to one big string literal
anyway. (Though if files.el is preloaded, everything happens at
build-time and the .elc does not matter much, IIUC?)
Let me know if we would prefer a plain raw string literal.
Added a NEWS entry (under § 'Changes in Emacs 30.1 / Miscellaneous',
assuming 'master'); added a bug reference; squashed it all.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Recognize-shebang-lines-that-pass-S-split-string-to-.patch --]
[-- Type: text/x-patch, Size: 4499 bytes --]
From 95068836b5970c1aebb088e987741ad316007b79 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?K=C3=A9vin=20Le=20Gouguec?= <kevin.legouguec@gmail.com>
Date: Sun, 12 Nov 2023 10:55:24 +0100
Subject: [PATCH] Recognize shebang lines that pass -S/--split-string to env
(bug#66902)
* etc/NEWS: announce the change.
* lisp/files.el (auto-mode-interpreter-regexp): Add optional -S switch
to the ignored group capturing the env invocation.
Allow multiple spaces between #!, interpreter and first argument:
empirically, Linux's execve accepts that.
* test/lisp/files-tests.el (files-tests--check-shebang): New helper to
generate a temporary file with a given interpreter line, and assert
that the mode picked by 'set-auto-mode' is derived from an expected
mode. Write the 'should' form so that failure reports include useful
context; for example:
(ert-test-failed
((should
(equal (list shebang actual-mode) (list shebang expected-mode)))
:form
(equal ("#!/usr/bin/env -S make -f" fundamental-mode)
("#!/usr/bin/env -S make -f" makefile-mode))
:value nil :explanation
(list-elt 1 (different-atoms fundamental-mode makefile-mode))))
(files-tests-auto-mode-interpreter): New test; exercise some aspects
of interpreter-mode-alist.
---
etc/NEWS | 6 ++++++
lisp/files.el | 12 ++++++++++--
test/lisp/files-tests.el | 25 +++++++++++++++++++++++++
3 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/etc/NEWS b/etc/NEWS
index 12ae8058cb1..b9ee3747040 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -233,6 +233,12 @@ to enter the file you want to modify.
It can be used to customize the look of the appointment notification
displayed on the mode line when 'appt-display-mode-line' is non-nil.
+---
+*** Emacs now recognizes shebang lines that pass -S/--split-string to env.
+When visiting a script that invokes 'env -S INTERPRETER ARGS...' in
+its shebang line, Emacs will now skip over 'env -S' and deduce the
+major mode based on the interpreter.
+
** Emacs Server and Client
---
diff --git a/lisp/files.el b/lisp/files.el
index d729bdf8c25..1cdcec23b11 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -3245,8 +3245,16 @@ inhibit-local-variables-p
temp))
(defvar auto-mode-interpreter-regexp
- (purecopy "#![ \t]?\\([^ \t\n]*\
-/bin/env[ \t]\\)?\\([^ \t\n]+\\)")
+ (purecopy
+ (concat
+ "#![ \t]*"
+ ;; Optional group 1: env(1) invocation.
+ "\\("
+ "[^ \t\n]*/bin/env[ \t]*"
+ "\\(?:-S[ \t]*\\|--split-string\\(?:=\\|[ \t]*\\)\\)?"
+ "\\)?"
+ ;; Group 2: interpreter.
+ "\\([^ \t\n]+\\)"))
"Regexp matching interpreters, for file mode determination.
This regular expression is matched against the first line of a file
to determine the file's mode in `set-auto-mode'. If it matches, the file
diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el
index 3492bd701b2..3e499fff468 100644
--- a/test/lisp/files-tests.el
+++ b/test/lisp/files-tests.el
@@ -1656,6 +1656,31 @@ files-tests-file-name-base
(should (equal (file-name-base "foo") "foo"))
(should (equal (file-name-base "foo/bar") "bar")))
+(defun files-tests--check-shebang (shebang expected-mode)
+ "Assert that mode for SHEBANG derives from EXPECTED-MODE."
+ (let ((actual-mode
+ (ert-with-temp-file script-file
+ :text shebang
+ (find-file script-file)
+ (if (derived-mode-p expected-mode)
+ expected-mode
+ major-mode))))
+ ;; Tuck all the information we need in the `should' form: input
+ ;; shebang, expected mode vs actual.
+ (should
+ (equal (list shebang actual-mode)
+ (list shebang expected-mode)))))
+
+(ert-deftest files-tests-auto-mode-interpreter ()
+ "Test that `set-auto-mode' deduces correct modes from shebangs."
+ (files-tests--check-shebang "#!/bin/bash" 'sh-mode)
+ (files-tests--check-shebang "#!/usr/bin/env bash" 'sh-mode)
+ (files-tests--check-shebang "#!/usr/bin/env python" 'python-base-mode)
+ (files-tests--check-shebang "#!/usr/bin/env python3" 'python-base-mode)
+ (files-tests--check-shebang "#!/usr/bin/env -S awk -v FS=\"\\t\" -v OFS=\"\\t\" -f" 'awk-mode)
+ (files-tests--check-shebang "#!/usr/bin/env -S make -f" 'makefile-mode)
+ (files-tests--check-shebang "#!/usr/bin/make -f" 'makefile-mode))
+
(ert-deftest files-test-dir-locals-auto-mode-alist ()
"Test an `auto-mode-alist' entry in `.dir-locals.el'"
(find-file (ert-resource-file "whatever.quux"))
--
2.42.1
next prev parent reply other threads:[~2023-11-18 17:44 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-02 20:57 bug#66902: 30.0.50; Recognize env -S/--split-string in shebangs Kévin Le Gouguec
2023-11-12 17:53 ` Kévin Le Gouguec
2023-11-18 9:41 ` Eli Zaretskii
2023-11-18 10:31 ` Kévin Le Gouguec
2023-11-18 17:44 ` Kévin Le Gouguec [this message]
2023-11-19 9:09 ` Eli Zaretskii
2023-11-19 10:51 ` Kévin Le Gouguec
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=87wmufardl.fsf@gmail.com \
--to=kevin.legouguec@gmail.com \
--cc=66902@debbugs.gnu.org \
--cc=eliz@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.