Merge from emacs--devo--0
[bpt/emacs.git] / lisp / net / tramp-ftp.el
1 ;;; tramp-ftp.el --- Tramp convenience functions for Ange-FTP
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006,
4 ;; 2007 Free Software Foundation, Inc.
5
6 ;; Author: Michael Albinus <michael.albinus@gmx.de>
7 ;; Keywords: comm, processes
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, see
23 ;; <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Convenience functions for calling Ange-FTP from Tramp.
28 ;; Most of them are displaced from tramp.el.
29
30 ;;; Code:
31
32 (require 'tramp)
33 (autoload 'tramp-set-connection-property "tramp-cache")
34
35 (eval-when-compile (require 'custom))
36
37 ;; Disable Ange-FTP from file-name-handler-alist.
38 ;; To handle EFS, the following functions need to be dealt with:
39 ;;
40 ;; * dired-before-readin-hook contains efs-dired-before-readin
41 ;; * file-name-handler-alist contains efs-file-handler-function
42 ;; and efs-root-handler-function and efs-sifn-handler-function
43 ;; * find-file-hooks contains efs-set-buffer-mode
44 ;;
45 ;; But it won't happen for EFS since the XEmacs maintainers
46 ;; don't want to use a unified filename syntax.
47 (defun tramp-disable-ange-ftp ()
48 "Turn Ange-FTP off.
49 This is useful for unified remoting. See
50 `tramp-file-name-structure-unified' and
51 `tramp-file-name-structure-separate' for details. Requests suitable
52 for Ange-FTP will be forwarded to Ange-FTP. Also see the variables
53 `tramp-ftp-method', `tramp-default-method', and
54 `tramp-default-method-alist'.
55
56 This function is not needed in Emacsen which include Tramp, but is
57 present for backward compatibility."
58 (let ((a1 (rassq 'ange-ftp-hook-function file-name-handler-alist))
59 (a2 (rassq 'ange-ftp-completion-hook-function file-name-handler-alist)))
60 (setq file-name-handler-alist
61 (delete a1 (delete a2 file-name-handler-alist)))))
62
63 (eval-after-load "ange-ftp"
64 '(when (functionp 'tramp-disable-ange-ftp)
65 (tramp-disable-ange-ftp)))
66
67 ;;;###autoload
68 (defun tramp-ftp-enable-ange-ftp ()
69 ;; The following code is commented out in Ange-FTP.
70
71 ;;; This regexp takes care of real ange-ftp file names (with a slash
72 ;;; and colon).
73 ;;; Don't allow the host name to end in a period--some systems use /.:
74 (or (assoc "^/[^/:]*[^/:.]:" file-name-handler-alist)
75 (setq file-name-handler-alist
76 (cons '("^/[^/:]*[^/:.]:" . ange-ftp-hook-function)
77 file-name-handler-alist)))
78
79 ;;; This regexp recognizes absolute filenames with only one component,
80 ;;; for the sake of hostname completion.
81 (or (assoc "^/[^/:]*\\'" file-name-handler-alist)
82 (setq file-name-handler-alist
83 (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function)
84 file-name-handler-alist)))
85
86 ;;; This regexp recognizes absolute filenames with only one component
87 ;;; on Windows, for the sake of hostname completion.
88 (and (memq system-type '(ms-dos windows-nt))
89 (or (assoc "^[a-zA-Z]:/[^/:]*\\'" file-name-handler-alist)
90 (setq file-name-handler-alist
91 (cons '("^[a-zA-Z]:/[^/:]*\\'" .
92 ange-ftp-completion-hook-function)
93 file-name-handler-alist)))))
94
95 (add-hook 'tramp-ftp-unload-hook 'tramp-ftp-enable-ange-ftp)
96
97 ;; Define FTP method ...
98 (defcustom tramp-ftp-method "ftp"
99 "*When this method name is used, forward all calls to Ange-FTP."
100 :group 'tramp
101 :type 'string)
102
103 ;; ... and add it to the method list.
104 (add-to-list 'tramp-methods (cons tramp-ftp-method nil))
105
106 ;; Add some defaults for `tramp-default-method-alist'
107 (add-to-list 'tramp-default-method-alist
108 (list "\\`ftp\\." "" tramp-ftp-method))
109 (add-to-list 'tramp-default-method-alist
110 (list "" "\\`\\(anonymous\\|ftp\\)\\'" tramp-ftp-method))
111
112 ;; Add completion function for FTP method.
113 (tramp-set-completion-function
114 tramp-ftp-method
115 '((tramp-parse-netrc "~/.netrc")))
116
117 ;; If there is URL syntax, `substitute-in-file-name' needs special
118 ;; handling.
119 (put 'substitute-in-file-name 'ange-ftp 'tramp-handle-substitute-in-file-name)
120
121 (defun tramp-ftp-file-name-handler (operation &rest args)
122 "Invoke the Ange-FTP handler for OPERATION.
123 First arg specifies the OPERATION, second arg is a list of arguments to
124 pass to the OPERATION."
125 (save-match-data
126 (or (boundp 'ange-ftp-name-format)
127 (require 'ange-ftp))
128 (let ((ange-ftp-name-format
129 (list (nth 0 tramp-file-name-structure)
130 (nth 3 tramp-file-name-structure)
131 (nth 2 tramp-file-name-structure)
132 (nth 4 tramp-file-name-structure)))
133 ;; ange-ftp uses `ange-ftp-ftp-name-arg' and `ange-ftp-ftp-name-res'
134 ;; for optimization in `ange-ftp-ftp-name'. If Tramp wasn't active,
135 ;; there could be incorrect values from previous calls in case the
136 ;; "ftp" method is used in the Tramp file name. So we unset
137 ;; those values.
138 (ange-ftp-ftp-name-arg "")
139 (ange-ftp-ftp-name-res nil))
140 (cond
141 ;; If argument is a symlink, `file-directory-p' and
142 ;; `file-exists-p' call the traversed file recursively. So we
143 ;; cannot disable the file-name-handler this case. We set the
144 ;; connection property "started" in order to put the remote
145 ;; location into the cache, which is helpful for further
146 ;; completion. We don't use `with-parsed-tramp-file-name',
147 ;; because this returns another user but the one declared in
148 ;; "~/.netrc".
149 ((memq operation '(file-directory-p file-exists-p))
150 (if (apply 'ange-ftp-hook-function operation args)
151 (let ((v (tramp-dissect-file-name (car args) t)))
152 (aset v 0 tramp-ftp-method)
153 (tramp-set-connection-property v "started" t))
154 nil))
155
156 ;; If the second argument of `copy-file' or `rename-file' is a
157 ;; remote file name but via FTP, ange-ftp doesn't check this.
158 ;; We must copy it locally first, because there is no place in
159 ;; ange-ftp for correct handling.
160 ((and (memq operation '(copy-file rename-file))
161 (file-remote-p (cadr args))
162 (not (tramp-ftp-file-name-p (cadr args))))
163 (let* ((filename (car args))
164 (newname (cadr args))
165 (tmpfile (tramp-compat-make-temp-file filename))
166 (args (cddr args)))
167 ;; We must set `ok-if-already-exists' to t in the first
168 ;; step, because the temp file has been created already.
169 (if (eq operation 'copy-file)
170 (apply operation filename tmpfile t (cdr args))
171 (apply operation filename tmpfile t))
172 (unwind-protect
173 (rename-file tmpfile newname (car args))
174 ;; Cleanup.
175 (ignore-errors (delete-file tmpfile)))))
176
177 ;; Normally, the handlers must be discarded.
178 (t (let* ((inhibit-file-name-handlers
179 (list 'tramp-file-name-handler
180 'tramp-completion-file-name-handler
181 (and (eq inhibit-file-name-operation operation)
182 inhibit-file-name-handlers)))
183 (inhibit-file-name-operation operation))
184 (apply 'ange-ftp-hook-function operation args)))))))
185
186 (defun tramp-ftp-file-name-p (filename)
187 "Check if it's a filename that should be forwarded to Ange-FTP."
188 (let ((v (tramp-dissect-file-name filename)))
189 (string= (tramp-file-name-method v) tramp-ftp-method)))
190
191 (add-to-list 'tramp-foreign-file-name-handler-alist
192 (cons 'tramp-ftp-file-name-p 'tramp-ftp-file-name-handler))
193
194 (provide 'tramp-ftp)
195
196 ;;; TODO:
197
198 ;; * In case of "/ftp:host:file" this works only for functions which
199 ;; are defined in `tramp-file-name-handler-alist'. Call has to be
200 ;; pretended in `tramp-file-name-handler' otherwise.
201 ;; Furthermore, there are no backup files on FTP hosts.
202 ;; Worth further investigations.
203
204 ;;; arch-tag: 759fb338-5c63-4b99-bd36-b4d59db91cff
205 ;;; tramp-ftp.el ends here