diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el index c079455a444..615f9219448 100644 --- a/lisp/net/tramp-sh.el +++ b/lisp/net/tramp-sh.el @@ -64,6 +64,7 @@ tramp-copy-size-limit :group 'tramp :type '(choice (const nil) integer)) +;;;###tramp-autoload (defcustom tramp-histfile-override "~/.tramp_history" "When invoking a shell, override the HISTFILE with this value. When setting to a string, it redirects the shell history to that @@ -80,6 +81,8 @@ tramp-histfile-override (const :tag "Unset HISTFILE" t) (string :tag "Redirect to a file"))) +(put 'tramp-histfile-override 'permanent-local t) + ;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for ;; root users. It uses the `$' character for other users. In order ;; to guarantee a proper prompt, we use "#$ " for the prompt. diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el index 9385b023392..936fdd25e6c 100644 --- a/lisp/net/tramp.el +++ b/lisp/net/tramp.el @@ -4962,6 +4962,18 @@ tramp-handle-make-process (string-join (tramp-get-remote-path v) ":"))) (setenv-internal env "PATH" remote-path 'keep) env)) + ;; Add HISTFILE if indicated. + (env (if-let ((sh-file-name-handler-p)) + (cond + ((stringp tramp-histfile-override) + (setenv-internal env "HISTFILE" tramp-histfile-override 'keep)) + (tramp-histfile-override + (setq env (setenv-internal env "HISTFILE" "''" 'keep)) + (setq env (setenv-internal env "HISTSIZE" "0" 'keep)) + (setenv-internal env "HISTFILESIZE" "0" 'keep)) + (t env)) + env)) + ;; Add INSIDE_EMACS. (env (setenv-internal env "INSIDE_EMACS" (tramp-inside-emacs) 'keep)) (env (mapcar #'tramp-shell-quote-argument (delq nil env))) @@ -5248,7 +5260,8 @@ tramp-handle-shell-command (with-current-buffer output-buffer (setq mode-line-process '(":%s")) (unless (eq major-mode 'shell-mode) - (shell-mode)) + (with-connection-local-application-variables 'shell-command + (shell-mode))) (set-process-filter p #'comint-output-filter) (set-process-sentinel p #'shell-command-sentinel) (when error-file diff --git a/lisp/shell.el b/lisp/shell.el index e6b315ee5c0..60a805f33c9 100644 --- a/lisp/shell.el +++ b/lisp/shell.el @@ -609,6 +609,8 @@ sh-shell-file (declare-function w32-application-type "w32proc.c" (program) t) +(defvar tramp-histfile-override) + (define-derived-mode shell-mode comint-mode "Shell" "Major mode for interacting with an inferior shell. \\ @@ -726,9 +728,11 @@ shell-mode (hsize (getenv "HISTSIZE")) (hfile (getenv "HISTFILE"))) (when remote - ;; `shell-snarf-envar' does not work trustworthy. - (setq hsize (shell-command-to-string "echo -n $HISTSIZE") - hfile (shell-command-to-string "echo -n $HISTFILE"))) + (if (eq tramp-histfile-override t) + (setq remote "" hfile nil hsize nil) + ;; `shell-snarf-envar' does not work trustworthy. + (setq hsize (shell-command-to-string "echo -n $HISTSIZE") + hfile (shell-command-to-string "echo -n $HISTFILE")))) (and (string-equal hfile "") (setq hfile nil)) (and (stringp hsize) (integerp (setq hsize (string-to-number hsize))) @@ -738,10 +742,11 @@ shell-mode (concat remote (or hfile - (cond ((string-equal shell "bash") "~/.bash_history") - ((string-equal shell "ksh") "~/.sh_history") - ((string-equal shell "zsh") "~/.zsh_history") - (t "~/.history"))))) + (and (not (string-equal remote "")) + (cond ((string-equal shell "bash") "~/.bash_history") + ((string-equal shell "ksh") "~/.sh_history") + ((string-equal shell "zsh") "~/.zsh_history") + (t "~/.history")))))) (if (or (equal comint-input-ring-file-name "") (equal (file-truename comint-input-ring-file-name) (file-truename null-device)))