From 2e71d2c709f003cd33597d106e8a483500ec99c9 Mon Sep 17 00:00:00 2001 From: Michael Albinus Date: Wed, 13 May 2026 18:39:04 +0200 Subject: [PATCH] Propagate EMACSCLIENT_TRAMP to remote hosts with Tramp * doc/misc/tramp.texi (Remote processes): Explain `tramp-propagate-emacsclient-tramp'. * lisp/net/tramp.el (tramp-remote-process-environment): Adapt docstring. (tramp-propagate-emacsclient-tramp): New defcustom. (tramp-handle-make-process): * lisp/net/tramp-sh.el (tramp-sh-handle-make-process) (tramp-sh-handle-process-file): Use it. * test/lisp/net/tramp-tests.el (tramp-test33-environment-variables): Adapt test. --- doc/misc/tramp.texi | 8 ++++++++ lisp/net/tramp-sh.el | 9 +++++++++ lisp/net/tramp.el | 16 ++++++++++++++++ test/lisp/net/tramp-tests.el | 15 +++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi index 8428109fde7..ae65cf2a620 100644 --- a/doc/misc/tramp.texi +++ b/doc/misc/tramp.texi @@ -4227,6 +4227,14 @@ called is local or remote, since @value{tramp} would add just the @env{HGPLAIN} setting and local processes would take whole value of @code{process-environment} along with the new value of @env{HGPLAIN}. +@vindex tramp-propagate-emacsclient-tramp +@vindex EMACSCLIENT_TRAMP@r{, environment variable} +If you set the user option @code{tramp-propagate-emacsclient-tramp} to +a non-@code{nil} value, the environment variable +@env{EMACSCLIENT_TRAMP} will be set to a value which allows to call +@command{emacsclient} from a process running on the remote +host. @xref{emacsclient Options, , , emacs}. + For integrating other Emacs packages so @value{tramp} can execute remotely, please file a bug report. @xref{Bug Reports}. diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el index 92002b854b3..7b90ae9c11b 100644 --- a/lisp/net/tramp-sh.el +++ b/lisp/net/tramp-sh.el @@ -3091,6 +3091,11 @@ will be used." (if (string-search "=" elt) (setq env (append env `(,elt))) (setq uenv (cons elt uenv)))))) + (env (if tramp-propagate-emacsclient-tramp + (setenv-internal + env "EMACSCLIENT_TRAMP" + (tramp-make-tramp-file-name v 'noloc) 'keep) + env)) (env (setenv-internal env "INSIDE_EMACS" (tramp-inside-emacs) 'keep)) ;; Environment is too large. Keep it here. @@ -3340,6 +3345,10 @@ will be used." (if (string-search "=" elt) (setq env (append env `(,elt))) (setq uenv (cons elt uenv))))) + (when tramp-propagate-emacsclient-tramp + (setq env (setenv-internal + env "EMACSCLIENT_TRAMP" + (tramp-make-tramp-file-name v 'noloc) 'keep))) (setq env (setenv-internal env "INSIDE_EMACS" (tramp-inside-emacs) 'keep)) (when env (setq command diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el index 6219d097f4f..ffcf8f4929c 100644 --- a/lisp/net/tramp.el +++ b/lisp/net/tramp.el @@ -1528,12 +1528,21 @@ The PATH environment variable should be set via `tramp-remote-path'. The TERM environment variable should be set via `tramp-terminal-type'. +The EMACSCLIENT_TRAMP environment variable will be set accordingly, if +`tramp-propagate-emacsclient-tramp' is non-nil. + The INSIDE_EMACS environment variable will automatically be set based on the Tramp and Emacs versions, and should not be set here." :version "26.1" :type '(repeat string) :link '(info-link :tag "Tramp manual" "(tramp) Remote processes")) +(defcustom tramp-propagate-emacsclient-tramp nil + "Whether to propagate the EMACSCLIENT_TRAMP environment variable." + :version "31.1" + :type 'boolean + :link '(info-link :tag "Tramp manual" "(tramp) Remote processes")) + ;;; Internal Variables: ;;;###tramp-autoload @@ -5509,6 +5518,13 @@ processes." (env (if sh-file-name-handler-p (setenv-internal env "TERM" tramp-terminal-type 'keep) env)) + ;; Add EMACSCLIENT_TRAMP. + (env (if (and tramp-propagate-emacsclient-tramp + sh-file-name-handler-p) + (setenv-internal + env "EMACSCLIENT_TRAMP" + (tramp-make-tramp-file-name v 'noloc) 'keep) + env)) ;; Add INSIDE_EMACS. (env (setenv-internal env "INSIDE_EMACS" (tramp-inside-emacs) 'keep)) (env (mapcar #'tramp-shell-quote-argument (delq nil env))) diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el index c76d6d54c3d..d6eb782df3d 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el @@ -6555,6 +6555,21 @@ INPUT, if non-nil, is a string sent to the process." (funcall this-shell-command-to-string "echo \"${INSIDE_EMACS:-bla}\"")))) + ;; Check EMACSCLIENT_TRAMP. + (setenv "EMACSCLIENT_TRAMP") + (let ((tramp-propagate-emacsclient-tramp t)) + (should + (string-equal + (format "%s\n" (tramp-make-tramp-file-name tramp-test-vec 'noloc)) + (funcall + this-shell-command-to-string "echo \"${EMACSCLIENT_TRAMP:-bla}\"")))) + (let (tramp-propagate-emacsclient-tramp) + (should + (string-equal + "bla\n" + (funcall + this-shell-command-to-string "echo \"${EMACSCLIENT_TRAMP:-bla}\"")))) + ;; Set a value. (let ((process-environment (cons (concat envvar "=foo") process-environment)))