]> code.delx.au - gnu-emacs/blobdiff - lisp/net/tramp-sh.el
Merge from origin/emacs-24
[gnu-emacs] / lisp / net / tramp-sh.el
index dc78acefba6d5155983f6154d3a50753b4feae1b..2e2e2ac369854b7684ae610ae65ea007e63b0766 100644 (file)
@@ -64,6 +64,22 @@ files conditionalize this setup based on the TERM environment variable."
   :group 'tramp
   :type 'string)
 
+;;;###tramp-autoload
+(defcustom tramp-histfile-override "/dev/null"
+  "When invoking a shell, override the HISTFILE with this value.
+By default, the HISTFILE is set to the \"/dev/null\" value, which
+is special on Unix systems and indicates the shell history should
+not be logged (this avoids clutter due to Tramp commands).
+
+If you set this variable to nil, however, the *override* is
+disabled, so the history will go to the default storage
+location, e.g. \"$HOME/.sh_history\"."
+  :group 'tramp
+  :version "25.1"
+  :type '(choice (const :tag "Do not override HISTFILE" nil)
+                 (const :tag "Empty the history (/dev/null)" "/dev/null")
+                 (string :tag "Redirect to a file")))
+
 ;;;###tramp-autoload
 (defconst tramp-color-escape-sequence-regexp "\e[[;0-9]+m"
   "Escape sequences produced by the \"ls\" command.")
@@ -2835,16 +2851,27 @@ the result will be a local, non-Tramp, file name."
                        (list (replace-match " \\\\\n" nil nil (cadr args))))
                       (setq i (+ i 250))))
                   (cdr args)))
+          ;; Use a human-friendly prompt, for example for `shell'.
+          (prompt (format "PS1=%s"
+                          (format "%s %s"
+                                  (file-remote-p default-directory)
+                                  tramp-initial-end-of-output)))
+          ;; We use as environment the difference to toplevel
+          ;; `process-environment'.
+          env
+          (env
+           (dolist
+               (elt
+                (cons prompt (nreverse (copy-sequence process-environment)))
+                env)
+             (or (member elt (default-toplevel-value 'process-environment))
+                 (setq env (cons elt env)))))
           (command
            (when (stringp program)
-             (format "cd %s && exec %s env PS1=%s %s"
+             (format "cd %s && exec %s env %s %s"
                      (tramp-shell-quote-argument localname)
                      (if heredoc (format "<<'%s'" tramp-end-of-heredoc) "")
-                     ;; Use a human-friendly prompt, for example for `shell'.
-                     (tramp-shell-quote-argument
-                      (format "%s %s"
-                              (file-remote-p default-directory)
-                              tramp-initial-end-of-output))
+                     (mapconcat 'tramp-shell-quote-argument env " ")
                      (if heredoc
                          (format "%s\n(\n%s\n) </dev/tty\n%s"
                                  program (car args) tramp-end-of-heredoc)
@@ -2931,10 +2958,20 @@ the result will be a local, non-Tramp, file name."
     (error "Implementation does not handle immediate return"))
 
   (with-parsed-tramp-file-name default-directory nil
-    (let (command input tmpinput stderr tmpstderr outbuf ret)
+    (let (command env input tmpinput stderr tmpstderr outbuf ret)
       ;; Compute command.
       (setq command (mapconcat 'tramp-shell-quote-argument
                               (cons program args) " "))
+      ;; We use as environment the difference to toplevel `process-environment'.
+      (setq env
+           (dolist (elt (nreverse (copy-sequence process-environment)) env)
+             (or (member elt (default-toplevel-value 'process-environment))
+                 (setq env (cons elt env)))))
+      (when env
+       (setq command
+             (format
+              "env %s %s"
+              (mapconcat 'tramp-shell-quote-argument env " ") command)))
       ;; Determine input.
       (if (null infile)
          (setq input "/dev/null")
@@ -3861,7 +3898,10 @@ file exists and nonzero exit status otherwise."
       ;; the prompt in /bin/bash, it must be discarded as well.
       (tramp-send-command
        vec (format
-           "exec env ENV='' HISTFILE=/dev/null PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
+           "exec env ENV=''%s PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
+            (if tramp-histfile-override
+                (concat " HISTFILE=" tramp-histfile-override)
+              "")
            (tramp-shell-quote-argument tramp-end-of-output)
            shell (or extra-args ""))
        t))
@@ -4583,7 +4623,8 @@ connection if a previous connection has died for some reason."
                (delete-process p))
              (setenv "TERM" tramp-terminal-type)
              (setenv "LC_ALL" "en_US.utf8")
-             (setenv "HISTFILE" "/dev/null")
+             (when tramp-histfile-override
+                (setenv "HISTFILE" tramp-histfile-override))
              (setenv "PROMPT_COMMAND")
              (setenv "PS1" tramp-initial-end-of-output)
              (let* ((target-alist (tramp-compute-multi-hops vec))