[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help: how to obtain a correct file path from a possible absolute or

From: Michael Albinus
Subject: Re: Help: how to obtain a correct file path from a possible absolute or relative path considering remote environment
Date: Fri, 03 Jan 2020 13:39:01 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Fan Yang <address@hidden> writes:

> Hi all,

Hi Fan,

> Recently I am trying to write a package but get stuck on handling remote
> file path correctly.  I wonder if it is OK to ask here.
> What I want seems simple: I want to `find-file' a file based on a path
> name obtained from the output of a command line tool.  However,
> 1. This command line tool may output relative or absolute path.
> 2. I want my package work in both local and remote environment.  That
>    is, the command line tool may be run on a remote machine based on the
>    user's current `default-directory' using `process-file'.  Thus I need
>    to somehow "transform" the output path into a path with correct TRAMP
>    prefix.
> Here is an example.  Suppose,
> - I am at buffer "/scp:fan@vm00:/home/fan/dir1/dir2/a.c".
> - The output of the command line tool can be
>   - a relative path, "b.c", or
>   - an absolute path, "/home/fan/dir1/dir2/b.c".
> Then I want to use `find-file' to open the "b.c" file.
> Attempt 1:
> ,----
> | (find-file (concat default-directory path))
> | ;; for a relative path, it produces
> | "/scp:fan@vm00:/home/fan/dir1/dir2/b.c", correct.
> | ;; for an absolute path, it produces
> | "/scp:fan@vm00:/home/fan/dir1/dir2//home/fan/dir1/dir2/virtio_pm_balloon.c",
> | wrong.
> `----
> Attempt 2:
> ,----
> | (find-file (concat (file-remote-p default-directory) path))
> | ;; for a relative path, it produces "/scp:fan@vm00:b.c, wrong.
> | ;; for an absolute path, it produces
> | "/scp:fan@vm00:/home/fan/dir1/dir2/b.c", correct.
> `----
> Attempt 3:
> ,----
> | (if (file-name-absolute-p path)
> |     (find-file (concat (file-remote-p default-directory) path))
> |   (find-file (concat default-directory path)))
> `----
> Attempt 3 seems correct, but here `file-name-absolute-p' uses the local
> rule to judge a remote path.  Is it still correct if the remote machine
> is Windows, the local machine is Linux, and the output path is like
> "E:/xxx/xxx"?  This is an absolute path for the remote machine, but
> `file-name-absolute-p' on the local machine will treat it incorrectly.
> How do I "transform" the path correctly, and elegantly?  Thanks in
> advance for any help you are able to provide.

Attempt 3 seems to be OK to me. I don't understand your problem with
Windows; I do not expect Tramp to run process-file on a remote Windows machine.

> Fan

Best regards, Michael.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]