Merge from emacs--rel--22
[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
34 (eval-when-compile (require 'custom))
35
36 ;; Disable Ange-FTP from file-name-handler-alist.
37 ;; To handle EFS, the following functions need to be dealt with:
38 ;;
39 ;; * dired-before-readin-hook contains efs-dired-before-readin
40 ;; * file-name-handler-alist contains efs-file-handler-function
41 ;; and efs-root-handler-function and efs-sifn-handler-function
42 ;; * find-file-hooks contains efs-set-buffer-mode
43 ;;
44 ;; But it won't happen for EFS since the XEmacs maintainers
45 ;; don't want to use a unified filename syntax.
46 (defun tramp-disable-ange-ftp ()
47 "Turn Ange-FTP off.
48 This is useful for unified remoting. See
49 `tramp-file-name-structure-unified' and
50 `tramp-file-name-structure-separate' for details. Requests suitable
51 for Ange-FTP will be forwarded to Ange-FTP. Also see the variables
52 `tramp-ftp-method', `tramp-default-method', and
53 `tramp-default-method-alist'.
54
55 This function is not needed in Emacsen which include Tramp, but is
56 present for backward compatibility."
57 (let ((a1 (rassq 'ange-ftp-hook-function file-name-handler-alist))
58 (a2 (rassq 'ange-ftp-completion-hook-function file-name-handler-alist)))
59 (setq file-name-handler-alist
60 (delete a1 (delete a2 file-name-handler-alist)))))
61
62 (eval-after-load "ange-ftp"
63 '(when (functionp 'tramp-disable-ange-ftp)
64 (tramp-disable-ange-ftp)))
65
66 ;;;###autoload
67 (defun tramp-ftp-enable-ange-ftp ()
68 ;; The following code is commented out in Ange-FTP.
69
70 ;;; This regexp takes care of real ange-ftp file names (with a slash
71 ;;; and colon).
72 ;;; Don't allow the host name to end in a period--some systems use /.:
73 (or (assoc "^/[^/:]*[^/:.]:" file-name-handler-alist)
74 (setq file-name-handler-alist
75 (cons '("^/[^/:]*[^/:.]:" . ange-ftp-hook-function)
76 file-name-handler-alist)))
77
78 ;;; This regexp recognizes absolute filenames with only one component,
79 ;;; for the sake of hostname completion.
80 (or (assoc "^/[^/:]*\\'" file-name-handler-alist)
81 (setq file-name-handler-alist
82 (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function)
83 file-name-handler-alist)))
84
85 ;;; This regexp recognizes absolute filenames with only one component
86 ;;; on Windows, for the sake of hostname completion.
87 (and (memq system-type '(ms-dos windows-nt))
88 (or (assoc "^[a-zA-Z]:/[^/:]*\\'" file-name-handler-alist)
89 (setq file-name-handler-alist
90 (cons '("^[a-zA-Z]:/[^/:]*\\'" .
91 ange-ftp-completion-hook-function)
92 file-name-handler-alist)))))
93
94 (add-hook 'tramp-ftp-unload-hook 'tramp-ftp-enable-ange-ftp)
95
96 ;; Define FTP method ...
97 (defcustom tramp-ftp-method "ftp"
98 "*When this method name is used, forward all calls to Ange-FTP."
99 :group 'tramp
100 :type 'string)
101
102 ;; ... and add it to the method list.
103 (add-to-list 'tramp-methods (cons tramp-ftp-method nil))
104
105 ;; Add some defaults for `tramp-default-method-alist'
106 (add-to-list 'tramp-default-method-alist
107 (list "\\`ftp\\." "" tramp-ftp-method))
108 (add-to-list 'tramp-default-method-alist
109 (list "" "\\`\\(anonymous\\|ftp\\)\\'" tramp-ftp-method))
110
111 ;; Add completion function for FTP method.
112 (tramp-set-completion-function
113 tramp-ftp-method
114 '((tramp-parse-netrc "~/.netrc")))
115
116 ;; If there is URL syntax, `substitute-in-file-name' needs special
117 ;; handling.
118 (put 'substitute-in-file-name 'ange-ftp 'tramp-handle-substitute-in-file-name)
119
120 (defun tramp-ftp-file-name-handler (operation &rest args)
121 "Invoke the Ange-FTP handler for OPERATION.
122 First arg specifies the OPERATION, second arg is a list of arguments to
123 pass to the OPERATION."
124 (save-match-data
125 (or (boundp 'ange-ftp-name-format)
126 (require 'ange-ftp))
127 (let ((ange-ftp-name-format
128 (list (nth 0 tramp-file-name-structure)
129 (nth 3 tramp-file-name-structure)
130 (nth 2 tramp-file-name-structure)
131 (nth 4 tramp-file-name-structure)))
132 ;; ange-ftp uses `ange-ftp-ftp-name-arg' and `ange-ftp-ftp-name-res'
133 ;; for optimization in `ange-ftp-ftp-name'. If Tramp wasn't active,
134 ;; there could be incorrect values from previous calls in case the
135 ;; "ftp" method is used in the Tramp file name. So we unset
136 ;; those values.
137 (ange-ftp-ftp-name-arg "")
138 (ange-ftp-ftp-name-res nil))
139 (cond
140 ;; If argument is a symlink, `file-directory-p' and `file-exists-p'
141 ;; call the traversed file recursively. So we cannot disable the
142 ;; file-name-handler this case.
143 ((memq operation '(file-directory-p file-exists-p))
144 (apply 'ange-ftp-hook-function operation args))
145 ;; Normally, the handlers must be discarded
146 (t (let* ((inhibit-file-name-handlers
147 (list 'tramp-file-name-handler
148 'tramp-completion-file-name-handler
149 (and (eq inhibit-file-name-operation operation)
150 inhibit-file-name-handlers)))
151 (inhibit-file-name-operation operation))
152 (apply 'ange-ftp-hook-function operation args)))))))
153
154 (defun tramp-ftp-file-name-p (filename)
155 "Check if it's a filename that should be forwarded to Ange-FTP."
156 (let ((v (tramp-dissect-file-name filename)))
157 (string= (tramp-file-name-method v) tramp-ftp-method)))
158
159 (add-to-list 'tramp-foreign-file-name-handler-alist
160 (cons 'tramp-ftp-file-name-p 'tramp-ftp-file-name-handler))
161
162 (provide 'tramp-ftp)
163
164 ;;; TODO:
165
166 ;; * In case of "/ftp:host:file" this works only for functions which
167 ;; are defined in `tramp-file-name-handler-alist'. Call has to be
168 ;; pretended in `tramp-file-name-handler' otherwise.
169 ;; Furthermore, there are no backup files on FTP hosts.
170 ;; Worth further investigations.
171
172 ;;; arch-tag: 759fb338-5c63-4b99-bd36-b4d59db91cff
173 ;;; tramp-ftp.el ends here