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