Fan Yang <[email protected]> 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.
