all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: file-relative-name and remote files
@ 2003-02-27 20:03 Lars Hansen
  2003-03-01  2:25 ` Richard Stallman
  2003-03-24 11:43 ` Kai Großjohann
  0 siblings, 2 replies; 53+ messages in thread
From: Lars Hansen @ 2003-02-27 20:03 UTC (permalink / raw)
  Cc: emacs-devel

I suggest the following implementation of file-relative-name.
It does not require a new file handler operation, it detects
remote files in the same way as file-remote-p do. Please see
the doc string for further explanation.

(defun file-relative-name (filename &optional directory separate-trees)
  "Convert FILENAME to be relative to DIRECTORY (default: 
`default-directory').
This function returns a relative file name which is equivalent to FILENAME
when used with that default directory as the default.
If SEPARATE-TREES is non-nil and FILENAME and DIRECTORY lie on different
machines or on different drives (DOS/Windows), it returns FILENAME on
expanded form."
  (save-match-data
    (setq
      directory
      (file-name-as-directory (expand-file-name (or directory 
default-directory))))
    (setq filename (expand-file-name filename))
    (let ((hf (find-file-name-handler filename 'file-local-copy))
          (hd (find-file-name-handler directory 'file-local-copy)))
      (when (and hf (not (get hf 'file-remote-p))) (setq hf nil))
      (when (and hd (not (get hd 'file-remote-p))) (setq hd nil))
      (if
        (and
          separate-trees
          ;; Conditions for separate trees
          (or
            ;; Test for different drives on DOS/Windows
            (and
              (memq system-type '(ms-dos cygwin windows-nt))
              (not (string-equal (substring filename  0 2) (substring 
directory 0 2))))
            ;; Test for different remote file handlers
            (not (eq hf hd))
            ;; Test for different remote file system identification
            (and
              hf
              (let ((re (car (rassq hf file-name-handler-alist))))
                (not
                  (equal
                    (and
                      (string-match re filename)
                      (substring filename 0 (match-end 0)))
                    (and
                      (string-match re directory)
                      (substring directory 0 (match-end 0)))))))))
        filename
        (unless (eq (aref filename 0) ?/) (setq filename (concat "/" 
filename)))
        (unless (eq (aref directory 0) ?/) (setq directory (concat "/" 
directory)))
        (let (
          (ancestor ".")
          (filename-dir (file-name-as-directory filename)))
          (while
            (and
              (not (string-match (concat "^" (regexp-quote directory)) 
filename-dir))
              (not (string-match (concat "^" (regexp-quote directory)) 
filename)))
            (setq
              directory (file-name-directory (substring directory 0 -1))
              ancestor (if (equal ancestor ".") ".." (concat "../" 
ancestor))))
          ;; Now ancestor is empty, or .., or ../.., etc.
          (if (string-match (concat "^" (regexp-quote directory)) filename)
            ;; We matched within FILENAME's directory part.
            ;; Add the rest of FILENAME onto ANCESTOR.
            (let ((rest (substring filename (match-end 0))))
              (if (and (equal ancestor ".") (not (equal rest "")))
                ;; But don't bother with ANCESTOR if it would give us `./'.
                rest
                (concat (file-name-as-directory ancestor) rest)))
            ;; We matched FILENAME's directory equivalent.
            ancestor))))))

^ permalink raw reply	[flat|nested] 53+ messages in thread
[parent not found: <E18xZCF-0008JB-04@monty-python.gnu.org>]
[parent not found: <E18xQiP-00067n-01@monty-python.gnu.org>]
[parent not found: <E18t79a-00069E-04@monty-python.gnu.org>]
[parent not found: <E18sn8p-0003dO-01@monty-python.gnu.org>]
[parent not found: <E18s4BL-0000Xi-00@monty-python.gnu.org>]
[parent not found: <E18rhhr-0005Oc-03@monty-python.gnu.org>]
[parent not found: <E18rQiV-0004PA-00@monty-python.gnu.org>]
[parent not found: <E18nLzM-0001k5-01@monty-python.gnu.org>]
* Re: file-relative-name and remote files
@ 2003-02-23 15:42 Lars Hansen
  2003-02-23 16:41 ` Kai Großjohann
  2003-02-23 18:30 ` Stefan Monnier
  0 siblings, 2 replies; 53+ messages in thread
From: Lars Hansen @ 2003-02-23 15:42 UTC (permalink / raw)


> IMHO, the following might work: file-relative-name finds a filename 
> handler for the filename and one for the directory. If they are 
> different, then the two must come from different handlers, so do like 
> the different-drives case. If they are equal, invoke the handler. This 
> requires a new file operation, file-relative-name. It seems to work 
> for Ange-FTP and Tramp. However... Imagine that there was a filename 
> handler which could look inside of tar files, so that the filename 
> /tmp/foo.tar/x/y would extract the file x/y from the tarball 
> /tmp/foo.tar. So should (file-relative-name "/tmp/foo.tar/x/y" "/tmp") 
> eval to "foo.tar/x/y" or not?


