(tramp-perl-directory-files-and-attributes): Add error handling.
[bpt/emacs.git] / lisp / net / tramp-ftp.el
CommitLineData
d2a2c17f 1;;; tramp-ftp.el --- Tramp convenience functions for Ange-FTP -*- coding: iso-8859-1; -*-
4007ba5b 2
ebd06e28 3;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4007ba5b 4
d2a2c17f 5;; Author: Michael Albinus <michael.albinus@gmx.de>
4007ba5b
KG
6;; Keywords: comm, processes
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
4007ba5b
KG
24
25;;; Commentary:
26
5ec2cc41
KG
27;; Convenience functions for calling Ange-FTP from Tramp.
28;; Most of them are displaced from tramp.el.
4007ba5b
KG
29
30;;; Code:
31
32(require 'tramp)
33
34(eval-when-compile
35 (require 'cl)
36 (require 'custom)
37 ;; Emacs 19.34 compatibility hack -- is this needed?
38 (or (>= emacs-major-version 20)
39 (load "cl-seq")))
40
41;; Disable Ange-FTP from file-name-handler-alist.
42;; To handle EFS, the following functions need to be dealt with:
43;;
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
48;;
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.
53This is useful for unified remoting. See
54`tramp-file-name-structure-unified' and
55`tramp-file-name-structure-separate' for details. Requests suitable
56for 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'.
59
60This function is not needed in Emacsen which include Tramp, but is
61present 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)))))
66(tramp-disable-ange-ftp)
8daea7fc 67(eval-after-load "ange-ftp" '(tramp-disable-ange-ftp))
4007ba5b
KG
68
69;; Define FTP method ...
70(defcustom tramp-ftp-method "ftp"
71 "*When this method name is used, forward all calls to Ange-FTP."
72 :group 'tramp
73 :type 'string)
74
75;; ... and add it to the method list.
76(add-to-list 'tramp-methods (cons tramp-ftp-method nil))
77
78;; Add some defaults for `tramp-default-method-alist'
79(add-to-list 'tramp-default-method-alist
8daea7fc 80 (list "\\`ftp\\." "" tramp-ftp-method))
4007ba5b 81(add-to-list 'tramp-default-method-alist
8daea7fc 82 (list "" "\\`\\(anonymous\\|ftp\\)\\'" tramp-ftp-method))
4007ba5b
KG
83
84;; Add completion function for FTP method.
85(unless (memq system-type '(windows-nt))
86 (tramp-set-completion-function
87 tramp-ftp-method
88 '((tramp-parse-netrc "~/.netrc"))))
89
90(defun tramp-ftp-file-name-handler (operation &rest args)
91 "Invoke the Ange-FTP handler for OPERATION.
92First arg specifies the OPERATION, second arg is a list of arguments to
93pass to the OPERATION."
94 (save-match-data
95 (or (boundp 'ange-ftp-name-format)
8daea7fc 96 (require 'ange-ftp))
83bbd71b
KG
97 (let ((ange-ftp-name-format
98 (list (nth 0 tramp-file-name-structure)
99 (nth 3 tramp-file-name-structure)
100 (nth 2 tramp-file-name-structure)
5ec2cc41
KG
101 (nth 4 tramp-file-name-structure)))
102 ;; ange-ftp uses `ange-ftp-ftp-name-arg' and `ange-ftp-ftp-name-res'
103 ;; for optimization in `ange-ftp-ftp-name'. If Tramp wasn't active,
104 ;; there could be incorrect values from previous calls in case the
105 ;; "ftp" method is used in the Tramp file name. So we unset
106 ;; those values.
107 (ange-ftp-ftp-name-arg "")
108 (ange-ftp-ftp-name-res nil))
83bbd71b 109 (cond
5ec2cc41 110 ;; If argument is a symlink, `file-directory-p' and `file-exists-p'
83bbd71b 111 ;; call the traversed file recursively. So we cannot disable the
b25a52cc 112 ;; file-name-handler this case.
83bbd71b
KG
113 ((memq operation '(file-directory-p file-exists-p))
114 (apply 'ange-ftp-hook-function operation args))
115 ;; Normally, the handlers must be discarded
116 (t (let* ((inhibit-file-name-handlers
117 (list 'tramp-file-name-handler
118 'tramp-completion-file-name-handler
119 (and (eq inhibit-file-name-operation operation)
120 inhibit-file-name-handlers)))
121 (inhibit-file-name-operation operation))
122 (apply 'ange-ftp-hook-function operation args)))))))
4007ba5b
KG
123
124(defun tramp-ftp-file-name-p (filename)
125 "Check if it's a filename that should be forwarded to Ange-FTP."
126 (let ((v (tramp-dissect-file-name filename)))
127 (string=
128 (tramp-find-method
129 (tramp-file-name-multi-method v)
130 (tramp-file-name-method v)
131 (tramp-file-name-user v)
132 (tramp-file-name-host v))
133 tramp-ftp-method)))
134
135(add-to-list 'tramp-foreign-file-name-handler-alist
136 (cons 'tramp-ftp-file-name-p 'tramp-ftp-file-name-handler))
137
138(provide 'tramp-ftp)
139
140;;; TODO:
141
142;; * In case of "/ftp:host:file" this works only for functions which
143;; are defined in `tramp-file-name-handler-alist'. Call has to be
83bbd71b
KG
144;; pretended in `tramp-file-name-handler' otherwise.
145;; Furthermore, there are no backup files on FTP hosts.
4007ba5b 146;; Worth further investigations.
c951aecb
KG
147;; * Map /multi:ssh:out@gate:ftp:kai@real.host:/path/to.file
148;; on Ange-FTP gateways.
4007ba5b 149
ab5796a9 150;;; arch-tag: 759fb338-5c63-4b99-bd36-b4d59db91cff
4007ba5b 151;;; tramp-ftp.el ends here