emacs-devel
[Top][All Lists]
Advanced

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

Re: file-relative-name and remote files


From: Lars Hansen
Subject: Re: file-relative-name and remote files
Date: Sun, 23 Feb 2003 21:42:38 +0100
User-agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.2.1) Gecko/20021130



How about something like:

  (defun f-r-n (filename &optional directory)
    (let* ((dir (if directory (expand-file-name directory) default-directory))
           (file (expand-file-name (file-name-directory filename)))
           (hf (find-file-name-handler file 'file-relative-name))
           (hd (find-file-name-handler dir 'file-relative-name)))
      (cond
       ((not (eq hf hd))
        ;; `filename' and `directory' are on different drives:
        ;; there is hence no relative name from `directory' to `filename'.
        (expand-file-name filename))
       ((null hf)
        ;; Both are plain local: use the builtin code.
        (file-relative-name filename directory))
       ((let ((re (car (rassq hf file-name-handler-alist))))
          (equal (and (string-match re file) (substring file 0 (match-end 0)))
                 (and (string-match re dir) (substring dir 0 (match-end 0)))))
;; Both are non-local, use the same handler and same drive name. (file-relative-name filename directory))
       (t
        ;; Both are non-local and on different drives.
        (expand-file-name filename)))))

Note how I check the handler for (file-name-directory filename) rather
than for `filename' so as to avoid uselessly catching jka-compr-style
handlers.


        Stefan
I like the idea. I builds on the assumptions:

1. File names don't have a file-handler invoking syntax that is special in the directory part and the base part at the same time. 2. Files not residing in the local directory tree have a file-handler invoking syntax that is special in the directory part of the file name.

I guess these assumptions are ok, but if we decide on having them, they should be documented.

A minor issue: Your code returns an expanded file name in the cases where there is no local name, whereas the existing code in file-relative-name returns filename without expansion. We should do the same (I prefer expansion) in all cases.






reply via email to

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