What about changing it to:
file-relative-name finds a filename handler for the filename and one for 
the directory. If they are non-nil and different, then the two must come 
from different handlers, so do like the different-drives case. If they 
are non-nil and equal, or one is non-nil and the other nil, invoke the 
handler. If both are nil, execute the build in code.

Then the handler can decide what to do when one of the files is an 
ordinary one, and in your example it could return "foo.tar/x/y".

^ permalink raw reply	[flat|nested] 53+ messages in thread
* file-relative-name and remote files
@ 2003-02-23 10:36 Lars Hansen
  2003-02-23 11:03 ` Kai Großjohann
  2003-02-28 18:33 ` Kai Großjohann
  0 siblings, 2 replies; 53+ messages in thread
From: Lars Hansen @ 2003-02-23 10:36 UTC (permalink / raw)


file-relative-name assumes that filename and directory is part of the 
same tree except when we are on a DOS/Windows system and the first two 
chars in the expanded file names (the drive) are not equal. This 
approach does not work with remote files. With remote files filename and 
directory may reside in different trees. Since the syntax of remote 
files just is a prefix to an ordinary file name, it should be a matter 
of comparing prefixes, just as in the DOS/Windows case. However, remote 
files are handled by the file name handler mechanism which does not work 
for file-relative-name, since file-relative-name is a lisp function.

What would be the best way to solve this problem?

^ permalink raw reply	[flat|nested] 53+ messages in thread

end of thread, other threads:[~2003-03-25 14:46 UTC | newest]

Thread overview: 53+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-02-27 20:03 file-relative-name and remote files Lars Hansen
2003-03-01  2:25 ` Richard Stallman
2003-03-24 11:43 ` Kai Großjohann
2003-03-25  0:52   ` Richard Stallman
     [not found] <E18xZCF-0008JB-04@monty-python.gnu.org>
2003-03-25 12:22 ` Lars Hansen
2003-03-25 14:46   ` Kai Großjohann
     [not found] <E18xQiP-00067n-01@monty-python.gnu.org>
2003-03-24 12:58 ` Lars Hansen
2003-03-24 18:24   ` Kai Großjohann
     [not found] <E18t79a-00069E-04@monty-python.gnu.org>
2003-03-14 20:40 ` Lars Hansen
     [not found] <E18sn8p-0003dO-01@monty-python.gnu.org>
2003-03-12  7:20 ` Lars Hansen
2003-03-12 11:53   ` Kai Großjohann
     [not found] <E18s4BL-0000Xi-00@monty-python.gnu.org>
2003-03-11 10:05 ` Lars Hansen
2003-03-11 10:34   ` Lars Hansen
2003-03-11 15:08   ` Kai Großjohann
     [not found] <E18rhhr-0005Oc-03@monty-python.gnu.org>
2003-03-08 20:05 ` Lars Hansen
2003-03-09 13:36   ` Kai Großjohann
     [not found] <E18rQiV-0004PA-00@monty-python.gnu.org>
2003-03-08  8:38 ` Lars Hansen
2003-03-08  8:46 ` Lars Hansen
2003-03-08 14:41   ` Kai Großjohann
     [not found] <E18nLzM-0001k5-01@monty-python.gnu.org>
2003-02-24 21:24 ` Lars Hansen
2003-02-24 21:36   ` Stefan Monnier
2003-02-24 21:52     ` Lars Hansen
2003-02-25 16:57   ` Richard Stallman
2003-02-26  9:41     ` Lars Hansen
2003-02-26 23:24       ` Richard Stallman
2003-02-27 14:22         ` Lars Hansen
2003-03-01  2:25           ` Richard Stallman
2003-03-03 10:22             ` Lars Hansen
2003-03-07 20:57             ` Kai Großjohann
2003-03-17  4:52               ` Richard Stallman
2003-03-21 14:20                 ` Kai Großjohann
2003-03-24  2:05                   ` Richard Stallman
2003-03-07 21:02           ` Kai Großjohann
2003-03-09 19:25             ` Richard Stallman
2003-02-27 17:02         ` Kai Großjohann
2003-02-27 17:05         ` Kai Großjohann
2003-02-27 17:37           ` Andreas Schwab
2003-02-28 14:53             ` Kai Großjohann
2003-02-28 15:43               ` Andreas Schwab
2003-02-28 16:27                 ` Miles Bader
2003-02-28 18:35                   ` Stefan Monnier
2003-02-28 21:32                     ` Andreas Schwab
2003-03-01 13:00                     ` Kai Großjohann
2003-03-02 15:06                       ` Richard Stallman
  -- strict thread matches above, loose matches on Subject: below --
2003-02-23 15:42 Lars Hansen
2003-02-23 16:41 ` Kai Großjohann
2003-02-23 18:30 ` Stefan Monnier
2003-02-23 20:42   ` Lars Hansen
2003-02-26  9:46     ` Richard Stallman
2003-02-23 10:36 Lars Hansen
2003-02-23 11:03 ` Kai Großjohann
2003-02-24 16:38   ` Richard Stallman
2003-02-28 18:33 ` Kai Großjohann

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.