Expand absolute filenames with Tramp host info

347 views Asked by At

Ultimately I want the call the elisp (find-file "/abs/olute/file.name") when (file-remote-p default-directory) is non-nil and have the remote host applied to the absolute file name in the find-file call.

The actual use-case: I have a shell-mode session on a remote host where default-directory is of the form "/meth:user@host:/abs/path/". I have added a command in shell mode that opens a file in a new buffer. If I send a relative file name the remote file is opened properly; if I send an absolute file path (starts with "/" or "~") it is evaluated relative to my local context rather than the remote.

I can wrap the function so that the remote info is tacked in front of the absolute file path before it's opened but I was wondering if there is a TRAMP function or variable that I can use to force the use of the remote host for absolute file paths. Note, again, that relative paths are handled appropriately by applying the remote host and path from default-directory correctly.

2

There are 2 answers

0
Michael Albinus On BEST ANSWER

There is not such a Tramp function. (find-file "/abs/olute/file.name") knows, that the file name is a local one, and it doesn't call Tramp.

So you must implement this on your own, indeed.

0
noelbk On

Here's what I use:

(defun expand-file-name-remote (file)
  "A tramp-friendly version of expand-file-name.  Expand file
relative to the remote part of default-directory."
  (let ((file (expand-file-name file))
        (dir-remote (file-remote-p default-directory)))
    (if (file-remote-p file)
        ;; if file is already remote, return it
        file
      ;; otherwise prepend the remote part (if any) of default-directory
      (concat dir-remote file)
      )))