Subject: bug#56406: [PATCH] Send region to the subshell specified by the current file interpreter

From: Davide Masserut <[email-masked]>
Date: Tue, 05 Jul 2022 20:59:01 +0200

Tags: patch

This patch makes sh-execute-region to send the region to the subshell
specified by the current file interpreter.

Without it Emacs would run the region using the default user shell which
is probably not what you would expect if you are editing scripts made
for other shells.

Beware that I have not tested it on Windows or TRAMP.

BTW it is nice to edit code older than me 😀.

In GNU Emacs 29.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version 3.24.34, cairo version 1.17.6)
 of 2022-07-02 built on T480s
Repository revision: b79cfaaf14a7aa7e8bb56fc4063bd007e0edc69a
Repository branch: master
System Description: Arch Linux

Configured using:
 'configure --sysconfdir=/etc --prefix=/usr --libexecdir=/usr/lib
 --localstatedir=/var --with-cairo --with-harfbuzz --with-libsystemd
 --with-modules --with-pgtk --with-native-compilation
 'CFLAGS=-march=native -O2 -pipe -fno-plt -fexceptions
 -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security
 -fstack-clash-protection -fcf-protection -g
 -ffile-prefix-map=/home/davide/src/emacs-mssdvd-git/src=/usr/src/debug'
 LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now'

>From 7a79d1e56593dc8fde56f7f950725f5e4f6c96a7 Mon Sep 17 00:00:00 2001
From: Davide Masserut <[email-masked]>
Date: Tue, 5 Jul 2022 20:28:43 +0200
Subject: [PATCH] Send region to the subshell specified by the current file
 interpreter

* sh-script.el (sh-execute-region): Send region to the subshell
specified by the current file interpreter.
---
 lisp/progmodes/sh-script.el | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el
index 75758fd39a..c3e93c397a 100644
--- a/lisp/progmodes/sh-script.el
+++ b/lisp/progmodes/sh-script.el
@@ -2410,6 +2410,8 @@ sh-execute-region
 The working directory is that of the buffer, and only environment variables
 are already set which is why you can mark a header within the script.
 
+The executed subshell is `sh-shell-file'.
+
 With a positive prefix ARG, instead of sending region, define header from
 beginning of buffer to point.  With a negative prefix ARG, instead of sending
 region, clear header."
@@ -2417,17 +2419,18 @@ sh-execute-region
   (if flag
       (setq sh-header-marker (if (> (prefix-numeric-value flag) 0)
 				 (point-marker)))
-    (if sh-header-marker
-	(save-excursion
-	  (let (buffer-undo-list)
-	    (goto-char sh-header-marker)
-	    (append-to-buffer (current-buffer) start end)
-	    (shell-command-on-region (point-min)
-				     (setq end (+ sh-header-marker
-						  (- end start)))
-				     sh-shell-file)
-	    (delete-region sh-header-marker end)))
-      (shell-command-on-region start end (concat sh-shell-file " -")))))
+    (let ((shell-file-name sh-shell-file))
+      (if sh-header-marker
+	  (save-excursion
+	    (let (buffer-undo-list)
+	      (goto-char sh-header-marker)
+	      (append-to-buffer (current-buffer) start end)
+	      (shell-command-on-region (point-min)
+				       (setq end (+ sh-header-marker
+						    (- end start)))
+				       sh-shell-file)
+	      (delete-region sh-header-marker end)))
+        (shell-command-on-region start end (concat sh-shell-file " -"))))))

 (defun sh-remember-variable (var)
-- 
2.37.0