Subject: Re: bug#31355: 26.1; [PATCH] Fix remote-host/tramp directory tracking for shells in `term' buffers

From: Phil Sainty <[email-masked]>
Date: Fri, 04 May 2018 02:47:40 +1200

On 2018-05-04 02:04, Michael Albinus wrote:
> Furthermore, I wouldn't hard-code "ssh". People might have other
> preferences, and on some systems it doesn't work. Think about Emacs
> running on MS WIndows, where "plink" is used instead of "ssh". Just 
> take
> "-" as method, this is a notation to apply user's default (Tramp) 
> method.

Revised patch attached to use the "-" pseudo method.

> I believe it is always save to use term-ansi-at-user. There's no need
> to check, whether it is equal to (user-real-login-name).

I've not changed this.  The code is evidentially deciding whether the
user/host pair being tracked in the shell in the term buffer is a match
for the user/host pair known locally to Emacs.  Unless both of those
components match, a tramp path is created.  I've not tested the case
of a different user on the local host, but it seems like reasonable
logic to me, at first look.

(if (and (string= term-ansi-at-host (system-name))
          (string= term-ansi-at-user (user-real-login-name)))
     ;; Use a local path
   ;; Use a tramp path

From 4c3620d3a420b3ed617e95d127ce5e5333ceda61 Mon Sep 17 00:00:00 2001
From: Phil Sainty <[email-masked]>
Date: Fri, 4 May 2018 01:29:42 +1200
Subject: [PATCH] Fix remote-host directory tracking for shells in `term'
 buffers

* lisp/term.el (term-handle-ansi-terminal-messages): Use an explicit
tramp method when constructing the tramp path for a non-local host,
as this is now mandatory.  "-" is a pseudo-method for the user's
`tramp-default-method'.

Minor related improvements to the commentary.
---
 lisp/term.el | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/lisp/term.el b/lisp/term.el
index 75c2779..2fbc796 100644
--- a/lisp/term.el
+++ b/lisp/term.el
@@ -233,31 +233,33 @@
 ;;
 ;;  Notice: for directory/host/user tracking you need to have something
 ;; like this in your shell startup script (this is for a POSIXish shell
-;; like Bash but should be quite easy to port to other shells)
+;; like Bash but should be quite easy to port to other shells).
+;;
+;; For troubleshooting in Bash, you can check the definition of the
+;; custom functions with the "type" command.  e.g. "type cd".  If you
+;; do not see the expected definition from the config below, then the
+;; directory tracking will not work.
 ;;
 ;;             ----------------------------------------
 ;;
-;;  # Set HOSTNAME if not already set.
+;;	# Set HOSTNAME if not already set.
 ;;	: ${HOSTNAME=$(uname -n)}
 ;;
-;;  # su does not change this but I'd like it to
-;;
+;;	# su does not change this but I'd like it to
 ;;	USER=$(whoami)
 ;;
-;;  # ...
+;;	# ...
 ;;
 ;;	case $TERM in
 ;;	    eterm*)
 ;;
 ;;		printf '%s\n' \
 ;;		 -------------------------------------------------------------- \
-;;		 "Hello $user" \
+;;		 "Hello $USER" \
 ;;		 "Today is $(date)" \
 ;;		 "We are on $HOSTNAME running $(uname) under Emacs term mode" \
 ;;		 --------------------------------------------------------------
 ;;
-;;		export EDITOR=emacsclient
-;;
 ;;		# The \033 stands for ESC.
 ;;		# There is a space between "AnSiT?" and $whatever.
 ;;
@@ -269,10 +271,11 @@
 ;;		printf '\033AnSiTh %s\n' "$HOSTNAME"
 ;;		printf '\033AnSiTu %s\n' "$USER"
 ;;
-;;		eval $(dircolors $HOME/.emacs_dircolors)
+;;		# Use custom dircolors in term buffers.
+;;		# eval $(dircolors $HOME/.emacs_dircolors)
 ;;	esac
 ;;
-;;  # ...
+;;	# ...
 ;;
 ;;
 
@@ -2753,8 +2756,8 @@ term-handle-ansi-terminal-messages
 					(string= term-ansi-at-user (user-real-login-name)))
 		   (expand-file-name term-ansi-at-dir)
 		 (if (string= term-ansi-at-user (user-real-login-name))
-		     (concat "/" term-ansi-at-host ":" term-ansi-at-dir)
-		   (concat "/" term-ansi-at-user "@" term-ansi-at-host ":"
+                     (concat "/-:" term-ansi-at-host ":" term-ansi-at-dir)
+                   (concat "/-:" term-ansi-at-user "@" term-ansi-at-host ":"
 			   term-ansi-at-dir)))))
 
 	;; I'm not sure this is necessary,
-- 
2.8.3