* window.c (syms_of_window): Add missing defsubr for window-use-time.
[bpt/emacs.git] / lisp / net / tramp.el
CommitLineData
b1a2b924 1;;; tramp.el --- Transparent Remote Access, Multiple Protocol
fb7933a3 2
5fd6d89f 3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
114f9c96 4;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
fb7933a3 5
cdd44874 6;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
d2a2c17f 7;; Michael Albinus <michael.albinus@gmx.de>
fb7933a3 8;; Keywords: comm, processes
0f34aa77 9;; Package: tramp
fb7933a3
KG
10
11;; This file is part of GNU Emacs.
12
874a927a 13;; GNU Emacs is free software: you can redistribute it and/or modify
fb7933a3 14;; it under the terms of the GNU General Public License as published by
874a927a
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
fb7933a3
KG
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
874a927a 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
fb7933a3
KG
25
26;;; Commentary:
27
28;; This package provides remote file editing, similar to ange-ftp.
29;; The difference is that ange-ftp uses FTP to transfer files between
30;; the local and the remote host, whereas tramp.el uses a combination
31;; of rsh and rcp or other work-alike programs, such as ssh/scp.
32;;
b1a2b924 33;; For more detailed instructions, please see the info file.
fb7933a3
KG
34;;
35;; Notes:
36;; -----
bf247b6e 37;;
b533bc97 38;; This package only works for Emacs 22.1 and higher, and for XEmacs 21.4
00d6fd04 39;; and higher. For XEmacs 21, you need the package `fsf-compat' for
7f4d4a97 40;; the `with-timeout' macro.
fb7933a3 41;;
fb7933a3
KG
42;; Also see the todo list at the bottom of this file.
43;;
b1a2b924 44;; The current version of Tramp can be retrieved from the following URL:
340b8d4f 45;; http://ftp.gnu.org/gnu/tramp/
fb7933a3
KG
46;;
47;; There's a mailing list for this, as well. Its name is:
340b8d4f
MA
48;; tramp-devel@gnu.org
49;; You can use the Web to subscribe, under the following URL:
50;; http://lists.gnu.org/mailman/listinfo/tramp-devel
fb7933a3
KG
51;;
52;; For the adventurous, the current development sources are available
53;; via CVS. You can find instructions about this at the following URL:
c62c9d08 54;; http://savannah.gnu.org/projects/tramp/
fb7933a3
KG
55;; Click on "CVS" in the navigation bar near the top.
56;;
57;; Don't forget to put on your asbestos longjohns, first!
58
59;;; Code:
60
9e6ab520 61(require 'tramp-compat)
fb7933a3 62
fb7933a3
KG
63;;; User Customizable Internal Variables:
64
65(defgroup tramp nil
66 "Edit remote files with a combination of rsh and rcp or similar programs."
589d30dd 67 :group 'files
26f4b8ab 68 :group 'comm
bf247b6e 69 :version "22.1")
fb7933a3 70
2e271195
MA
71;; Maybe we need once a real Tramp mode, with key bindings etc.
72;;;###autoload
73(defcustom tramp-mode t
74 "*Whether Tramp is enabled.
75If it is set to nil, all remote file names are used literally."
76 :group 'tramp
77 :type 'boolean)
78
00d6fd04 79(defcustom tramp-verbose 3
263c02ef 80 "*Verbosity level for Tramp messages.
00d6fd04
MA
81Any level x includes messages for all levels 1 .. x-1. The levels are
82
83 0 silent (no tramp messages at all)
84 1 errors
85 2 warnings
86 3 connection to remote hosts (default level)
87 4 activities
88 5 internal
89 6 sent and received strings
90 7 file caching
91 8 connection properties
8fbcce2d 92 9 test commands
00d6fd04 9310 traces (huge)."
fb7933a3
KG
94 :group 'tramp
95 :type 'integer)
96
263c02ef 97;; Emacs case.
38c65fca
KG
98(eval-and-compile
99 (when (boundp 'backup-directory-alist)
100 (defcustom tramp-backup-directory-alist nil
101 "Alist of filename patterns and backup directory names.
102Each element looks like (REGEXP . DIRECTORY), with the same meaning like
103in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
104is a local file name, the backup directory is prepended with Tramp file
00d6fd04 105name prefix \(method, user, host\) of file.
38c65fca
KG
106
107\(setq tramp-backup-directory-alist backup-directory-alist\)
108
109gives the same backup policy for Tramp files on their hosts like the
110policy for local files."
111 :group 'tramp
112 :type '(repeat (cons (regexp :tag "Regexp matching filename")
113 (directory :tag "Backup directory name"))))))
114
115;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
116;; the package "backup-dir" might not be loaded yet.
117(eval-and-compile
118 (when (featurep 'xemacs)
119 (defcustom tramp-bkup-backup-directory-info nil
120 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
121It has the same meaning like `bkup-backup-directory-info' from package
122`backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
123file name, the backup directory is prepended with Tramp file name prefix
00d6fd04 124\(method, user, host\) of file.
38c65fca
KG
125
126\(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
127
128gives the same backup policy for Tramp files on their hosts like the
129policy for local files."
bf247b6e 130 :type '(repeat
38c65fca
KG
131 (list (regexp :tag "File regexp")
132 (string :tag "Backup Dir")
133 (set :inline t
134 (const ok-create)
135 (const full-path)
136 (const prepend-name)
137 (const search-upward))))
138 :group 'tramp)))
139
fb7933a3
KG
140(defcustom tramp-auto-save-directory nil
141 "*Put auto-save files in this directory, if set.
142The idea is to use a local directory so that auto-saving is faster."
143 :group 'tramp
00d6fd04 144 :type '(choice (const nil) string))
fb7933a3 145
16674e4f
KG
146(defcustom tramp-encoding-shell
147 (if (memq system-type '(windows-nt))
148 (getenv "COMSPEC")
149 "/bin/sh")
150 "*Use this program for encoding and decoding commands on the local host.
151This shell is used to execute the encoding and decoding command on the
152local host, so if you want to use `~' in those commands, you should
153choose a shell here which groks tilde expansion. `/bin/sh' normally
154does not understand tilde expansion.
155
156For encoding and deocding, commands like the following are executed:
157
158 /bin/sh -c COMMAND < INPUT > OUTPUT
159
160This variable can be used to change the \"/bin/sh\" part. See the
00d6fd04 161variable `tramp-encoding-command-switch' for the \"-c\" part.
fb7933a3
KG
162
163Note that this variable is not used for remote commands. There are
164mechanisms in tramp.el which automatically determine the right shell to
165use for the remote host."
166 :group 'tramp
167 :type '(file :must-match t))
168
16674e4f
KG
169(defcustom tramp-encoding-command-switch
170 (if (string-match "cmd\\.exe" tramp-encoding-shell)
171 "/c"
172 "-c")
173 "*Use this switch together with `tramp-encoding-shell' for local commands.
174See the variable `tramp-encoding-shell' for more information."
175 :group 'tramp
176 :type 'string)
177
0f34aa77 178;;;###tramp-autoload
03c1ad43 179(defvar tramp-methods nil
fb7933a3
KG
180 "*Alist of methods for remote files.
181This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
182Each NAME stands for a remote access method. Each PARAM is a
183pair of the form (KEY VALUE). The following KEYs are defined:
fb7933a3
KG
184 * `tramp-remote-sh'
185 This specifies the Bourne shell to use on the remote host. This
186 MUST be a Bourne-like shell. It is normally not necessary to set
a4aeb9a4 187 this to any value other than \"/bin/sh\": Tramp wants to use a shell
fb7933a3
KG
188 which groks tilde expansion, but it can search for it. Also note
189 that \"/bin/sh\" exists on all Unixen, this might not be true for
190 the value that you decide to use. You Have Been Warned.
b25a52cc
KG
191 * `tramp-login-program'
192 This specifies the name of the program to use for logging in to the
00d6fd04
MA
193 remote host. This may be the name of rsh or a workalike program,
194 or the name of telnet or a workalike, or the name of su or a workalike.
b25a52cc 195 * `tramp-login-args'
fb7933a3 196 This specifies the list of arguments to pass to the above
00d6fd04 197 mentioned program. Please note that this is a list of list of arguments,
fb7933a3 198 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
00d6fd04
MA
199 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
200 There are some patterns: \"%h\" in this list is replaced by the host
201 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
202 port number, and \"%%\" can be used to obtain a literal percent character.
203 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
204 expansion (i.e. no host or no user specified), this list is not used as
205 argument. By this, arguments like (\"-l\" \"%u\") are optional.
206 \"%t\" is replaced by the temporary file name produced with
207 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
208 parameter of a program, if exists.
a7723e05
MA
209 * `tramp-async-args'
210 When an asynchronous process is started, we know already that
211 the connection works. Therefore, we can pass additional
212 parameters to suppress diagnostic messages, in order not to
213 tamper the process output.
b25a52cc
KG
214 * `tramp-copy-program'
215 This specifies the name of the program to use for remotely copying
216 the file; this might be the absolute filename of rcp or the name of
217 a workalike program.
218 * `tramp-copy-args'
fb7933a3 219 This specifies the list of parameters to pass to the above mentioned
b25a52cc 220 program, the hints for `tramp-login-args' also apply here.
00d6fd04
MA
221 * `tramp-copy-keep-date'
222 This specifies whether the copying program when the preserves the
223 timestamp of the original file.
b88f2d0a
MA
224 * `tramp-copy-keep-tmpfile'
225 This specifies whether a temporary local file shall be kept
226 for optimization reasons (useful for \"rsync\" methods).
227 * `tramp-copy-recursive'
228 Whether the operation copies directories recursively.
00d6fd04
MA
229 * `tramp-default-port'
230 The default port of a method is needed in case of gateway connections.
231 Additionally, it is used as indication which method is prepared for
232 passing gateways.
233 * `tramp-gw-args'
234 As the attribute name says, additional arguments are specified here
235 when a method is applied via a gateway.
90f8dc03
KG
236 * `tramp-password-end-of-line'
237 This specifies the string to use for terminating the line after
238 submitting the password. If this method parameter is nil, then the
239 value of the normal variable `tramp-default-password-end-of-line'
240 is used. This parameter is necessary because the \"plink\" program
241 requires any two characters after sending the password. These do
242 not have to be newline or carriage return characters. Other login
243 programs are happy with just one character, the newline character.
244 We use \"xy\" as the value for methods using \"plink\".
b25a52cc
KG
245
246What does all this mean? Well, you should specify `tramp-login-program'
247for all methods; this program is used to log in to the remote site. Then,
248there are two ways to actually transfer the files between the local and the
249remote side. One way is using an additional rcp-like program. If you want
250to do this, set `tramp-copy-program' in the method.
fb7933a3
KG
251
252Another possibility for file transfer is inline transfer, i.e. the
b25a52cc 253file is passed through the same buffer used by `tramp-login-program'. In
fb7933a3 254this case, the file contents need to be protected since the
b25a52cc 255`tramp-login-program' might use escape codes or the connection might not
fb7933a3 256be eight-bit clean. Therefore, file contents are encoded for transit.
00d6fd04
MA
257See the variables `tramp-local-coding-commands' and
258`tramp-remote-coding-commands' for details.
fb7933a3 259
16674e4f 260So, to summarize: if the method is an out-of-band method, then you
b25a52cc 261must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
00d6fd04
MA
262inline method, then these two parameters should be nil. Methods which
263are fit for gateways must have `tramp-default-port' at least.
fb7933a3
KG
264
265Notes:
266
00d6fd04
MA
267When using `su' or `sudo' the phrase `open connection to a remote
268host' sounds strange, but it is used nevertheless, for consistency.
269No connection is opened to a remote host, but `su' or `sudo' is
270started on the local host. You should specify a remote host
271`localhost' or the name of the local host. Another host name is
272useful only in combination with `tramp-default-proxies-alist'.")
fb7933a3 273
a92375d9
MA
274(defun tramp-detect-ssh-controlmaster ()
275 "Call ssh to detect whether it supports the ControlMaster argument.
276This function may return nil when the argument is supported, but
277shouldn't return t when it isn't."
278 (ignore-errors
279 (with-temp-buffer
280 (call-process "ssh" nil t nil "-o" "ControlMaster")
281 (goto-char (point-min))
282 (search-forward-regexp "Missing ControlMaster argument" nil t))))
283
b25a52cc 284(defcustom tramp-default-method
83e20b5c
MA
285 ;; An external copy method seems to be preferred, because it is much
286 ;; more performant for large files, and it hasn't too serious delays
287 ;; for small files. But it must be ensured that there aren't
288 ;; permanent password queries. Either a password agent like
263c02ef
MA
289 ;; "ssh-agent" or "Pageant" shall run, or the optional
290 ;; password-cache.el or auth-sources.el packages shall be active for
a92375d9
MA
291 ;; password caching. "scpc" is chosen if we detect that the user is
292 ;; running OpenSSH 4.0 or newer.
00d6fd04
MA
293 (cond
294 ;; PuTTY is installed.
295 ((executable-find "pscp")
296 (if (or (fboundp 'password-read)
263c02ef 297 (fboundp 'auth-source-user-or-password)
00d6fd04 298 ;; Pageant is running.
70c11b0b 299 (tramp-compat-process-running-p "Pageant"))
00d6fd04
MA
300 "pscp"
301 "plink"))
302 ;; There is an ssh installation.
303 ((executable-find "scp")
a92375d9
MA
304 (cond
305 ((tramp-detect-ssh-controlmaster) "scpc")
306 ((or (fboundp 'password-read)
307 (fboundp 'auth-source-user-or-password)
308 ;; ssh-agent is running.
309 (getenv "SSH_AUTH_SOCK")
310 (getenv "SSH_AGENT_PID"))
311 "scp")
312 (t "ssh")))
00d6fd04
MA
313 ;; Fallback.
314 (t "ftp"))
fb7933a3 315 "*Default method to use for transferring files.
c62c9d08 316See `tramp-methods' for possibilities.
4007ba5b 317Also see `tramp-default-method-alist'."
c62c9d08
KG
318 :group 'tramp
319 :type 'string)
320
03c1ad43 321(defcustom tramp-default-method-alist nil
00d6fd04 322 "*Default method to use for specific host/user pairs.
c62c9d08
KG
323This is an alist of items (HOST USER METHOD). The first matching item
324specifies the method to use for a file name which does not specify a
325method. HOST and USER are regular expressions or nil, which is
326interpreted as a regular expression which always matches. If no entry
327matches, the variable `tramp-default-method' takes effect.
328
329If the file name does not specify the user, lookup is done using the
330empty string for the user name.
331
332See `tramp-methods' for a list of possibilities for METHOD."
333 :group 'tramp
e40fc745
MA
334 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
335 (choice :tag "User regexp" regexp sexp)
336 (choice :tag "Method name" string (const nil)))))
c62c9d08 337
03c1ad43 338(defcustom tramp-default-user nil
00d6fd04
MA
339 "*Default user to use for transferring files.
340It is nil by default; otherwise settings in configuration files like
341\"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
342
343This variable is regarded as obsolete, and will be removed soon."
344 :group 'tramp
345 :type '(choice (const nil) string))
346
03c1ad43 347(defcustom tramp-default-user-alist nil
00d6fd04
MA
348 "*Default user to use for specific method/host pairs.
349This is an alist of items (METHOD HOST USER). The first matching item
350specifies the user to use for a file name which does not specify a
351user. METHOD and USER are regular expressions or nil, which is
352interpreted as a regular expression which always matches. If no entry
353matches, the variable `tramp-default-user' takes effect.
354
355If the file name does not specify the method, lookup is done using the
356empty string for the method name."
357 :group 'tramp
e40fc745
MA
358 :type '(repeat (list (choice :tag "Method regexp" regexp sexp)
359 (choice :tag " Host regexp" regexp sexp)
360 (choice :tag " User name" string (const nil)))))
00d6fd04 361
03c1ad43 362(defcustom tramp-default-host (system-name)
00d6fd04
MA
363 "*Default host to use for transferring files.
364Useful for su and sudo methods mostly."
365 :group 'tramp
366 :type 'string)
367
368(defcustom tramp-default-proxies-alist nil
369 "*Route to be followed for specific host/user pairs.
370This is an alist of items (HOST USER PROXY). The first matching
371item specifies the proxy to be passed for a file name located on
372a remote target matching USER@HOST. HOST and USER are regular
70c11b0b
MA
373expressions. PROXY must be a Tramp filename without a localname
374part. Method and user name on PROXY are optional, which is
375interpreted with the default values. PROXY can contain the
376patterns %h and %u, which are replaced by the strings matching
377HOST or USER, respectively.
378
379HOST, USER or PROXY could also be Lisp forms, which will be
380evaluated. The result must be a string or nil, which is
381interpreted as a regular expression which always matches."
00d6fd04 382 :group 'tramp
70c11b0b
MA
383 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
384 (choice :tag "User regexp" regexp sexp)
e40fc745 385 (choice :tag " Proxy name" string (const nil)))))
00d6fd04 386
b96e6899
MA
387(defconst tramp-local-host-regexp
388 (concat
66feec8b
MA
389 "\\`"
390 (regexp-opt
391 (list "localhost" "localhost6" (system-name) "127\.0\.0\.1" "::1") t)
392 "\\'")
b96e6899
MA
393 "*Host names which are regarded as local host.")
394
5ec2cc41 395(defvar tramp-completion-function-alist nil
16674e4f 396 "*Alist of methods for remote files.
b533bc97 397This is a list of entries of the form \(NAME PAIR1 PAIR2 ...\).
16674e4f 398Each NAME stands for a remote access method. Each PAIR is of the form
b533bc97 399\(FUNCTION FILE\). FUNCTION is responsible to extract user names and host
16674e4f
KG
400names from FILE for completion. The following predefined FUNCTIONs exists:
401
5ec2cc41
KG
402 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
403 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
404 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
405 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
406 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
407 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
408 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
409 * `tramp-parse-netrc' for \"~/.netrc\" like files.
00d6fd04 410 * `tramp-parse-putty' for PuTTY registry keys.
5ec2cc41
KG
411
412FUNCTION can also be a customer defined function. For more details see
413the info pages.")
414
674da028
MA
415(defconst tramp-echo-mark-marker "_echo"
416 "String marker to surround echoed commands.")
417
68712eb6
MA
418(defconst tramp-echo-mark-marker-length (length tramp-echo-mark-marker)
419 "String length of `tramp-echo-mark-marker'.")
420
421(defconst tramp-echo-mark
422 (concat tramp-echo-mark-marker
423 (make-string tramp-echo-mark-marker-length ?\b))
00d6fd04
MA
424 "String mark to be transmitted around shell commands.
425Used to separate their echo from the output they produce. This
426will only be used if we cannot disable remote echo via stty.
427This string must have no effect on the remote shell except for
428producing some echo which can later be detected by
674da028
MA
429`tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
430followed by an equal number of backspaces to erase them will
431usually suffice.")
00d6fd04 432
68712eb6
MA
433(defconst tramp-echoed-echo-mark-regexp
434 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
435 tramp-echo-mark-marker tramp-echo-mark-marker-length)
00d6fd04
MA
436 "Regexp which matches `tramp-echo-mark' as it gets echoed by
437the remote shell.")
438
fb7933a3
KG
439(defcustom tramp-rsh-end-of-line "\n"
440 "*String used for end of line in rsh connections.
441I don't think this ever needs to be changed, so please tell me about it
16674e4f 442if you need to change this.
90f8dc03
KG
443Also see the method parameter `tramp-password-end-of-line' and the normal
444variable `tramp-default-password-end-of-line'."
16674e4f
KG
445 :group 'tramp
446 :type 'string)
447
90f8dc03
KG
448(defcustom tramp-default-password-end-of-line
449 tramp-rsh-end-of-line
16674e4f 450 "*String used for end of line after sending a password.
90f8dc03
KG
451This variable provides the default value for the method parameter
452`tramp-password-end-of-line', see `tramp-methods' for more details.
453
16674e4f
KG
454It seems that people using plink under Windows need to send
455\"\\r\\n\" (carriage-return, then newline) after a password, but just
456\"\\n\" after all other lines. This variable can be used for the
457password, see `tramp-rsh-end-of-line' for the other cases.
458
459The default value is to use the same value as `tramp-rsh-end-of-line'."
fb7933a3
KG
460 :group 'tramp
461 :type 'string)
462
fb7933a3 463(defcustom tramp-login-prompt-regexp
bc103d00 464 ".*ogin\\( .*\\)?: *"
fb7933a3 465 "*Regexp matching login-like prompts.
bc103d00
MA
466The regexp should match at end of buffer.
467
468Sometimes the prompt is reported to look like \"login as:\"."
fb7933a3
KG
469 :group 'tramp
470 :type 'regexp)
471
821e6e36 472(defcustom tramp-shell-prompt-pattern
aa485f7c 473 ;; Allow a prompt to start right after a ^M since it indeed would be
b81a0b56
MA
474 ;; displayed at the beginning of the line (and Zsh uses it). This
475 ;; regexp works only for GNU Emacs.
476 (concat (if (featurep 'xemacs) "" "\\(?:^\\|\r\\)")
477 "[^#$%>\n]*#?[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*")
821e6e36
KG
478 "Regexp to match prompts from remote shell.
479Normally, Tramp expects you to configure `shell-prompt-pattern'
480correctly, but sometimes it happens that you are connecting to a
481remote host which sends a different kind of shell prompt. Therefore,
482Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
483and also things matched by this variable. The default value of this
b25a52cc 484variable is similar to the default value of `shell-prompt-pattern',
dab816a9
MA
485which should work well in many cases.
486
487This regexp must match both `tramp-initial-end-of-output' and
488`tramp-end-of-output'."
821e6e36
KG
489 :group 'tramp
490 :type 'regexp)
491
fb7933a3 492(defcustom tramp-password-prompt-regexp
00d6fd04 493 "^.*\\([pP]assword\\|[pP]assphrase\\).*:\^@? *"
fb7933a3 494 "*Regexp matching password-like prompts.
ac474af1 495The regexp should match at end of buffer.
fb7933a3
KG
496
497The `sudo' program appears to insert a `^@' character into the prompt."
498 :group 'tramp
499 :type 'regexp)
500
501(defcustom tramp-wrong-passwd-regexp
b1d06e75
KG
502 (concat "^.*"
503 ;; These strings should be on the last line
a4aeb9a4 504 (regexp-opt '("Permission denied"
b1d06e75
KG
505 "Login incorrect"
506 "Login Incorrect"
507 "Connection refused"
27e813fe 508 "Connection closed"
7e5686f0 509 "Timeout, server not responding."
b1d06e75
KG
510 "Sorry, try again."
511 "Name or service not known"
00d6fd04 512 "Host key verification failed."
70c11b0b 513 "No supported authentication methods left to try!") t)
b1d06e75
KG
514 ".*"
515 "\\|"
516 "^.*\\("
517 ;; Here comes a list of regexes, separated by \\|
518 "Received signal [0-9]+"
519 "\\).*")
fb7933a3 520 "*Regexp matching a `login failed' message.
ac474af1
KG
521The regexp should match at end of buffer."
522 :group 'tramp
523 :type 'regexp)
524
525(defcustom tramp-yesno-prompt-regexp
3cdaec13
KG
526 (concat
527 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
528 "\\s-*")
529 "Regular expression matching all yes/no queries which need to be confirmed.
ac474af1 530The confirmation should be done with yes or no.
3cdaec13
KG
531The regexp should match at end of buffer.
532See also `tramp-yn-prompt-regexp'."
fb7933a3
KG
533 :group 'tramp
534 :type 'regexp)
535
3cdaec13 536(defcustom tramp-yn-prompt-regexp
658052a2
MA
537 (concat
538 (regexp-opt '("Store key in cache? (y/n)"
539 "Update cached key? (y/n, Return cancels connection)") t)
540 "\\s-*")
3cdaec13
KG
541 "Regular expression matching all y/n queries which need to be confirmed.
542The confirmation should be done with y or n.
543The regexp should match at end of buffer.
544See also `tramp-yesno-prompt-regexp'."
545 :group 'tramp
546 :type 'regexp)
487f4fb7
KG
547
548(defcustom tramp-terminal-prompt-regexp
549 (concat "\\("
550 "TERM = (.*)"
551 "\\|"
552 "Terminal type\\? \\[.*\\]"
553 "\\)\\s-*")
554 "Regular expression matching all terminal setting prompts.
555The regexp should match at end of buffer.
556The answer will be provided by `tramp-action-terminal', which see."
557 :group 'tramp
558 :type 'regexp)
3cdaec13 559
01917a18
MA
560(defcustom tramp-operation-not-permitted-regexp
561 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
562 (regexp-opt '("Operation not permitted") t))
563 "Regular expression matching keep-date problems in (s)cp operations.
564Copying has been performed successfully already, so this message can
565be ignored safely."
566 :group 'tramp
567 :type 'regexp)
568
6b2633cc
LH
569(defcustom tramp-copy-failed-regexp
570 (concat "\\(.+: "
571 (regexp-opt '("Permission denied"
572 "not a regular file"
573 "is a directory"
574 "No such file or directory") t)
575 "\\)\\s-*")
576 "Regular expression matching copy problems in (s)cp operations."
577 :group 'tramp
578 :type 'regexp)
579
19a87064 580(defcustom tramp-process-alive-regexp
38c65fca 581 ""
19a87064 582 "Regular expression indicating a process has finished.
38c65fca
KG
583In fact this expression is empty by intention, it will be used only to
584check regularly the status of the associated process.
07dfe738 585The answer will be provided by `tramp-action-process-alive',
00d6fd04 586`tramp-action-out-of-band', which see."
38c65fca
KG
587 :group 'tramp
588 :type 'regexp)
589
03c1ad43 590(defconst tramp-temp-name-prefix "tramp."
fb7933a3
KG
591 "*Prefix to use for temporary files.
592If this is a relative file name (such as \"tramp.\"), it is considered
593relative to the directory name returned by the function
9e6ab520 594`tramp-compat-temporary-file-directory' (which see). It may also be an
fb7933a3 595absolute file name; don't forget to include a prefix for the filename
03c1ad43 596part, though.")
fb7933a3 597
2296b54d
MA
598(defconst tramp-temp-buffer-name " *tramp temp*"
599 "Buffer name for a temporary buffer.
600It shall be used in combination with `generate-new-buffer-name'.")
601
b88f2d0a
MA
602(defvar tramp-temp-buffer-file-name nil
603 "File name of a persistent local temporary file.
604Useful for \"rsync\" like methods.")
605(make-variable-buffer-local 'tramp-temp-buffer-file-name)
d68b0220 606(put 'tramp-temp-buffer-file-name 'permanent-local t)
b88f2d0a 607
00d6fd04
MA
608;; XEmacs is distributed with few Lisp packages. Further packages are
609;; installed using EFS. If we use a unified filename format, then
610;; Tramp is required in addition to EFS. (But why can't Tramp just
611;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
612;; just like before.) Another reason for using a separate filename
613;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
614;; Tramp only knows how to deal with `file-name-handler-alist', not
615;; the other places.
616
617;; Currently, we have the choice between 'ftp, 'sep, and 'url.
618;;;###autoload
619(defcustom tramp-syntax
620 (if (featurep 'xemacs) 'sep 'ftp)
621 "Tramp filename syntax to be used.
622
623It can have the following values:
624
625 'ftp -- Ange-FTP respective EFS like syntax (GNU Emacs default)
626 'sep -- Syntax as defined for XEmacs (not available yet for GNU Emacs)
627 'url -- URL-like syntax."
16674e4f 628 :group 'tramp
00d6fd04
MA
629 :type (if (featurep 'xemacs)
630 '(choice (const :tag "EFS" ftp)
631 (const :tag "XEmacs" sep)
632 (const :tag "URL" url))
633 '(choice (const :tag "Ange-FTP" ftp)
634 (const :tag "URL" url))))
635
636(defconst tramp-prefix-format
637 (cond ((equal tramp-syntax 'ftp) "/")
638 ((equal tramp-syntax 'sep) "/[")
639 ((equal tramp-syntax 'url) "/")
640 (t (error "Wrong `tramp-syntax' defined")))
a4aeb9a4 641 "*String matching the very beginning of Tramp file names.
00d6fd04 642Used in `tramp-make-tramp-file-name'.")
16674e4f 643
00d6fd04 644(defconst tramp-prefix-regexp
16674e4f 645 (concat "^" (regexp-quote tramp-prefix-format))
a4aeb9a4 646 "*Regexp matching the very beginning of Tramp file names.
00d6fd04 647Should always start with \"^\". Derived from `tramp-prefix-format'.")
16674e4f 648
00d6fd04 649(defconst tramp-method-regexp
16674e4f 650 "[a-zA-Z_0-9-]+"
00d6fd04 651 "*Regexp matching methods identifiers.")
16674e4f 652
00d6fd04
MA
653(defconst tramp-postfix-method-format
654 (cond ((equal tramp-syntax 'ftp) ":")
655 ((equal tramp-syntax 'sep) "/")
656 ((equal tramp-syntax 'url) "://")
657 (t (error "Wrong `tramp-syntax' defined")))
16674e4f 658 "*String matching delimeter between method and user or host names.
00d6fd04 659Used in `tramp-make-tramp-file-name'.")
16674e4f 660
00d6fd04
MA
661(defconst tramp-postfix-method-regexp
662 (regexp-quote tramp-postfix-method-format)
16674e4f 663 "*Regexp matching delimeter between method and user or host names.
00d6fd04 664Derived from `tramp-postfix-method-format'.")
16674e4f 665
03c1ad43 666(defconst tramp-user-regexp "[^:/ \t]+"
00d6fd04 667 "*Regexp matching user names.")
16674e4f 668
b96e6899
MA
669(defconst tramp-prefix-domain-format "%"
670 "*String matching delimeter between user and domain names.")
671
672(defconst tramp-prefix-domain-regexp
673 (regexp-quote tramp-prefix-domain-format)
674 "*Regexp matching delimeter between user and domain names.
675Derived from `tramp-prefix-domain-format'.")
676
03c1ad43 677(defconst tramp-domain-regexp "[-a-zA-Z0-9_.]+"
b96e6899
MA
678 "*Regexp matching domain names.")
679
680(defconst tramp-user-with-domain-regexp
681 (concat "\\(" tramp-user-regexp "\\)"
682 tramp-prefix-domain-regexp
683 "\\(" tramp-domain-regexp "\\)")
684 "*Regexp matching user names with domain names.")
685
03c1ad43 686(defconst tramp-postfix-user-format "@"
16674e4f 687 "*String matching delimeter between user and host names.
00d6fd04 688Used in `tramp-make-tramp-file-name'.")
16674e4f 689
00d6fd04 690(defconst tramp-postfix-user-regexp
16674e4f
KG
691 (regexp-quote tramp-postfix-user-format)
692 "*Regexp matching delimeter between user and host names.
00d6fd04
MA
693Derived from `tramp-postfix-user-format'.")
694
03c1ad43 695(defconst tramp-host-regexp "[a-zA-Z0-9_.-]+"
00d6fd04
MA
696 "*Regexp matching host names.")
697
b96e6899
MA
698(defconst tramp-prefix-ipv6-format
699 (cond ((equal tramp-syntax 'ftp) "[")
700 ((equal tramp-syntax 'sep) "")
701 ((equal tramp-syntax 'url) "[")
702 (t (error "Wrong `tramp-syntax' defined")))
703 "*String matching left hand side of IPv6 addresses.
704Used in `tramp-make-tramp-file-name'.")
705
706(defconst tramp-prefix-ipv6-regexp
707 (regexp-quote tramp-prefix-ipv6-format)
708 "*Regexp matching left hand side of IPv6 addresses.
709Derived from `tramp-prefix-ipv6-format'.")
710
e0b6e3b9
MA
711;; The following regexp is a bit sloppy. But it shall serve our
712;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
713;; "::ffff:192.168.0.1".
b96e6899 714(defconst tramp-ipv6-regexp
e0b6e3b9 715 "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
b96e6899
MA
716 "*Regexp matching IPv6 addresses.")
717
718(defconst tramp-postfix-ipv6-format
719 (cond ((equal tramp-syntax 'ftp) "]")
720 ((equal tramp-syntax 'sep) "")
721 ((equal tramp-syntax 'url) "]")
722 (t (error "Wrong `tramp-syntax' defined")))
723 "*String matching right hand side of IPv6 addresses.
724Used in `tramp-make-tramp-file-name'.")
725
726(defconst tramp-postfix-ipv6-regexp
727 (regexp-quote tramp-postfix-ipv6-format)
728 "*Regexp matching right hand side of IPv6 addresses.
729Derived from `tramp-postfix-ipv6-format'.")
730
00d6fd04
MA
731(defconst tramp-prefix-port-format
732 (cond ((equal tramp-syntax 'ftp) "#")
733 ((equal tramp-syntax 'sep) "#")
734 ((equal tramp-syntax 'url) ":")
735 (t (error "Wrong `tramp-syntax' defined")))
736 "*String matching delimeter between host names and port numbers.")
737
738(defconst tramp-prefix-port-regexp
739 (regexp-quote tramp-prefix-port-format)
740 "*Regexp matching delimeter between host names and port numbers.
741Derived from `tramp-prefix-port-format'.")
742
03c1ad43 743(defconst tramp-port-regexp "[0-9]+"
00d6fd04
MA
744 "*Regexp matching port numbers.")
745
746(defconst tramp-host-with-port-regexp
747 (concat "\\(" tramp-host-regexp "\\)"
748 tramp-prefix-port-regexp
749 "\\(" tramp-port-regexp "\\)")
750 "*Regexp matching host names with port numbers.")
751
752(defconst tramp-postfix-host-format
753 (cond ((equal tramp-syntax 'ftp) ":")
754 ((equal tramp-syntax 'sep) "]")
755 ((equal tramp-syntax 'url) "")
756 (t (error "Wrong `tramp-syntax' defined")))
7432277c 757 "*String matching delimeter between host names and localnames.
00d6fd04 758Used in `tramp-make-tramp-file-name'.")
16674e4f 759
00d6fd04 760(defconst tramp-postfix-host-regexp
16674e4f 761 (regexp-quote tramp-postfix-host-format)
7432277c 762 "*Regexp matching delimeter between host names and localnames.
00d6fd04 763Derived from `tramp-postfix-host-format'.")
16674e4f 764
03c1ad43 765(defconst tramp-localname-regexp ".*$"
00d6fd04 766 "*Regexp matching localnames.")
16674e4f 767
4a93e698 768;;; File name format:
505edaeb 769
00d6fd04 770(defconst tramp-file-name-structure
16674e4f
KG
771 (list
772 (concat
773 tramp-prefix-regexp
00d6fd04
MA
774 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp "\\)?"
775 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
b96e6899
MA
776 "\\(" "\\(" tramp-host-regexp
777 "\\|"
778 tramp-prefix-ipv6-regexp tramp-ipv6-regexp
779 tramp-postfix-ipv6-regexp "\\)"
780 "\\(" tramp-prefix-port-regexp tramp-port-regexp "\\)?" "\\)?"
00d6fd04
MA
781 tramp-postfix-host-regexp
782 "\\(" tramp-localname-regexp "\\)")
b96e6899 783 2 4 5 8)
16674e4f 784
fb7933a3 785 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
a4aeb9a4 786the Tramp file name structure.
fb7933a3 787
a4aeb9a4 788The first element REGEXP is a regular expression matching a Tramp file
fb7933a3
KG
789name. The regex should contain parentheses around the method name,
790the user name, the host name, and the file name parts.
791
792The second element METHOD is a number, saying which pair of
793parentheses matches the method name. The third element USER is
794similar, but for the user name. The fourth element HOST is similar,
795but for the host name. The fifth element FILE is for the file name.
796These numbers are passed directly to `match-string', which see. That
797means the opening parentheses are counted to identify the pair.
798
00d6fd04 799See also `tramp-file-name-regexp'.")
fb7933a3
KG
800
801;;;###autoload
505edaeb 802(defconst tramp-file-name-regexp-unified
1eb5ca1c
MA
803 (if (memq system-type '(cygwin windows-nt))
804 "\\`/\\([^[/:]\\{2,\\}\\|[^/]\\{2,\\}]\\):"
805 "\\`/\\([^[/:]+\\|[^/]+]\\):")
505edaeb
KG
806 "Value for `tramp-file-name-regexp' for unified remoting.
807Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1eb5ca1c
MA
808Tramp. See `tramp-file-name-structure' for more explanations.
809
810On W32 systems, the volume letter must be ignored.")
505edaeb
KG
811
812;;;###autoload
03c1ad43 813(defconst tramp-file-name-regexp-separate "\\`/\\[.*\\]"
505edaeb
KG
814 "Value for `tramp-file-name-regexp' for separate remoting.
815XEmacs uses a separate filename syntax for Tramp and EFS.
00d6fd04 816See `tramp-file-name-structure' for more explanations.")
505edaeb
KG
817
818;;;###autoload
03c1ad43 819(defconst tramp-file-name-regexp-url "\\`/[^/:]+://"
00d6fd04
MA
820 "Value for `tramp-file-name-regexp' for URL-like remoting.
821See `tramp-file-name-structure' for more explanations.")
822
823;;;###autoload
824(defconst tramp-file-name-regexp
825 (cond ((equal tramp-syntax 'ftp) tramp-file-name-regexp-unified)
826 ((equal tramp-syntax 'sep) tramp-file-name-regexp-separate)
827 ((equal tramp-syntax 'url) tramp-file-name-regexp-url)
828 (t (error "Wrong `tramp-syntax' defined")))
94be87e8 829 "*Regular expression matching file names handled by Tramp.
a4aeb9a4 830This regexp should match Tramp file names but no other file names.
b533bc97 831When tramp.el is loaded, this regular expression is prepended to
fb7933a3 832`file-name-handler-alist', and that is searched sequentially. Thus,
a4aeb9a4
MA
833if the Tramp entry appears rather early in the `file-name-handler-alist'
834and is a bit too general, then some files might be considered Tramp
00d6fd04 835files which are not really Tramp files.
fb7933a3
KG
836
837Please note that the entry in `file-name-handler-alist' is made when
b533bc97 838this file \(tramp.el\) is loaded. This means that this variable must be set
fb7933a3
KG
839before loading tramp.el. Alternatively, `file-name-handler-alist' can be
840updated after changing this variable.
841
00d6fd04 842Also see `tramp-file-name-structure'.")
fb7933a3 843
16674e4f 844;;;###autoload
8a798e41 845(defconst tramp-root-regexp
00d6fd04 846 (if (memq system-type '(cygwin windows-nt))
aa485f7c
MA
847 "\\`\\([a-zA-Z]:\\)?/"
848 "\\`/")
8a798e41 849 "Beginning of an incomplete Tramp file name.
aa485f7c 850Usually, it is just \"\\\\`/\". On W32 systems, there might be a
57671b72 851volume letter, which will be removed by `tramp-drop-volume-letter'.")
8a798e41
MA
852
853;;;###autoload
854(defconst tramp-completion-file-name-regexp-unified
1eb5ca1c
MA
855 (if (memq system-type '(cygwin windows-nt))
856 (concat tramp-root-regexp "[^/]\\{2,\\}\\'")
857 (concat tramp-root-regexp "[^/]*\\'"))
16674e4f 858 "Value for `tramp-completion-file-name-regexp' for unified remoting.
8a798e41 859GNU Emacs uses a unified filename syntax for Tramp and Ange-FTP.
1eb5ca1c
MA
860See `tramp-file-name-structure' for more explanations.
861
862On W32 systems, the volume letter must be ignored.")
fb7933a3 863
16674e4f
KG
864;;;###autoload
865(defconst tramp-completion-file-name-regexp-separate
aa485f7c 866 (concat tramp-root-regexp "\\([[][^]]*\\)?\\'")
16674e4f
KG
867 "Value for `tramp-completion-file-name-regexp' for separate remoting.
868XEmacs uses a separate filename syntax for Tramp and EFS.
00d6fd04 869See `tramp-file-name-structure' for more explanations.")
fb7933a3 870
16674e4f 871;;;###autoload
00d6fd04 872(defconst tramp-completion-file-name-regexp-url
aa485f7c 873 (concat tramp-root-regexp "[^/:]+\\(:\\(/\\(/[^/]*\\)?\\)?\\)?\\'")
00d6fd04
MA
874 "Value for `tramp-completion-file-name-regexp' for URL-like remoting.
875See `tramp-file-name-structure' for more explanations.")
876
877;;;###autoload
878(defconst tramp-completion-file-name-regexp
879 (cond ((equal tramp-syntax 'ftp) tramp-completion-file-name-regexp-unified)
880 ((equal tramp-syntax 'sep) tramp-completion-file-name-regexp-separate)
881 ((equal tramp-syntax 'url) tramp-completion-file-name-regexp-url)
882 (t (error "Wrong `tramp-syntax' defined")))
a4aeb9a4
MA
883 "*Regular expression matching file names handled by Tramp completion.
884This regexp should match partial Tramp file names only.
16674e4f
KG
885
886Please note that the entry in `file-name-handler-alist' is made when
887this file (tramp.el) is loaded. This means that this variable must be set
888before loading tramp.el. Alternatively, `file-name-handler-alist' can be
889updated after changing this variable.
890
00d6fd04 891Also see `tramp-file-name-structure'.")
fb7933a3 892
00d6fd04 893;; Chunked sending kludge. We set this to 500 for black-listed constellations
7432277c 894;; known to have a bug in `process-send-string'; some ssh connections appear
7177e2a3
MA
895;; to drop bytes when data is sent too quickly. There is also a connection
896;; buffer local variable, which is computed depending on remote host properties
897;; when `tramp-chunksize' is zero or nil.
7432277c
KG
898(defcustom tramp-chunksize
899 (when (and (not (featurep 'xemacs))
900 (memq system-type '(hpux)))
901 500)
55880756
MA
902;; Parentheses in docstring starting at beginning of line are escaped.
903;; Fontification is messed up when
904;; `open-paren-in-column-0-is-defun-start' set to t.
7432277c
KG
905 "*If non-nil, chunksize for sending input to local process.
906It is necessary only on systems which have a buggy `process-send-string'
907implementation. The necessity, whether this variable must be set, can be
908checked via the following code:
909
910 (with-temp-buffer
11948172
MA
911 (let* ((user \"xxx\") (host \"yyy\")
912 (init 0) (step 50)
913 (sent init) (received init))
914 (while (= sent received)
915 (setq sent (+ sent step))
916 (erase-buffer)
917 (let ((proc (start-process (buffer-name) (current-buffer)
918 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
919 (when (memq (process-status proc) '(run open))
920 (process-send-string proc (make-string sent ?\\ ))
921 (process-send-eof proc)
922 (process-send-eof proc))
923 (while (not (progn (goto-char (point-min))
924 (re-search-forward \"\\\\w+\" (point-max) t)))
925 (accept-process-output proc 1))
926 (when (memq (process-status proc) '(run open))
927 (setq received (string-to-number (match-string 0)))
928 (delete-process proc)
929 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
930 (sit-for 0))))
931 (if (> sent (+ init step))
932 (message \"You should set `tramp-chunksize' to a maximum of %s\"
933 (- sent step))
934 (message \"Test does not work\")
935 (display-buffer (current-buffer))
936 (sit-for 30))))
937
938In the Emacs normally running Tramp, evaluate the above code
55880756 939\(replace \"xxx\" and \"yyy\" by the remote user and host name,
b533bc97 940respectively\). You can do this, for example, by pasting it into
11948172
MA
941the `*scratch*' buffer and then hitting C-j with the cursor after the
942last closing parenthesis. Note that it works only if you have configured
b533bc97 943\"ssh\" to run without password query, see ssh-agent\(1\).
11948172
MA
944
945You will see the number of bytes sent successfully to the remote host.
946If that number exceeds 1000, you can stop the execution by hitting
947C-g, because your Emacs is likely clean.
948
11948172 949When it is necessary to set `tramp-chunksize', you might consider to
b533bc97
MA
950use an out-of-the-band method \(like \"scp\"\) instead of an internal one
951\(like \"ssh\"\), because setting `tramp-chunksize' to non-nil decreases
11948172 952performance.
c951aecb 953
00d6fd04
MA
954If your Emacs is buggy, the code stops and gives you an indication
955about the value `tramp-chunksize' should be set. Maybe you could just
956experiment a bit, e.g. changing the values of `init' and `step'
957in the third line of the code.
958
7432277c
KG
959Please raise a bug report via \"M-x tramp-bug\" if your system needs
960this variable to be set as well."
961 :group 'tramp
b1a2b924 962 :type '(choice (const nil) integer))
7432277c 963
5ec2cc41
KG
964;; Logging in to a remote host normally requires obtaining a pty. But
965;; Emacs on MacOS X has process-connection-type set to nil by default,
966;; so on those systems Tramp doesn't obtain a pty. Here, we allow
967;; for an override of the system default.
968(defcustom tramp-process-connection-type t
969 "Overrides `process-connection-type' for connections from Tramp.
970Tramp binds process-connection-type to the value given here before
971opening a connection to a remote host."
972 :group 'tramp
973 :type '(choice (const nil) (const t) (const pty)))
974
b50dd0d2
MA
975(defcustom tramp-completion-reread-directory-timeout 10
976 "Defines seconds since last remote command before rereading a directory.
977A remote directory might have changed its contents. In order to
978make it visible during file name completion in the minibuffer,
979Tramp flushes its cache and rereads the directory contents when
980more than `tramp-completion-reread-directory-timeout' seconds
4bc3c53d
MA
981have been gone since last remote command execution. A value of `t'
982would require an immediate reread during filename completion, `nil'
b50dd0d2
MA
983means to use always cached values for the directory contents."
984 :group 'tramp
985 :type '(choice (const nil) integer))
986
fb7933a3
KG
987;;; Internal Variables:
988
fb7933a3 989(defvar tramp-current-method nil
00d6fd04 990 "Connection method for this *tramp* buffer.")
fb7933a3
KG
991
992(defvar tramp-current-user nil
00d6fd04 993 "Remote login name for this *tramp* buffer.")
fb7933a3
KG
994
995(defvar tramp-current-host nil
00d6fd04
MA
996 "Remote host for this *tramp* buffer.")
997
a01b1e22 998;;;###autoload
16674e4f 999(defconst tramp-completion-file-name-handler-alist
a01b1e22 1000 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
41c8e348 1001 (file-name-completion . tramp-completion-handle-file-name-completion))
16674e4f 1002 "Alist of completion handler functions.
03c1ad43
MA
1003Used for file names matching `tramp-file-name-regexp'. Operations
1004not mentioned here will be handled by Tramp's file name handler
1005functions, or the normal Emacs functions.")
16674e4f 1006
4007ba5b 1007;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
0f34aa77 1008;;;###tramp-autoload
03c1ad43 1009(defvar tramp-foreign-file-name-handler-alist nil
4007ba5b
KG
1010 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1011If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1012calling HANDLER.")
1013
0664ff72 1014;;; Internal functions which must come first:
fb7933a3 1015
0f34aa77
MA
1016;; Conversion functions between external representation and
1017;; internal data structure. Convenience functions for internal
1018;; data structure.
1019
1020(defun tramp-file-name-p (vec)
1021 "Check, whether VEC is a Tramp object."
1022 (and (vectorp vec) (= 4 (length vec))))
1023
1024(defun tramp-file-name-method (vec)
1025 "Return method component of VEC."
1026 (and (tramp-file-name-p vec) (aref vec 0)))
1027
1028(defun tramp-file-name-user (vec)
1029 "Return user component of VEC."
1030 (and (tramp-file-name-p vec) (aref vec 1)))
1031
1032(defun tramp-file-name-host (vec)
1033 "Return host component of VEC."
1034 (and (tramp-file-name-p vec) (aref vec 2)))
1035
1036(defun tramp-file-name-localname (vec)
1037 "Return localname component of VEC."
1038 (and (tramp-file-name-p vec) (aref vec 3)))
1039
1040;; The user part of a Tramp file name vector can be of kind
1041;; "user%domain". Sometimes, we must extract these parts.
1042(defun tramp-file-name-real-user (vec)
1043 "Return the user name of VEC without domain."
1044 (save-match-data
1045 (let ((user (tramp-file-name-user vec)))
1046 (if (and (stringp user)
1047 (string-match tramp-user-with-domain-regexp user))
1048 (match-string 1 user)
1049 user))))
1050
1051(defun tramp-file-name-domain (vec)
1052 "Return the domain name of VEC."
1053 (save-match-data
1054 (let ((user (tramp-file-name-user vec)))
1055 (and (stringp user)
1056 (string-match tramp-user-with-domain-regexp user)
1057 (match-string 2 user)))))
1058
1059;; The host part of a Tramp file name vector can be of kind
1060;; "host#port". Sometimes, we must extract these parts.
1061(defun tramp-file-name-real-host (vec)
1062 "Return the host name of VEC without port."
1063 (save-match-data
1064 (let ((host (tramp-file-name-host vec)))
1065 (if (and (stringp host)
1066 (string-match tramp-host-with-port-regexp host))
1067 (match-string 1 host)
1068 host))))
1069
1070(defun tramp-file-name-port (vec)
1071 "Return the port number of VEC."
1072 (save-match-data
66feec8b
MA
1073 (let ((method (tramp-file-name-method vec))
1074 (host (tramp-file-name-host vec)))
1075 (or (and (stringp host)
1076 (string-match tramp-host-with-port-regexp host)
1077 (string-to-number (match-string 2 host)))
1078 (tramp-get-method-parameter method 'tramp-default-port)))))
0f34aa77
MA
1079
1080;;;###tramp-autoload
1081(defun tramp-tramp-file-p (name)
1082 "Return t if NAME is a string with Tramp file name syntax."
1083 (save-match-data
1084 (and (stringp name) (string-match tramp-file-name-regexp name))))
1085
1086(defun tramp-find-method (method user host)
1087 "Return the right method string to use.
1088This is METHOD, if non-nil. Otherwise, do a lookup in
1089`tramp-default-method-alist'."
1090 (or method
1091 (let ((choices tramp-default-method-alist)
1092 lmethod item)
1093 (while choices
1094 (setq item (pop choices))
1095 (when (and (string-match (or (nth 0 item) "") (or host ""))
1096 (string-match (or (nth 1 item) "") (or user "")))
1097 (setq lmethod (nth 2 item))
1098 (setq choices nil)))
1099 lmethod)
1100 tramp-default-method))
1101
1102(defun tramp-find-user (method user host)
1103 "Return the right user string to use.
1104This is USER, if non-nil. Otherwise, do a lookup in
1105`tramp-default-user-alist'."
1106 (or user
1107 (let ((choices tramp-default-user-alist)
1108 luser item)
1109 (while choices
1110 (setq item (pop choices))
1111 (when (and (string-match (or (nth 0 item) "") (or method ""))
1112 (string-match (or (nth 1 item) "") (or host "")))
1113 (setq luser (nth 2 item))
1114 (setq choices nil)))
1115 luser)
1116 tramp-default-user))
1117
1118(defun tramp-find-host (method user host)
1119 "Return the right host string to use.
1120This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
1121 (or (and (> (length host) 0) host)
1122 tramp-default-host))
1123
1124(defun tramp-dissect-file-name (name &optional nodefault)
1125 "Return a `tramp-file-name' structure.
1126The structure consists of remote method, remote user, remote host
1127and localname (file name on remote host). If NODEFAULT is
1128non-nil, the file name parts are not expanded to their default
1129values."
1130 (save-match-data
1131 (let ((match (string-match (nth 0 tramp-file-name-structure) name)))
1132 (unless match (error "Not a Tramp file name: %s" name))
1133 (let ((method (match-string (nth 1 tramp-file-name-structure) name))
1134 (user (match-string (nth 2 tramp-file-name-structure) name))
1135 (host (match-string (nth 3 tramp-file-name-structure) name))
1136 (localname (match-string (nth 4 tramp-file-name-structure) name)))
0f34aa77
MA
1137 (when host
1138 (when (string-match tramp-prefix-ipv6-regexp host)
1139 (setq host (replace-match "" nil t host)))
1140 (when (string-match tramp-postfix-ipv6-regexp host)
1141 (setq host (replace-match "" nil t host))))
1142 (if nodefault
1143 (vector method user host localname)
1144 (vector
1145 (tramp-find-method method user host)
1146 (tramp-find-user method user host)
1147 (tramp-find-host method user host)
1148 localname))))))
1149
1150(defun tramp-buffer-name (vec)
1151 "A name for the connection buffer VEC."
1152 ;; We must use `tramp-file-name-real-host', because for gateway
1153 ;; methods the default port will be expanded later on, which would
1154 ;; tamper the name.
1155 (let ((method (tramp-file-name-method vec))
1156 (user (tramp-file-name-user vec))
1157 (host (tramp-file-name-real-host vec)))
1158 (if (not (zerop (length user)))
1159 (format "*tramp/%s %s@%s*" method user host)
1160 (format "*tramp/%s %s*" method host))))
1161
1162(defun tramp-make-tramp-file-name (method user host localname)
1163 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME."
1164 (concat tramp-prefix-format
1165 (when (not (zerop (length method)))
1166 (concat method tramp-postfix-method-format))
1167 (when (not (zerop (length user)))
1168 (concat user tramp-postfix-user-format))
1169 (when host
1170 (if (string-match tramp-ipv6-regexp host)
1171 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
1172 host))
1173 tramp-postfix-host-format
1174 (when localname localname)))
1175
1176(defun tramp-completion-make-tramp-file-name (method user host localname)
1177 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
1178It must not be a complete Tramp file name, but as long as there are
1179necessary only. This function will be used in file name completion."
1180 (concat tramp-prefix-format
1181 (when (not (zerop (length method)))
1182 (concat method tramp-postfix-method-format))
1183 (when (not (zerop (length user)))
1184 (concat user tramp-postfix-user-format))
1185 (when (not (zerop (length host)))
1186 (concat
1187 (if (string-match tramp-ipv6-regexp host)
03c1ad43
MA
1188 (concat
1189 tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
0f34aa77
MA
1190 host)
1191 tramp-postfix-host-format))
1192 (when localname localname)))
1193
1194(defun tramp-get-buffer (vec)
1195 "Get the connection buffer to be used for VEC."
1196 (or (get-buffer (tramp-buffer-name vec))
1197 (with-current-buffer (get-buffer-create (tramp-buffer-name vec))
1198 (setq buffer-undo-list t)
1199 (setq default-directory
1200 (tramp-make-tramp-file-name
1201 (tramp-file-name-method vec)
1202 (tramp-file-name-user vec)
1203 (tramp-file-name-host vec)
1204 "/"))
1205 (current-buffer))))
1206
1207(defun tramp-get-connection-buffer (vec)
1208 "Get the connection buffer to be used for VEC.
1209In case a second asynchronous communication has been started, it is different
1210from `tramp-get-buffer'."
1211 (or (tramp-get-connection-property vec "process-buffer" nil)
1212 (tramp-get-buffer vec)))
1213
66feec8b
MA
1214(defun tramp-get-connection-name (vec)
1215 "Get the connection name to be used for VEC.
1216In case a second asynchronous communication has been started, it is different
1217from the default one."
1218 (or (tramp-get-connection-property vec "process-name" nil)
1219 (tramp-buffer-name vec)))
1220
0f34aa77
MA
1221(defun tramp-get-connection-process (vec)
1222 "Get the connection process to be used for VEC.
1223In case a second asynchronous communication has been started, it is different
1224from the default one."
66feec8b 1225 (get-process (tramp-get-connection-name vec)))
0f34aa77
MA
1226
1227(defun tramp-debug-buffer-name (vec)
1228 "A name for the debug buffer for VEC."
1229 ;; We must use `tramp-file-name-real-host', because for gateway
1230 ;; methods the default port will be expanded later on, which would
1231 ;; tamper the name.
1232 (let ((method (tramp-file-name-method vec))
1233 (user (tramp-file-name-user vec))
1234 (host (tramp-file-name-real-host vec)))
1235 (if (not (zerop (length user)))
1236 (format "*debug tramp/%s %s@%s*" method user host)
1237 (format "*debug tramp/%s %s*" method host))))
1238
1239(defconst tramp-debug-outline-regexp
03c1ad43
MA
1240 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #"
1241 "Used for highlighting Tramp debug buffers in `outline-mode'.")
1242
1243(defun tramp-debug-outline-level ()
1244 "Return the depth to which a statement is nested in the outline.
1245Point must be at the beginning of a header line.
1246
1247The outline level is equal to the verbosity of the Tramp message."
1248 (1+ (string-to-number (match-string 1))))
0f34aa77
MA
1249
1250(defun tramp-get-debug-buffer (vec)
1251 "Get the debug buffer for VEC."
1252 (with-current-buffer
1253 (get-buffer-create (tramp-debug-buffer-name vec))
1254 (when (bobp)
1255 (setq buffer-undo-list t)
1256 ;; Activate `outline-mode'. This runs `text-mode-hook' and
1257 ;; `outline-mode-hook'. We must prevent that local processes
1258 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
1259 ;; Furthermore, `outline-regexp' must have the correct value
1260 ;; already, because it is used by `font-lock-compile-keywords'.
1261 (let ((default-directory (tramp-compat-temporary-file-directory))
1262 (outline-regexp tramp-debug-outline-regexp))
1263 (outline-mode))
1264 (set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp)
03c1ad43 1265 (set (make-local-variable 'outline-level) 'tramp-debug-outline-level))
0f34aa77
MA
1266 (current-buffer)))
1267
00d6fd04
MA
1268(defsubst tramp-debug-message (vec fmt-string &rest args)
1269 "Append message to debug buffer.
1270Message is formatted with FMT-STRING as control string and the remaining
1271ARGS to actually emit the message (if applicable)."
1272 (when (get-buffer (tramp-buffer-name vec))
1273 (with-current-buffer (tramp-get-debug-buffer vec)
1274 (goto-char (point-max))
70c11b0b
MA
1275 ;; Headline.
1276 (when (bobp)
1277 (insert
1278 (format
1279 ";; %sEmacs: %s Tramp: %s -*- mode: outline; -*-"
1280 (if (featurep 'sxemacs) "SX" (if (featurep 'xemacs) "X" "GNU "))
1281 emacs-version tramp-version)))
00d6fd04
MA
1282 (unless (bolp)
1283 (insert "\n"))
70c11b0b 1284 ;; Timestamp.
736ac90f
MA
1285 (let ((now (current-time)))
1286 (insert (format-time-string "%T." now))
1287 (insert (format "%06d " (nth 2 now))))
70c11b0b 1288 ;; Calling function.
00d6fd04
MA
1289 (let ((btn 1) btf fn)
1290 (while (not fn)
1291 (setq btf (nth 1 (backtrace-frame btn)))
1292 (if (not btf)
1293 (setq fn "")
1294 (when (symbolp btf)
1295 (setq fn (symbol-name btf))
1296 (unless (and (string-match "^tramp" fn)
1297 (not (string-match
66feec8b 1298 "^tramp\\(-debug\\)?\\(-message\\|-error\\|-compat\\(-funcall\\|-with-temp-message\\)\\)$"
00d6fd04
MA
1299 fn)))
1300 (setq fn nil)))
1301 (setq btn (1+ btn))))
1302 ;; The following code inserts filename and line number.
1303 ;; Should be deactivated by default, because it is time
1304 ;; consuming.
1305; (let ((ffn (find-function-noselect (intern fn))))
1306; (insert
1307; (format
1308; "%s:%d: "
1309; (file-name-nondirectory (buffer-file-name (car ffn)))
1310; (with-current-buffer (car ffn)
1311; (1+ (count-lines (point-min) (cdr ffn)))))))
1312 (insert (format "%s " fn)))
70c11b0b 1313 ;; The message.
00d6fd04
MA
1314 (insert (apply 'format fmt-string args)))))
1315
946a5aeb
MA
1316(defvar tramp-message-show-message t
1317 "Show Tramp message in the minibuffer.
1318This variable is used to disable messages from `tramp-error'.
1319The messages are visible anyway, because an error is raised.")
1320
00d6fd04 1321(defsubst tramp-message (vec-or-proc level fmt-string &rest args)
fb7933a3 1322 "Emit a message depending on verbosity level.
a4aeb9a4 1323VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
00d6fd04
MA
1324vector or a process. LEVEL says to be quiet if `tramp-verbose' is
1325less than LEVEL. The message is emitted only if `tramp-verbose' is
1326greater than or equal to LEVEL.
1327
1328The message is also logged into the debug buffer when `tramp-verbose'
1329is greater than or equal 4.
1330
1331Calls functions `message' and `tramp-debug-message' with FMT-STRING as
1332control string and the remaining ARGS to actually emit the message (if
1333applicable)."
03c1ad43
MA
1334 (ignore-errors
1335 (when (<= level tramp-verbose)
1336 ;; Match data must be preserved!
1337 (save-match-data
1338 ;; Display only when there is a minimum level.
1339 (when (and tramp-message-show-message (<= level 3))
1340 (apply 'message
1341 (concat
1342 (cond
1343 ((= level 0) "")
1344 ((= level 1) "")
1345 ((= level 2) "Warning: ")
1346 (t "Tramp: "))
1347 fmt-string)
1348 args))
1349 ;; Log only when there is a minimum level.
1350 (when (>= tramp-verbose 4)
1351 (when (and vec-or-proc
1352 (processp vec-or-proc)
1353 (buffer-name (process-buffer vec-or-proc)))
1354 (with-current-buffer (process-buffer vec-or-proc)
1355 ;; Translate proc to vec.
1356 (setq vec-or-proc (tramp-dissect-file-name default-directory))))
1357 (when (and vec-or-proc (vectorp vec-or-proc))
1358 (apply 'tramp-debug-message
1359 vec-or-proc
1360 (concat (format "(%d) # " level) fmt-string)
1361 args)))))))
00d6fd04
MA
1362
1363(defsubst tramp-error (vec-or-proc signal fmt-string &rest args)
1364 "Emit an error.
1365VEC-OR-PROC identifies the connection to use, SIGNAL is the
1366signal identifier to be raised, remaining args passed to
1367`tramp-message'. Finally, signal SIGNAL is raised."
946a5aeb
MA
1368 (let (tramp-message-show-message)
1369 (tramp-message
1370 vec-or-proc 1 "%s"
1371 (error-message-string
1372 (list signal
1373 (get signal 'error-message)
1374 (apply 'format fmt-string args))))
1375 (signal signal (list (apply 'format fmt-string args)))))
00d6fd04
MA
1376
1377(defsubst tramp-error-with-buffer
1378 (buffer vec-or-proc signal fmt-string &rest args)
1379 "Emit an error, and show BUFFER.
1380If BUFFER is nil, show the connection buffer. Wait for 30\", or until
1381an input event arrives. The other arguments are passed to `tramp-error'."
1382 (save-window-excursion
1383 (unwind-protect
1384 (apply 'tramp-error vec-or-proc signal fmt-string args)
4874f5e6
MA
1385 (when (and vec-or-proc
1386 (not (zerop tramp-verbose))
1387 (not (tramp-completion-mode-p)))
00d6fd04
MA
1388 (let ((enable-recursive-minibuffers t))
1389 (pop-to-buffer
1390 (or (and (bufferp buffer) buffer)
1391 (and (processp vec-or-proc) (process-buffer vec-or-proc))
1392 (tramp-get-buffer vec-or-proc)))
1393 (sit-for 30))))))
fb7933a3 1394
c62c9d08
KG
1395(defmacro with-parsed-tramp-file-name (filename var &rest body)
1396 "Parse a Tramp filename and make components available in the body.
1397
1398First arg FILENAME is evaluated and dissected into its components.
1399Second arg VAR is a symbol. It is used as a variable name to hold
1400the filename structure. It is also used as a prefix for the variables
1401holding the components. For example, if VAR is the symbol `foo', then
00d6fd04
MA
1402`foo' will be bound to the whole structure, `foo-method' will be bound to
1403the method component, and so on for `foo-user', `foo-host', `foo-localname'.
c62c9d08
KG
1404
1405Remaining args are Lisp expressions to be evaluated (inside an implicit
1406`progn').
1407
00d6fd04
MA
1408If VAR is nil, then we bind `v' to the structure and `method', `user',
1409`host', `localname' to the components."
c62c9d08 1410 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
c62c9d08
KG
1411 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
1412 (tramp-file-name-method ,(or var 'v)))
1413 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
1414 (tramp-file-name-user ,(or var 'v)))
1415 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
1416 (tramp-file-name-host ,(or var 'v)))
7432277c
KG
1417 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
1418 (tramp-file-name-localname ,(or var 'v))))
c62c9d08
KG
1419 ,@body))
1420
1421(put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
00d6fd04 1422(put 'with-parsed-tramp-file-name 'edebug-form-spec '(form symbolp body))
6139f995
MA
1423(tramp-compat-font-lock-add-keywords
1424 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>"))
00d6fd04 1425
9e021389
MA
1426(defun tramp-progress-reporter-update (reporter &optional value)
1427 (let* ((parameters (cdr reporter))
1428 (message (aref parameters 3)))
1429 (when (string-match message (or (current-message) ""))
6139f995 1430 (tramp-compat-funcall 'progress-reporter-update reporter value))))
9e021389 1431
a94d821f 1432(defmacro with-progress-reporter (vec level message &rest body)
b6827fff
MA
1433 "Executes BODY, spinning a progress reporter with MESSAGE.
1434If LEVEL does not fit for visible messages, or if this is a
1435nested call of the macro, there are only traces without a visible
1436progress reporter."
a94d821f
MA
1437 `(let (pr tm)
1438 (tramp-message ,vec ,level "%s..." ,message)
1439 ;; We start a pulsing progress reporter after 3 seconds. Feature
1440 ;; introduced in Emacs 24.1.
3b30ccda
MA
1441 (when (and tramp-message-show-message
1442 ;; Display only when there is a minimum level.
1443 (<= ,level (min tramp-verbose 3)))
03c1ad43
MA
1444 (ignore-errors
1445 (setq pr (tramp-compat-funcall 'make-progress-reporter ,message)
1446 tm (when pr
1447 (run-at-time 3 0.1 'tramp-progress-reporter-update pr)))))
a94d821f 1448 (unwind-protect
b6827fff
MA
1449 ;; Execute the body. Unset `tramp-message-show-message' when
1450 ;; the timer object is created, in order to suppress
1451 ;; concurrent timers.
3b30ccda
MA
1452 (let ((tramp-message-show-message
1453 (and tramp-message-show-message (not tm))))
1454 ,@body)
a94d821f 1455 ;; Stop progress reporter.
0d5852cf 1456 (if tm (tramp-compat-funcall 'cancel-timer tm))
a94d821f
MA
1457 (tramp-message ,vec ,level "%s...done" ,message))))
1458
1459(put 'with-progress-reporter 'lisp-indent-function 3)
1460(put 'with-progress-reporter 'edebug-form-spec t)
6139f995
MA
1461(tramp-compat-font-lock-add-keywords
1462 'emacs-lisp-mode '("\\<with-progress-reporter\\>"))
a94d821f
MA
1463
1464(eval-and-compile ;; Silence compiler.
628c97b2
GM
1465 (if (memq system-type '(cygwin windows-nt))
1466 (defun tramp-drop-volume-letter (name)
1467 "Cut off unnecessary drive letter from file NAME.
66feec8b 1468The functions `tramp-*-handle-expand-file-name' call `expand-file-name'
628c97b2
GM
1469locally on a remote file name. When the local system is a W32 system
1470but the remote system is Unix, this introduces a superfluous drive
1471letter into the file name. This function removes it."
1472 (save-match-data
1473 (if (string-match tramp-root-regexp name)
1474 (replace-match "/" nil t name)
1475 name)))
1476
1477 (defalias 'tramp-drop-volume-letter 'identity)))
1478
16674e4f
KG
1479;;; Config Manipulation Functions:
1480
1481(defun tramp-set-completion-function (method function-list)
1482 "Sets the list of completion functions for METHOD.
1483FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1484The FUNCTION is intended to parse FILE according its syntax.
1485It might be a predefined FUNCTION, or a user defined FUNCTION.
1486Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
8fc29035 1487`tramp-parse-sconfig', `tramp-parse-hosts', `tramp-parse-passwd',
8daea7fc
KG
1488and `tramp-parse-netrc'.
1489
16674e4f
KG
1490Example:
1491
1492 (tramp-set-completion-function
1493 \"ssh\"
8daea7fc
KG
1494 '((tramp-parse-sconfig \"/etc/ssh_config\")
1495 (tramp-parse-sconfig \"~/.ssh/config\")))"
16674e4f 1496
5ec2cc41
KG
1497 (let ((r function-list)
1498 (v function-list))
1499 (setq tramp-completion-function-alist
1500 (delete (assoc method tramp-completion-function-alist)
1501 tramp-completion-function-alist))
1502
1503 (while v
00d6fd04 1504 ;; Remove double entries.
5ec2cc41
KG
1505 (when (member (car v) (cdr v))
1506 (setcdr v (delete (car v) (cdr v))))
00d6fd04 1507 ;; Check for function and file or registry key.
5ec2cc41 1508 (unless (and (functionp (nth 0 (car v)))
00d6fd04
MA
1509 (if (string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
1510 ;; Windows registry.
1511 (and (memq system-type '(cygwin windows-nt))
a4aeb9a4 1512 (zerop
0f34aa77 1513 (tramp-compat-call-process
a4aeb9a4 1514 "reg" nil nil nil "query" (nth 1 (car v)))))
00d6fd04
MA
1515 ;; Configuration file.
1516 (file-exists-p (nth 1 (car v)))))
5ec2cc41
KG
1517 (setq r (delete (car v) r)))
1518 (setq v (cdr v)))
1519
1520 (when r
4007ba5b 1521 (add-to-list 'tramp-completion-function-alist
5ec2cc41 1522 (cons method r)))))
16674e4f
KG
1523
1524(defun tramp-get-completion-function (method)
00d6fd04 1525 "Returns a list of completion functions for METHOD.
16674e4f 1526For definition of that list see `tramp-set-completion-function'."
00d6fd04
MA
1527 (cons
1528 ;; Hosts visited once shall be remembered.
1529 `(tramp-parse-connection-properties ,method)
1530 ;; The method related defaults.
1531 (cdr (assoc method tramp-completion-function-alist))))
16674e4f 1532
d037d501 1533
0664ff72 1534;;; Fontification of `read-file-name':
d037d501 1535
0664ff72 1536;; rfn-eshadow.el is part of Emacs 22. It is autoloaded.
d037d501
MA
1537(defvar tramp-rfn-eshadow-overlay)
1538(make-variable-buffer-local 'tramp-rfn-eshadow-overlay)
1539
1540(defun tramp-rfn-eshadow-setup-minibuffer ()
1541 "Set up a minibuffer for `file-name-shadow-mode'.
1542Adds another overlay hiding filename parts according to Tramp's
1543special handling of `substitute-in-file-name'."
9ce8462a 1544 (when (symbol-value 'minibuffer-completing-file-name)
d037d501 1545 (setq tramp-rfn-eshadow-overlay
0d5852cf
MA
1546 (tramp-compat-funcall
1547 'make-overlay
1548 (tramp-compat-funcall 'minibuffer-prompt-end)
1549 (tramp-compat-funcall 'minibuffer-prompt-end)))
d037d501 1550 ;; Copy rfn-eshadow-overlay properties.
0d5852cf
MA
1551 (let ((props (tramp-compat-funcall
1552 'overlay-properties (symbol-value 'rfn-eshadow-overlay))))
d037d501 1553 (while props
0d5852cf
MA
1554 (tramp-compat-funcall
1555 'overlay-put tramp-rfn-eshadow-overlay (pop props) (pop props))))))
d037d501
MA
1556
1557(when (boundp 'rfn-eshadow-setup-minibuffer-hook)
1558 (add-hook 'rfn-eshadow-setup-minibuffer-hook
48846dc5
MA
1559 'tramp-rfn-eshadow-setup-minibuffer)
1560 (add-hook 'tramp-unload-hook
aa485f7c
MA
1561 (lambda ()
1562 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
1563 'tramp-rfn-eshadow-setup-minibuffer))))
d037d501 1564
adcbca53
MA
1565(defconst tramp-rfn-eshadow-update-overlay-regexp
1566 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format))
1567
d037d501
MA
1568(defun tramp-rfn-eshadow-update-overlay ()
1569 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
1570This is intended to be used as a minibuffer `post-command-hook' for
1571`file-name-shadow-mode'; the minibuffer should have already
1572been set up by `rfn-eshadow-setup-minibuffer'."
1573 ;; In remote files name, there is a shadowing just for the local part.
0d5852cf
MA
1574 (let ((end (or (tramp-compat-funcall
1575 'overlay-end (symbol-value 'rfn-eshadow-overlay))
1576 (tramp-compat-funcall 'minibuffer-prompt-end))))
1577 (when
1578 (file-remote-p
1579 (tramp-compat-funcall 'buffer-substring-no-properties end (point-max)))
bd316474
KY
1580 (save-excursion
1581 (save-restriction
1582 (narrow-to-region
adcbca53
MA
1583 (1+ (or (string-match
1584 tramp-rfn-eshadow-update-overlay-regexp (buffer-string) end)
1585 end))
1586 (point-max))
bd316474
KY
1587 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay)
1588 (rfn-eshadow-update-overlay-hook nil))
0d5852cf
MA
1589 (tramp-compat-funcall
1590 'move-overlay rfn-eshadow-overlay (point-max) (point-max))
1591 (tramp-compat-funcall 'rfn-eshadow-update-overlay)))))))
d037d501
MA
1592
1593(when (boundp 'rfn-eshadow-update-overlay-hook)
1594 (add-hook 'rfn-eshadow-update-overlay-hook
b88f2d0a
MA
1595 'tramp-rfn-eshadow-update-overlay)
1596 (add-hook 'tramp-unload-hook
1597 (lambda ()
1598 (remove-hook 'rfn-eshadow-update-overlay-hook
1599 'tramp-rfn-eshadow-update-overlay))))
d037d501 1600
00d6fd04
MA
1601;; Inodes don't exist for some file systems. Therefore we must
1602;; generate virtual ones. Used in `find-buffer-visiting'. The method
1603;; applied might be not so efficient (Ange-FTP uses hashes). But
1604;; performance isn't the major issue given that file transfer will
1605;; take time.
1606(defvar tramp-inodes nil
1607 "Keeps virtual inodes numbers.")
1608
8daea7fc
KG
1609;; Devices must distinguish physical file systems. The device numbers
1610;; provided by "lstat" aren't unique, because we operate on different hosts.
1611;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
1612;; EFS use device number "-1". In order to be different, we use device number
b946a456 1613;; (-1 . x), whereby "x" is unique for a given (method user host).
8daea7fc
KG
1614(defvar tramp-devices nil
1615 "Keeps virtual device numbers.")
1616
b86c1cd8
MA
1617(defun tramp-default-file-modes (filename)
1618 "Return file modes of FILENAME as integer.
1619If the file modes of FILENAME cannot be determined, return the
974647ac
MA
1620value of `default-file-modes', without execute permissions."
1621 (or (file-modes filename)
0f34aa77 1622 (logand (default-file-modes) (tramp-compat-octal-to-decimal "0666"))))
b86c1cd8 1623
c23c3394
MA
1624(defun tramp-replace-environment-variables (filename)
1625 "Replace environment variables in FILENAME.
1626Return the string with the replaced variables."
2e271195 1627 (save-match-data
ec5145d6 1628 (let ((idx (string-match "$\\(\\w+\\)" filename)))
2e271195 1629 ;; `$' is coded as `$$'.
ec5145d6
MA
1630 (when (and idx
1631 (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
1632 (getenv (match-string 1 filename)))
2e271195
MA
1633 (setq filename
1634 (replace-match
1635 (substitute-in-file-name (match-string 0 filename))
1636 t nil filename)))
1637 filename)))
c23c3394 1638
00d6fd04
MA
1639;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
1640;; which calls corresponding functions (see minibuf.el).
1641(when (fboundp 'minibuffer-electric-separator)
9e6ab520 1642 (mapc
aa485f7c
MA
1643 (lambda (x)
1644 (eval
1645 `(defadvice ,x
1646 (around ,(intern (format "tramp-advice-%s" x)) activate)
1647 "Invoke `substitute-in-file-name' for Tramp files."
1648 (if (and (symbol-value 'minibuffer-electric-file-name-behavior)
1649 (tramp-tramp-file-p (buffer-substring)))
1650 ;; We don't need to handle `last-input-event', because
1651 ;; due to the key map we know it must be ?/ or ?~.
1652 (let ((s (concat (buffer-substring (point-min) (point))
1653 (string last-command-char))))
1654 (delete-region (point-min) (point))
1655 (insert (substitute-in-file-name s))
1656 (setq ad-return-value last-command-char))
d7ec1df7
MA
1657 ad-do-it)))
1658 (eval
1659 `(add-hook
1660 'tramp-unload-hook
1661 (lambda ()
1662 (ad-remove-advice ',x 'around ',(intern (format "tramp-advice-%s" x)))
1663 (ad-activate ',x)))))
00d6fd04
MA
1664
1665 '(minibuffer-electric-separator
1666 minibuffer-electric-tilde)))
1667
eb562962
MA
1668(defun tramp-find-file-name-coding-system-alist (filename tmpname)
1669 "Like `find-operation-coding-system' for Tramp filenames.
1670Tramp's `insert-file-contents' and `write-region' work over
1671temporary file names. If `file-coding-system-alist' contains an
1672expression, which matches more than the file name suffix, the
1673coding system might not be determined. This function repairs it."
1674 (let (result)
1675 (dolist (elt file-coding-system-alist result)
1676 (when (and (consp elt) (string-match (car elt) filename))
1677 ;; We found a matching entry in `file-coding-system-alist'.
1678 ;; So we add a similar entry, but with the temporary file name
1679 ;; as regexp.
1680 (add-to-list
1681 'result (cons (regexp-quote tmpname) (cdr elt)) 'append)))))
1682
a01b1e22
MA
1683;;;###autoload
1684(progn (defun tramp-run-real-handler (operation args)
fb7933a3 1685 "Invoke normal file name handler for OPERATION.
c62c9d08
KG
1686First arg specifies the OPERATION, second arg is a list of arguments to
1687pass to the OPERATION."
4007ba5b
KG
1688 (let* ((inhibit-file-name-handlers
1689 `(tramp-file-name-handler
946a5aeb 1690 tramp-vc-file-name-handler
4007ba5b
KG
1691 tramp-completion-file-name-handler
1692 cygwin-mount-name-hook-function
1693 cygwin-mount-map-drive-hook-function
1694 .
1695 ,(and (eq inhibit-file-name-operation operation)
1696 inhibit-file-name-handlers)))
1697 (inhibit-file-name-operation operation))
a01b1e22 1698 (apply operation args))))
16674e4f 1699
a01b1e22
MA
1700;;;###autoload
1701(progn (defun tramp-completion-run-real-handler (operation args)
16674e4f
KG
1702 "Invoke `tramp-file-name-handler' for OPERATION.
1703First arg specifies the OPERATION, second arg is a list of arguments to
1704pass to the OPERATION."
4007ba5b
KG
1705 (let* ((inhibit-file-name-handlers
1706 `(tramp-completion-file-name-handler
1707 cygwin-mount-name-hook-function
1708 cygwin-mount-map-drive-hook-function
1709 .
1710 ,(and (eq inhibit-file-name-operation operation)
1711 inhibit-file-name-handlers)))
1712 (inhibit-file-name-operation operation))
a01b1e22 1713 (apply operation args))))
fb7933a3 1714
4007ba5b
KG
1715;; We handle here all file primitives. Most of them have the file
1716;; name as first parameter; nevertheless we check for them explicitly
04bf5b65 1717;; in order to be signaled if a new primitive appears. This
4007ba5b
KG
1718;; scenario is needed because there isn't a way to decide by
1719;; syntactical means whether a foreign method must be called. It would
19a87064 1720;; ease the life if `file-name-handler-alist' would support a decision
4007ba5b
KG
1721;; function as well but regexp only.
1722(defun tramp-file-name-for-operation (operation &rest args)
1723 "Return file name related to OPERATION file primitive.
1724ARGS are the arguments OPERATION has been called with."
1725 (cond
b533bc97 1726 ;; FILE resp DIRECTORY.
4007ba5b
KG
1727 ((member operation
1728 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
1729 'delete-file 'diff-latest-backup-file 'directory-file-name
1730 'directory-files 'directory-files-and-attributes
1731 'dired-compress-file 'dired-uncache
1732 'file-accessible-directory-p 'file-attributes
1733 'file-directory-p 'file-executable-p 'file-exists-p
25f14cdb 1734 'file-local-copy 'file-remote-p 'file-modes
19a87064
MA
1735 'file-name-as-directory 'file-name-directory
1736 'file-name-nondirectory 'file-name-sans-versions
1737 'file-ownership-preserved-p 'file-readable-p
1738 'file-regular-p 'file-symlink-p 'file-truename
1739 'file-writable-p 'find-backup-file-name 'find-file-noselect
1740 'get-file-buffer 'insert-directory 'insert-file-contents
1741 'load 'make-directory 'make-directory-internal
1742 'set-file-modes 'substitute-in-file-name
1743 'unhandled-file-name-directory 'vc-registered
b533bc97 1744 ;; Emacs 22+ only.
ce3f516f 1745 'set-file-times
25f14cdb
MA
1746 ;; Emacs 24+ only.
1747 'file-selinux-context 'set-file-selinux-context
b533bc97 1748 ;; XEmacs only.
4007ba5b
KG
1749 'abbreviate-file-name 'create-file-buffer
1750 'dired-file-modtime 'dired-make-compressed-filename
1751 'dired-recursive-delete-directory 'dired-set-file-modtime
1752 'dired-shell-unhandle-file-name 'dired-uucode-file
5615d63f 1753 'insert-file-contents-literally 'make-temp-name 'recover-file
4007ba5b 1754 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
8daea7fc
KG
1755 (if (file-name-absolute-p (nth 0 args))
1756 (nth 0 args)
1757 (expand-file-name (nth 0 args))))
b533bc97 1758 ;; FILE DIRECTORY resp FILE1 FILE2.
4007ba5b
KG
1759 ((member operation
1760 (list 'add-name-to-file 'copy-file 'expand-file-name
1761 'file-name-all-completions 'file-name-completion
1762 'file-newer-than-file-p 'make-symbolic-link 'rename-file
b533bc97 1763 ;; Emacs 23+ only.
263c02ef 1764 'copy-directory
b533bc97 1765 ;; XEmacs only.
4007ba5b
KG
1766 'dired-make-relative-symlink
1767 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
1768 (save-match-data
1769 (cond
1770 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
1771 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
1772 (t (buffer-file-name (current-buffer))))))
b533bc97 1773 ;; START END FILE.
4007ba5b
KG
1774 ((eq operation 'write-region)
1775 (nth 2 args))
b533bc97 1776 ;; BUFFER.
4007ba5b 1777 ((member operation
00d6fd04 1778 (list 'set-visited-file-modtime 'verify-visited-file-modtime
b533bc97 1779 ;; Emacs 22+ only.
00d6fd04 1780 'make-auto-save-file-name
b533bc97 1781 ;; XEmacs only.
4007ba5b
KG
1782 'backup-buffer))
1783 (buffer-file-name
1784 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
b533bc97 1785 ;; COMMAND.
4007ba5b 1786 ((member operation
b533bc97 1787 (list ;; not in Emacs 23+.
00d6fd04 1788 'dired-call-process
b533bc97 1789 ;; Emacs only.
b71c9e75 1790 'shell-command
b533bc97 1791 ;; Emacs 22+ only.
0457dd55 1792 'process-file
b533bc97 1793 ;; Emacs 23+ only.
00d6fd04 1794 'start-file-process
b533bc97 1795 ;; XEmacs only.
00d6fd04 1796 'dired-print-file 'dired-shell-call-process
b533bc97 1797 ;; nowhere yet.
b81a0b56
MA
1798 'executable-find 'start-process
1799 'call-process 'call-process-region))
4007ba5b 1800 default-directory)
b533bc97 1801 ;; Unknown file primitive.
4007ba5b
KG
1802 (t (error "unknown file I/O primitive: %s" operation))))
1803
1804(defun tramp-find-foreign-file-name-handler (filename)
1805 "Return foreign file name handler if exists."
b533bc97 1806 (when (tramp-tramp-file-p filename)
9ce8462a
MA
1807 (let ((v (tramp-dissect-file-name filename t))
1808 (handler tramp-foreign-file-name-handler-alist)
1809 elt res)
1810 ;; When we are not fully sure that filename completion is safe,
1811 ;; we should not return a handler.
1812 (when (or (tramp-file-name-method v) (tramp-file-name-user v)
1834b39f
MA
1813 (and (tramp-file-name-host v)
1814 (not (member (tramp-file-name-host v)
1815 (mapcar 'car tramp-methods))))
9ce8462a
MA
1816 (not (tramp-completion-mode-p)))
1817 (while handler
1818 (setq elt (car handler)
1819 handler (cdr handler))
1820 (when (funcall (car elt) filename)
1821 (setq handler nil
1822 res (cdr elt))))
1823 res))))
4007ba5b 1824
fb7933a3
KG
1825;; Main function.
1826;;;###autoload
1827(defun tramp-file-name-handler (operation &rest args)
ea9d1443 1828 "Invoke Tramp file name handler.
a4aeb9a4 1829Falls back to normal file name handler if no Tramp file name handler exists."
2e271195
MA
1830 (if tramp-mode
1831 (save-match-data
1832 (let* ((filename
1833 (tramp-replace-environment-variables
1834 (apply 'tramp-file-name-for-operation operation args)))
1835 (completion (tramp-completion-mode-p))
1836 (foreign (tramp-find-foreign-file-name-handler filename)))
1837 (with-parsed-tramp-file-name filename nil
4874f5e6
MA
1838 ;; Call the backend function.
1839 (if foreign
1840 (condition-case err
1841 (apply foreign operation args)
af4b9ae5 1842
03c1ad43 1843 ;; Trace, that somebody has interrupted the operation.
af4b9ae5
MA
1844 (quit
1845 (let (tramp-message-show-message)
1846 (tramp-message
1847 v 1 "Interrupt received in operation %s"
1848 (append (list operation) args)))
1849 ;; Propagate the quit signal.
1850 (signal (car err) (cdr err)))
1851
1852 ;; When we are in completion mode, some failed
1853 ;; operations shall return at least a default value
1854 ;; in order to give the user a chance to correct the
1855 ;; file name in the minibuffer.
4874f5e6
MA
1856 (error
1857 (cond
4874f5e6
MA
1858 ((and completion (zerop (length localname))
1859 (memq operation '(file-exists-p file-directory-p)))
1860 t)
1861 ((and completion (zerop (length localname))
1862 (memq operation
1863 '(expand-file-name file-name-as-directory)))
1864 filename)
1865 ;; Propagate the error.
1866 (t (signal (car err) (cdr err))))))
af4b9ae5 1867
4874f5e6
MA
1868 ;; Nothing to do for us.
1869 (tramp-run-real-handler operation args)))))
1870
2e271195
MA
1871 ;; When `tramp-mode' is not enabled, we don't do anything.
1872 (tramp-run-real-handler operation args)))
fb7933a3 1873
07dfe738
KG
1874;; In Emacs, there is some concurrency due to timers. If a timer
1875;; interrupts Tramp and wishes to use the same connection buffer as
1876;; the "main" Emacs, then garbage might occur in the connection
1877;; buffer. Therefore, we need to make sure that a timer does not use
1878;; the same connection buffer as the "main" Emacs. We implement a
1879;; cheap global lock, instead of locking each connection buffer
1880;; separately. The global lock is based on two variables,
1881;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
1882;; (with setq) to indicate a lock. But Tramp also calls itself during
1883;; processing of a single file operation, so we need to allow
1884;; recursive calls. That's where the `tramp-locker' variable comes in
1885;; -- it is let-bound to t during the execution of the current
1886;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
1887;; then we should just proceed because we have been called
1888;; recursively. But if `tramp-locker' is nil, then we are a timer
1889;; interrupting the "main" Emacs, and then we signal an error.
1890
1891(defvar tramp-locked nil
1892 "If non-nil, then Tramp is currently busy.
1893Together with `tramp-locker', this implements a locking mechanism
1894preventing reentrant calls of Tramp.")
1895
1896(defvar tramp-locker nil
1897 "If non-nil, then a caller has locked Tramp.
1898Together with `tramp-locked', this implements a locking mechanism
1899preventing reentrant calls of Tramp.")
1900
16674e4f 1901;;;###autoload
1ecc6145 1902(progn (defun tramp-completion-file-name-handler (operation &rest args)
a4aeb9a4
MA
1903 "Invoke Tramp file name completion handler.
1904Falls back to normal file name handler if no Tramp file name handler exists."
57671b72
MA
1905 ;; We bind `directory-sep-char' here for XEmacs on Windows, which
1906 ;; would otherwise use backslash.
aff67808
MA
1907 (let ((directory-sep-char ?/)
1908 (fn (assoc operation tramp-completion-file-name-handler-alist)))
aa485f7c
MA
1909 (if (and
1910 ;; When `tramp-mode' is not enabled, we don't do anything.
1911 fn tramp-mode
1912 ;; For other syntaxes than `sep', the regexp matches many common
1913 ;; situations where the user doesn't actually want to use Tramp.
1914 ;; So to avoid autoloading Tramp after typing just "/s", we
1915 ;; disable this part of the completion, unless the user implicitly
1916 ;; indicated his interest in using a fancier completion system.
1917 (or (eq tramp-syntax 'sep)
a94d821f
MA
1918 (featurep 'tramp) ;; If it's loaded, we may as well use it.
1919 ;; `partial-completion-mode' does not exist in XEmacs.
1920 ;; It is obsoleted with Emacs 24.1.
0d5852cf
MA
1921 (and (boundp 'partial-completion-mode)
1922 (symbol-value 'partial-completion-mode))
aa485f7c
MA
1923 ;; FIXME: These may have been loaded even if the user never
1924 ;; intended to use them.
1925 (featurep 'ido)
1926 (featurep 'icicles)))
aff67808
MA
1927 (save-match-data (apply (cdr fn) args))
1928 (tramp-completion-run-real-handler operation args)))))
a01b1e22 1929
b25a52cc 1930;;;###autoload
aa485f7c
MA
1931(progn (defun tramp-register-file-name-handlers ()
1932 "Add Tramp file name handlers to `file-name-handler-alist'."
1933 ;; Remove autoloaded handlers from file name handler alist. Useful,
00d6fd04
MA
1934 ;; if `tramp-syntax' has been changed.
1935 (let ((a1 (rassq 'tramp-file-name-handler file-name-handler-alist)))
aa485f7c
MA
1936 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
1937 (let ((a1 (rassq
1938 'tramp-completion-file-name-handler file-name-handler-alist)))
1939 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
1940 ;; Add the handlers.
a01b1e22
MA
1941 (add-to-list 'file-name-handler-alist
1942 (cons tramp-file-name-regexp 'tramp-file-name-handler))
0c0b61f1 1943 (put 'tramp-file-name-handler 'safe-magic t)
aa485f7c
MA
1944 (add-to-list 'file-name-handler-alist
1945 (cons tramp-completion-file-name-regexp
1946 'tramp-completion-file-name-handler))
1947 (put 'tramp-completion-file-name-handler 'safe-magic t)
1948 ;; If jka-compr or epa-file are already loaded, move them to the
1949 ;; front of `file-name-handler-alist'.
1950 (dolist (fnh '(epa-file-handler jka-compr-handler))
1951 (let ((entry (rassoc fnh file-name-handler-alist)))
1952 (when entry
1953 (setq file-name-handler-alist
1954 (cons entry (delete entry file-name-handler-alist))))))))
69cee873 1955
00d6fd04
MA
1956;; `tramp-file-name-handler' must be registered before evaluation of
1957;; site-start and init files, because there might exist remote files
1958;; already, f.e. files kept via recentf-mode.
4a93e698 1959;;;###autoload
aa485f7c 1960(tramp-register-file-name-handlers)
b25a52cc 1961
4a93e698
MA
1962(defun tramp-exists-file-name-handler (operation &rest args)
1963 "Check, whether OPERATION runs a file name handler."
1964 ;; The file name handler is determined on base of either an
1965 ;; argument, `buffer-file-name', or `default-directory'.
1966 (ignore-errors
1967 (let* ((buffer-file-name "/")
1968 (default-directory "/")
1969 (fnha file-name-handler-alist)
1970 (check-file-name-operation operation)
1971 (file-name-handler-alist
1972 (list
1973 (cons "/"
1974 (lambda (operation &rest args)
1975 "Returns OPERATION if it is the one to be checked."
1976 (if (equal check-file-name-operation operation)
1977 operation
1978 (let ((file-name-handler-alist fnha))
1979 (apply operation args))))))))
1980 (equal (apply operation args) operation))))
1981
fb7933a3 1982;;;###autoload
8c04e197 1983(defun tramp-unload-file-name-handlers ()
a69c01a0
MA
1984 (setq file-name-handler-alist
1985 (delete (rassoc 'tramp-file-name-handler
1986 file-name-handler-alist)
1987 (delete (rassoc 'tramp-completion-file-name-handler
1988 file-name-handler-alist)
1989 file-name-handler-alist))))
1990
8c04e197 1991(add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
a69c01a0 1992
0664ff72 1993;;; File name handler functions for completion mode:
a6e96327
MA
1994
1995(defvar tramp-completion-mode nil
1996 "If non-nil, external packages signal that they are in file name completion.
1997
1998This is necessary, because Tramp uses a heuristic depending on last
1999input event. This fails when external packages use other characters
2000but <TAB>, <SPACE> or ?\\? for file name completion. This variable
2001should never be set globally, the intention is to let-bind it.")
16674e4f
KG
2002
2003;; Necessary because `tramp-file-name-regexp-unified' and
00d6fd04
MA
2004;; `tramp-completion-file-name-regexp-unified' aren't different. If
2005;; nil, `tramp-completion-run-real-handler' is called (i.e. forwarding
2006;; to `tramp-file-name-handler'). Otherwise, it takes
2007;; `tramp-run-real-handler'. Using `last-input-event' is a little bit
2008;; risky, because completing a file might require loading other files,
2009;; like "~/.netrc", and for them it shouldn't be decided based on that
2010;; variable. On the other hand, those files shouldn't have partial
a4aeb9a4
MA
2011;; Tramp file name syntax. Maybe another variable should be introduced
2012;; overwriting this check in such cases. Or we change Tramp file name
00d6fd04 2013;; syntax in order to avoid ambiguities, like in XEmacs ...
0f34aa77 2014;;;###tramp-autoload
6c4e47fa 2015(defun tramp-completion-mode-p ()
a94d821f 2016 "Check, whether method / user name / host name completion is active."
6c4e47fa 2017 (or
5f2b693f
MA
2018 ;; Signal from outside. `non-essential' has been introduced in Emacs 24.
2019 (and (boundp 'non-essential) (symbol-value 'non-essential))
a6e96327
MA
2020 tramp-completion-mode
2021 ;; Emacs.
94be87e8 2022 (equal last-input-event 'tab)
6c4e47fa 2023 (and (natnump last-input-event)
94be87e8 2024 (or
a6e96327 2025 ;; ?\t has event-modifier 'control.
800a97b8 2026 (equal last-input-event ?\t)
94be87e8 2027 (and (not (event-modifiers last-input-event))
800a97b8
SM
2028 (or (equal last-input-event ?\?)
2029 (equal last-input-event ?\ )))))
a6e96327 2030 ;; XEmacs.
6c4e47fa
MA
2031 (and (featurep 'xemacs)
2032 ;; `last-input-event' might be nil.
2033 (not (null last-input-event))
2034 ;; `last-input-event' may have no character approximation.
0d5852cf 2035 (tramp-compat-funcall 'event-to-character last-input-event)
94be87e8 2036 (or
a6e96327 2037 ;; ?\t has event-modifier 'control.
800a97b8 2038 (equal
0d5852cf 2039 (tramp-compat-funcall 'event-to-character last-input-event) ?\t)
94be87e8 2040 (and (not (event-modifiers last-input-event))
800a97b8 2041 (or (equal
0d5852cf
MA
2042 (tramp-compat-funcall 'event-to-character last-input-event)
2043 ?\?)
800a97b8 2044 (equal
0d5852cf
MA
2045 (tramp-compat-funcall 'event-to-character last-input-event)
2046 ?\ )))))))
16674e4f 2047
acd1f317
MA
2048(defun tramp-connectable-p (filename)
2049 "Check, whether it is possible to connect the remote host w/o side-effects.
2050This is true, if either the remote host is already connected, or if we are
2051not in completion mode."
2052 (and (tramp-tramp-file-p filename)
2053 (with-parsed-tramp-file-name filename nil
2054 (or (get-buffer (tramp-buffer-name v))
2055 (not (tramp-completion-mode-p))))))
2056
16674e4f
KG
2057;; Method, host name and user name completion.
2058;; `tramp-completion-dissect-file-name' returns a list of
2059;; tramp-file-name structures. For all of them we return possible completions.
a01b1e22 2060;;;###autoload
16674e4f 2061(defun tramp-completion-handle-file-name-all-completions (filename directory)
00d6fd04 2062 "Like `file-name-all-completions' for partial Tramp files."
16674e4f 2063
00d6fd04
MA
2064 (let* ((fullname (tramp-drop-volume-letter
2065 (expand-file-name filename directory)))
2066 ;; Possible completion structures.
2067 (v (tramp-completion-dissect-file-name fullname))
2068 result result1)
2069
2070 (while v
2071 (let* ((car (car v))
2072 (method (tramp-file-name-method car))
2073 (user (tramp-file-name-user car))
2074 (host (tramp-file-name-host car))
2075 (localname (tramp-file-name-localname car))
2076 (m (tramp-find-method method user host))
2077 (tramp-current-user user) ; see `tramp-parse-passwd'
2078 all-user-hosts)
2079
2080 (unless localname ;; Nothing to complete.
2081
2082 (if (or user host)
2083
2084 ;; Method dependent user / host combinations.
2085 (progn
9e6ab520 2086 (mapc
00d6fd04
MA
2087 (lambda (x)
2088 (setq all-user-hosts
2089 (append all-user-hosts
2090 (funcall (nth 0 x) (nth 1 x)))))
2091 (tramp-get-completion-function m))
2092
9e6ab520
MA
2093 (setq result
2094 (append result
2095 (mapcar
2096 (lambda (x)
2097 (tramp-get-completion-user-host
2098 method user host (nth 0 x) (nth 1 x)))
2099 (delq nil all-user-hosts)))))
00d6fd04
MA
2100
2101 ;; Possible methods.
2102 (setq result
2103 (append result (tramp-get-completion-methods m)))))
2104
2105 (setq v (cdr v))))
2106
2107 ;; Unify list, remove nil elements.
2108 (while result
2109 (let ((car (car result)))
2110 (when car
2111 (add-to-list
2112 'result1
2113 (substring car (length (tramp-drop-volume-letter directory)))))
2114 (setq result (cdr result))))
2115
2116 ;; Complete local parts.
2117 (append
2118 result1
03c1ad43
MA
2119 (ignore-errors
2120 (apply (if (tramp-connectable-p fullname)
2121 'tramp-completion-run-real-handler
2122 'tramp-run-real-handler)
2123 'file-name-all-completions (list (list filename directory)))))))
16674e4f
KG
2124
2125;; Method, host name and user name completion for a file.
a01b1e22 2126;;;###autoload
e1e17cae
MA
2127(defun tramp-completion-handle-file-name-completion
2128 (filename directory &optional predicate)
00d6fd04 2129 "Like `file-name-completion' for Tramp files."
e1e17cae
MA
2130 (try-completion
2131 filename
2132 (mapcar 'list (file-name-all-completions filename directory))
acd1f317
MA
2133 (when (and predicate
2134 (tramp-connectable-p (expand-file-name filename directory)))
83e20b5c 2135 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
16674e4f
KG
2136
2137;; I misuse a little bit the tramp-file-name structure in order to handle
2138;; completion possibilities for partial methods / user names / host names.
2139;; Return value is a list of tramp-file-name structures according to possible
00d6fd04 2140;; completions. If "localname" is non-nil it means there
16674e4f
KG
2141;; shouldn't be a completion anymore.
2142
2143;; Expected results:
2144
00d6fd04
MA
2145;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
2146;; [nil nil "x" nil] [nil "x" nil nil] [nil "x" "y" nil]
2147;; [nil "x" nil nil]
2148;; ["x" nil nil nil]
2149
2150;; "/x:" "/x:y" "/x:y:"
2151;; [nil nil "x" ""] [nil nil "x" "y"] ["x" nil "y" ""]
2152;; "/[x/" "/[x/y"
2153;; ["x" nil "" nil] ["x" nil "y" nil]
2154;; ["x" "" nil nil] ["x" "y" nil nil]
2155
2156;; "/x:y@" "/x:y@z" "/x:y@z:"
2157;; [nil nil "x" "y@"] [nil nil "x" "y@z"] ["x" "y" "z" ""]
2158;; "/[x/y@" "/[x/y@z"
2159;; ["x" nil "y" nil] ["x" "y" "z" nil]
16674e4f
KG
2160(defun tramp-completion-dissect-file-name (name)
2161 "Returns a list of `tramp-file-name' structures.
2162They are collected by `tramp-completion-dissect-file-name1'."
2163
2164 (let* ((result)
4007ba5b 2165 (x-nil "\\|\\(\\)")
b96e6899
MA
2166 (tramp-completion-ipv6-regexp
2167 (format
2168 "[^%s]*"
2169 (if (zerop (length tramp-postfix-ipv6-format))
2170 tramp-postfix-host-format
2171 tramp-postfix-ipv6-format)))
4007ba5b
KG
2172 ;; "/method" "/[method"
2173 (tramp-completion-file-name-structure1
2174 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
2175 1 nil nil nil))
2176 ;; "/user" "/[user"
2177 (tramp-completion-file-name-structure2
2178 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
2179 nil 1 nil nil))
2180 ;; "/host" "/[host"
2181 (tramp-completion-file-name-structure3
2182 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
2183 nil nil 1 nil))
b96e6899 2184 ;; "/[ipv6" "/[ipv6"
4007ba5b 2185 (tramp-completion-file-name-structure4
b96e6899
MA
2186 (list (concat tramp-prefix-regexp
2187 tramp-prefix-ipv6-regexp
2188 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
2189 nil nil 1 nil))
2190 ;; "/user@host" "/[user@host"
2191 (tramp-completion-file-name-structure5
4007ba5b
KG
2192 (list (concat tramp-prefix-regexp
2193 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
2194 "\\(" tramp-host-regexp x-nil "\\)$")
2195 nil 1 2 nil))
b96e6899
MA
2196 ;; "/user@[ipv6" "/[user@ipv6"
2197 (tramp-completion-file-name-structure6
2198 (list (concat tramp-prefix-regexp
2199 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
2200 tramp-prefix-ipv6-regexp
2201 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
2202 nil 1 2 nil))
00d6fd04 2203 ;; "/method:user" "/[method/user" "/method://user"
b96e6899 2204 (tramp-completion-file-name-structure7
4007ba5b 2205 (list (concat tramp-prefix-regexp
00d6fd04 2206 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
4007ba5b
KG
2207 "\\(" tramp-user-regexp x-nil "\\)$")
2208 1 2 nil nil))
00d6fd04 2209 ;; "/method:host" "/[method/host" "/method://host"
b96e6899 2210 (tramp-completion-file-name-structure8
4007ba5b 2211 (list (concat tramp-prefix-regexp
00d6fd04 2212 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
4007ba5b
KG
2213 "\\(" tramp-host-regexp x-nil "\\)$")
2214 1 nil 2 nil))
b96e6899
MA
2215 ;; "/method:[ipv6" "/[method/ipv6" "/method://[ipv6"
2216 (tramp-completion-file-name-structure9
2217 (list (concat tramp-prefix-regexp
2218 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2219 tramp-prefix-ipv6-regexp
2220 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
2221 1 nil 2 nil))
00d6fd04 2222 ;; "/method:user@host" "/[method/user@host" "/method://user@host"
b96e6899 2223 (tramp-completion-file-name-structure10
4007ba5b 2224 (list (concat tramp-prefix-regexp
00d6fd04 2225 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
4007ba5b
KG
2226 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
2227 "\\(" tramp-host-regexp x-nil "\\)$")
00d6fd04 2228 1 2 3 nil))
b96e6899
MA
2229 ;; "/method:user@[ipv6" "/[method/user@ipv6" "/method://user@[ipv6"
2230 (tramp-completion-file-name-structure11
2231 (list (concat tramp-prefix-regexp
2232 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2233 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
2234 tramp-prefix-ipv6-regexp
2235 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
2236 1 2 3 nil))
00d6fd04 2237 ;; "/method: "/method:/"
b96e6899 2238 (tramp-completion-file-name-structure12
00d6fd04
MA
2239 (list
2240 (if (equal tramp-syntax 'url)
2241 (concat tramp-prefix-regexp
2242 "\\(" tramp-method-regexp "\\)"
2243 "\\(" (substring tramp-postfix-method-regexp 0 1)
2244 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
2245 "\\(" "\\)$")
2246 ;; Should not match if not URL syntax.
2247 (concat tramp-prefix-regexp "/$"))
2248 1 3 nil nil))
2249 ;; "/method: "/method:/"
b96e6899 2250 (tramp-completion-file-name-structure13
00d6fd04
MA
2251 (list
2252 (if (equal tramp-syntax 'url)
2253 (concat tramp-prefix-regexp
2254 "\\(" tramp-method-regexp "\\)"
2255 "\\(" (substring tramp-postfix-method-regexp 0 1)
2256 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
2257 "\\(" "\\)$")
2258 ;; Should not match if not URL syntax.
2259 (concat tramp-prefix-regexp "/$"))
2260 1 nil 3 nil)))
4007ba5b 2261
9e6ab520 2262 (mapc (lambda (regexp)
16674e4f
KG
2263 (add-to-list 'result
2264 (tramp-completion-dissect-file-name1 regexp name)))
2265 (list
2266 tramp-completion-file-name-structure1
2267 tramp-completion-file-name-structure2
2268 tramp-completion-file-name-structure3
2269 tramp-completion-file-name-structure4
2270 tramp-completion-file-name-structure5
2271 tramp-completion-file-name-structure6
2272 tramp-completion-file-name-structure7
00d6fd04
MA
2273 tramp-completion-file-name-structure8
2274 tramp-completion-file-name-structure9
b96e6899
MA
2275 tramp-completion-file-name-structure10
2276 tramp-completion-file-name-structure11
2277 tramp-completion-file-name-structure12
2278 tramp-completion-file-name-structure13
16674e4f
KG
2279 tramp-file-name-structure))
2280
2281 (delq nil result)))
2282
2283(defun tramp-completion-dissect-file-name1 (structure name)
2284 "Returns a `tramp-file-name' structure matching STRUCTURE.
00d6fd04 2285The structure consists of remote method, remote user,
7432277c 2286remote host and localname (filename on remote host)."
fb7933a3 2287
00d6fd04
MA
2288 (save-match-data
2289 (when (string-match (nth 0 structure) name)
2290 (let ((method (and (nth 1 structure)
2291 (match-string (nth 1 structure) name)))
2292 (user (and (nth 2 structure)
2293 (match-string (nth 2 structure) name)))
2294 (host (and (nth 3 structure)
2295 (match-string (nth 3 structure) name)))
2296 (localname (and (nth 4 structure)
2297 (match-string (nth 4 structure) name))))
2298 (vector method user host localname)))))
16674e4f
KG
2299
2300;; This function returns all possible method completions, adding the
2301;; trailing method delimeter.
16674e4f
KG
2302(defun tramp-get-completion-methods (partial-method)
2303 "Returns all method completions for PARTIAL-METHOD."
4007ba5b
KG
2304 (mapcar
2305 (lambda (method)
2306 (and method
2307 (string-match (concat "^" (regexp-quote partial-method)) method)
00d6fd04
MA
2308 (tramp-completion-make-tramp-file-name method nil nil nil)))
2309 (mapcar 'car tramp-methods)))
16674e4f
KG
2310
2311;; Compares partial user and host names with possible completions.
2312(defun tramp-get-completion-user-host (method partial-user partial-host user host)
2313 "Returns the most expanded string for user and host name completion.
2314PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
2315 (cond
2316
2317 ((and partial-user partial-host)
2318 (if (and host
2319 (string-match (concat "^" (regexp-quote partial-host)) host)
2320 (string-equal partial-user (or user partial-user)))
2321 (setq user partial-user)
2322 (setq user nil
2323 host nil)))
2324
2325 (partial-user
2326 (setq host nil)
2327 (unless
2328 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
2329 (setq user nil)))
2330
2331 (partial-host
2332 (setq user nil)
2333 (unless
2334 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
2335 (setq host nil)))
2336
2337 (t (setq user nil
2338 host nil)))
2339
292ffc15 2340 (unless (zerop (+ (length user) (length host)))
00d6fd04 2341 (tramp-completion-make-tramp-file-name method user host nil)))
16674e4f
KG
2342
2343(defun tramp-parse-rhosts (filename)
2344 "Return a list of (user host) tuples allowed to access.
292ffc15 2345Either user or host may be nil."
00d6fd04
MA
2346 ;; On Windows, there are problems in completion when
2347 ;; `default-directory' is remote.
9e6ab520 2348 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 2349 res)
8daea7fc 2350 (when (file-readable-p filename)
16674e4f
KG
2351 (with-temp-buffer
2352 (insert-file-contents filename)
2353 (goto-char (point-min))
2354 (while (not (eobp))
292ffc15 2355 (push (tramp-parse-rhosts-group) res))))
16674e4f
KG
2356 res))
2357
16674e4f
KG
2358(defun tramp-parse-rhosts-group ()
2359 "Return a (user host) tuple allowed to access.
292ffc15 2360Either user or host may be nil."
16674e4f
KG
2361 (let ((result)
2362 (regexp
2363 (concat
2364 "^\\(" tramp-host-regexp "\\)"
2365 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
6e060cee 2366 (narrow-to-region (point) (point-at-eol))
16674e4f
KG
2367 (when (re-search-forward regexp nil t)
2368 (setq result (append (list (match-string 3) (match-string 1)))))
2369 (widen)
2370 (forward-line 1)
2371 result))
2372
2373(defun tramp-parse-shosts (filename)
2374 "Return a list of (user host) tuples allowed to access.
2375User is always nil."
00d6fd04
MA
2376 ;; On Windows, there are problems in completion when
2377 ;; `default-directory' is remote.
9e6ab520 2378 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 2379 res)
8daea7fc 2380 (when (file-readable-p filename)
16674e4f
KG
2381 (with-temp-buffer
2382 (insert-file-contents filename)
2383 (goto-char (point-min))
2384 (while (not (eobp))
292ffc15 2385 (push (tramp-parse-shosts-group) res))))
16674e4f
KG
2386 res))
2387
2388(defun tramp-parse-shosts-group ()
2389 "Return a (user host) tuple allowed to access.
2390User is always nil."
16674e4f
KG
2391 (let ((result)
2392 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
6e060cee 2393 (narrow-to-region (point) (point-at-eol))
16674e4f
KG
2394 (when (re-search-forward regexp nil t)
2395 (setq result (list nil (match-string 1))))
2396 (widen)
2397 (or
2398 (> (skip-chars-forward ",") 0)
2399 (forward-line 1))
2400 result))
2401
8daea7fc
KG
2402(defun tramp-parse-sconfig (filename)
2403 "Return a list of (user host) tuples allowed to access.
2404User is always nil."
00d6fd04
MA
2405 ;; On Windows, there are problems in completion when
2406 ;; `default-directory' is remote.
9e6ab520 2407 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 2408 res)
8daea7fc
KG
2409 (when (file-readable-p filename)
2410 (with-temp-buffer
2411 (insert-file-contents filename)
2412 (goto-char (point-min))
2413 (while (not (eobp))
2414 (push (tramp-parse-sconfig-group) res))))
2415 res))
2416
2417(defun tramp-parse-sconfig-group ()
2418 "Return a (user host) tuple allowed to access.
2419User is always nil."
8daea7fc
KG
2420 (let ((result)
2421 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
6e060cee 2422 (narrow-to-region (point) (point-at-eol))
8daea7fc
KG
2423 (when (re-search-forward regexp nil t)
2424 (setq result (list nil (match-string 1))))
2425 (widen)
2426 (or
2427 (> (skip-chars-forward ",") 0)
2428 (forward-line 1))
2429 result))
2430
5ec2cc41
KG
2431(defun tramp-parse-shostkeys (dirname)
2432 "Return a list of (user host) tuples allowed to access.
2433User is always nil."
00d6fd04
MA
2434 ;; On Windows, there are problems in completion when
2435 ;; `default-directory' is remote.
9e6ab520 2436 (let* ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04
MA
2437 (regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
2438 (files (when (file-directory-p dirname) (directory-files dirname)))
2439 result)
5ec2cc41
KG
2440 (while files
2441 (when (string-match regexp (car files))
2442 (push (list nil (match-string 1 (car files))) result))
2443 (setq files (cdr files)))
2444 result))
2445
2446(defun tramp-parse-sknownhosts (dirname)
2447 "Return a list of (user host) tuples allowed to access.
2448User is always nil."
00d6fd04
MA
2449 ;; On Windows, there are problems in completion when
2450 ;; `default-directory' is remote.
9e6ab520 2451 (let* ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04
MA
2452 (regexp (concat "^\\(" tramp-host-regexp
2453 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
2454 (files (when (file-directory-p dirname) (directory-files dirname)))
2455 result)
5ec2cc41
KG
2456 (while files
2457 (when (string-match regexp (car files))
2458 (push (list nil (match-string 1 (car files))) result))
2459 (setq files (cdr files)))
2460 result))
2461
16674e4f
KG
2462(defun tramp-parse-hosts (filename)
2463 "Return a list of (user host) tuples allowed to access.
2464User is always nil."
00d6fd04
MA
2465 ;; On Windows, there are problems in completion when
2466 ;; `default-directory' is remote.
9e6ab520 2467 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 2468 res)
8daea7fc 2469 (when (file-readable-p filename)
16674e4f
KG
2470 (with-temp-buffer
2471 (insert-file-contents filename)
2472 (goto-char (point-min))
2473 (while (not (eobp))
292ffc15 2474 (push (tramp-parse-hosts-group) res))))
16674e4f
KG
2475 res))
2476
2477(defun tramp-parse-hosts-group ()
2478 "Return a (user host) tuple allowed to access.
2479User is always nil."
16674e4f 2480 (let ((result)
b96e6899
MA
2481 (regexp
2482 (concat "^\\(" tramp-ipv6-regexp "\\|" tramp-host-regexp "\\)")))
6e060cee 2483 (narrow-to-region (point) (point-at-eol))
16674e4f 2484 (when (re-search-forward regexp nil t)
b96e6899 2485 (setq result (list nil (match-string 1))))
16674e4f
KG
2486 (widen)
2487 (or
2488 (> (skip-chars-forward " \t") 0)
2489 (forward-line 1))
2490 result))
2491
8daea7fc
KG
2492;; For su-alike methods it would be desirable to return "root@localhost"
2493;; as default. Unfortunately, we have no information whether any user name
00d6fd04 2494;; has been typed already. So we use `tramp-current-user' as indication,
8daea7fc 2495;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
16674e4f
KG
2496(defun tramp-parse-passwd (filename)
2497 "Return a list of (user host) tuples allowed to access.
2498Host is always \"localhost\"."
00d6fd04
MA
2499 ;; On Windows, there are problems in completion when
2500 ;; `default-directory' is remote.
9e6ab520 2501 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 2502 res)
8daea7fc 2503 (if (zerop (length tramp-current-user))
16674e4f 2504 '(("root" nil))
8daea7fc 2505 (when (file-readable-p filename)
16674e4f
KG
2506 (with-temp-buffer
2507 (insert-file-contents filename)
2508 (goto-char (point-min))
2509 (while (not (eobp))
292ffc15 2510 (push (tramp-parse-passwd-group) res))))
16674e4f
KG
2511 res)))
2512
2513(defun tramp-parse-passwd-group ()
2514 "Return a (user host) tuple allowed to access.
292ffc15 2515Host is always \"localhost\"."
16674e4f
KG
2516 (let ((result)
2517 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
6e060cee 2518 (narrow-to-region (point) (point-at-eol))
16674e4f
KG
2519 (when (re-search-forward regexp nil t)
2520 (setq result (list (match-string 1) "localhost")))
2521 (widen)
2522 (forward-line 1)
2523 result))
2524
292ffc15
KG
2525(defun tramp-parse-netrc (filename)
2526 "Return a list of (user host) tuples allowed to access.
2527User may be nil."
00d6fd04
MA
2528 ;; On Windows, there are problems in completion when
2529 ;; `default-directory' is remote.
9e6ab520 2530 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 2531 res)
8daea7fc 2532 (when (file-readable-p filename)
292ffc15
KG
2533 (with-temp-buffer
2534 (insert-file-contents filename)
2535 (goto-char (point-min))
2536 (while (not (eobp))
2537 (push (tramp-parse-netrc-group) res))))
2538 res))
2539
2540(defun tramp-parse-netrc-group ()
2541 "Return a (user host) tuple allowed to access.
2542User may be nil."
292ffc15
KG
2543 (let ((result)
2544 (regexp
2545 (concat
2546 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
2547 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
6e060cee 2548 (narrow-to-region (point) (point-at-eol))
292ffc15
KG
2549 (when (re-search-forward regexp nil t)
2550 (setq result (list (match-string 3) (match-string 1))))
2551 (widen)
2552 (forward-line 1)
2553 result))
2554
00d6fd04
MA
2555(defun tramp-parse-putty (registry)
2556 "Return a list of (user host) tuples allowed to access.
2557User is always nil."
2558 ;; On Windows, there are problems in completion when
2559 ;; `default-directory' is remote.
9e6ab520 2560 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04
MA
2561 res)
2562 (with-temp-buffer
0f34aa77 2563 (when (zerop (tramp-compat-call-process "reg" nil t nil "query" registry))
00d6fd04
MA
2564 (goto-char (point-min))
2565 (while (not (eobp))
2566 (push (tramp-parse-putty-group registry) res))))
2567 res))
2568
2569(defun tramp-parse-putty-group (registry)
2570 "Return a (user host) tuple allowed to access.
2571User is always nil."
2572 (let ((result)
2573 (regexp (concat (regexp-quote registry) "\\\\\\(.+\\)")))
6e060cee 2574 (narrow-to-region (point) (point-at-eol))
00d6fd04
MA
2575 (when (re-search-forward regexp nil t)
2576 (setq result (list nil (match-string 1))))
2577 (widen)
2578 (forward-line 1)
2579 result))
2580
4a93e698 2581;;; Common file name handler functions for different backends:
b88f2d0a 2582
4a93e698
MA
2583(defvar tramp-handle-file-local-copy-hook nil
2584 "Normal hook to be run at the end of `tramp-*-handle-file-local-copy'.")
2585
2586(defvar tramp-handle-write-region-hook nil
2587 "Normal hook to be run at the end of `tramp-*-handle-write-region'.")
2588
2589(defun tramp-handle-directory-file-name (directory)
2590 "Like `directory-file-name' for Tramp files."
2591 ;; If localname component of filename is "/", leave it unchanged.
2592 ;; Otherwise, remove any trailing slash from localname component.
2593 ;; Method, host, etc, are unchanged. Does it make sense to try
2594 ;; to avoid parsing the filename?
2595 (with-parsed-tramp-file-name directory nil
2596 (if (and (not (zerop (length localname)))
2597 (eq (aref localname (1- (length localname))) ?/)
2598 (not (string= localname "/")))
2599 (substring directory 0 -1)
2600 directory)))
2601
2602(defun tramp-handle-directory-files
2603 (directory &optional full match nosort files-only)
2604 "Like `directory-files' for Tramp files."
2605 ;; FILES-ONLY is valid for XEmacs only.
2606 (when (file-directory-p directory)
2607 (setq directory (file-name-as-directory (expand-file-name directory)))
2608 (let ((temp (nreverse (file-name-all-completions "" directory)))
2609 result item)
2610
2611 (while temp
2612 (setq item (directory-file-name (pop temp)))
2613 (when (and (or (null match) (string-match match item))
2614 (or (null files-only)
2615 ;; Files only.
2616 (and (equal files-only t) (file-regular-p item))
2617 ;; Directories only.
2618 (file-directory-p item)))
2619 (push (if full (concat directory item) item)
2620 result)))
2621 (if nosort result (sort result 'string<)))))
2622
bd8fadca
MA
2623(defun tramp-handle-directory-files-and-attributes
2624 (directory &optional full match nosort id-format)
2625 "Like `directory-files-and-attributes' for Tramp files."
2626 (mapcar
2627 (lambda (x)
2628 (cons x (tramp-compat-file-attributes
2629 (if full x (expand-file-name x directory)) id-format)))
2630 (directory-files directory full match nosort)))
2631
4a93e698
MA
2632(defun tramp-handle-dired-uncache (dir &optional dir-p)
2633 "Like `dired-uncache' for Tramp files."
2634 ;; DIR-P is valid for XEmacs only.
2635 (with-parsed-tramp-file-name
2636 (if (or dir-p (file-directory-p dir)) dir (file-name-directory dir)) nil
2637 (tramp-flush-directory-property v localname)))
2638
bd8fadca
MA
2639(defun tramp-handle-file-exists-p (filename)
2640 "Like `file-exists-p' for Tramp files."
2641 (not (null (file-attributes filename))))
2642
4a93e698
MA
2643(defun tramp-handle-file-modes (filename)
2644 "Like `file-modes' for Tramp files."
2645 (let ((truename (or (file-truename filename) filename)))
2646 (when (file-exists-p truename)
2647 (tramp-mode-string-to-int (nth 8 (file-attributes truename))))))
2648
2649;; Localname manipulation functions that grok Tramp localnames...
2650(defun tramp-handle-file-name-as-directory (file)
2651 "Like `file-name-as-directory' but aware of Tramp files."
2652 ;; `file-name-as-directory' would be sufficient except localname is
2653 ;; the empty string.
2654 (let ((v (tramp-dissect-file-name file t)))
2655 ;; Run the command on the localname portion only.
2656 (tramp-make-tramp-file-name
2657 (tramp-file-name-method v)
2658 (tramp-file-name-user v)
2659 (tramp-file-name-host v)
2660 (tramp-run-real-handler
2661 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))))
2662
2663(defun tramp-handle-file-name-completion
2664 (filename directory &optional predicate)
2665 "Like `file-name-completion' for Tramp files."
2666 (unless (tramp-tramp-file-p directory)
2667 (error
2668 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2669 directory))
2670 (try-completion
2671 filename
2672 (mapcar 'list (file-name-all-completions filename directory))
2673 (when predicate
2674 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
2675
2676(defun tramp-handle-file-name-directory (file)
2677 "Like `file-name-directory' but aware of Tramp files."
2678 ;; Everything except the last filename thing is the directory. We
2679 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2680 ;; the remote file name parts. This is a problem when we are in
2681 ;; file name completion.
2682 (let ((v (tramp-dissect-file-name file t)))
2683 ;; Run the command on the localname portion only.
2684 (tramp-make-tramp-file-name
2685 (tramp-file-name-method v)
2686 (tramp-file-name-user v)
2687 (tramp-file-name-host v)
2688 (tramp-run-real-handler
2689 'file-name-directory (list (or (tramp-file-name-localname v) ""))))))
2690
2691(defun tramp-handle-file-name-nondirectory (file)
2692 "Like `file-name-nondirectory' but aware of Tramp files."
2693 (with-parsed-tramp-file-name file nil
2694 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
2695
bd8fadca
MA
2696(defun tramp-handle-file-newer-than-file-p (file1 file2)
2697 "Like `file-newer-than-file-p' for Tramp files."
2698 (cond
2699 ((not (file-exists-p file1)) nil)
2700 ((not (file-exists-p file2)) t)
2701 (t (tramp-time-less-p (nth 5 (file-attributes file2))
2702 (nth 5 (file-attributes file1))))))
2703
4a93e698
MA
2704(defun tramp-handle-file-regular-p (filename)
2705 "Like `file-regular-p' for Tramp files."
2706 (and (file-exists-p filename)
2707 (eq ?- (aref (nth 8 (file-attributes filename)) 0))))
2708
2709(defun tramp-handle-file-remote-p (filename &optional identification connected)
2710 "Like `file-remote-p' for Tramp files."
2711 (let ((tramp-verbose 3))
2712 (when (tramp-tramp-file-p filename)
2713 (let* ((v (tramp-dissect-file-name filename))
2714 (p (tramp-get-connection-process v))
2715 (c (and p (processp p) (memq (process-status p) '(run open)))))
2716 ;; We expand the file name only, if there is already a connection.
2717 (with-parsed-tramp-file-name
2718 (if c (expand-file-name filename) filename) nil
2719 (and (or (not connected) c)
2720 (cond
2721 ((eq identification 'method) method)
2722 ((eq identification 'user) user)
2723 ((eq identification 'host) host)
2724 ((eq identification 'localname) localname)
2725 (t (tramp-make-tramp-file-name method user host "")))))))))
2726
2727(defun tramp-handle-file-symlink-p (filename)
2728 "Like `file-symlink-p' for Tramp files."
2729 (with-parsed-tramp-file-name filename nil
2730 (let ((x (car (file-attributes filename))))
2731 (when (stringp x)
2732 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2733 ;; might do weird things.
2734 (if (file-name-absolute-p x)
2735 (tramp-make-tramp-file-name method user host x)
2736 x)))))
2737
2738(defun tramp-handle-find-backup-file-name (filename)
2739 "Like `find-backup-file-name' for Tramp files."
2740 (with-parsed-tramp-file-name filename nil
2741 ;; We set both variables. It doesn't matter whether it is
2742 ;; Emacs or XEmacs.
2743 (let ((backup-directory-alist
2744 ;; Emacs case.
2745 (when (boundp 'backup-directory-alist)
2746 (if (symbol-value 'tramp-backup-directory-alist)
2747 (mapcar
2748 (lambda (x)
2749 (cons
2750 (car x)
2751 (if (and (stringp (cdr x))
2752 (file-name-absolute-p (cdr x))
2753 (not (tramp-file-name-p (cdr x))))
2754 (tramp-make-tramp-file-name method user host (cdr x))
2755 (cdr x))))
2756 (symbol-value 'tramp-backup-directory-alist))
2757 (symbol-value 'backup-directory-alist))))
2758
2759 (bkup-backup-directory-info
2760 ;; XEmacs case.
2761 (when (boundp 'bkup-backup-directory-info)
2762 (if (symbol-value 'tramp-bkup-backup-directory-info)
2763 (mapcar
2764 (lambda (x)
2765 (nconc
2766 (list (car x))
2767 (list
2768 (if (and (stringp (car (cdr x)))
2769 (file-name-absolute-p (car (cdr x)))
2770 (not (tramp-file-name-p (car (cdr x)))))
2771 (tramp-make-tramp-file-name
2772 method user host (car (cdr x)))
2773 (car (cdr x))))
2774 (cdr (cdr x))))
2775 (symbol-value 'tramp-bkup-backup-directory-info))
2776 (symbol-value 'bkup-backup-directory-info)))))
2777
2778 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
2779
2780(defun tramp-handle-insert-file-contents
2781 (filename &optional visit beg end replace)
2782 "Like `insert-file-contents' for Tramp files."
2783 (barf-if-buffer-read-only)
2784 (setq filename (expand-file-name filename))
2785 (let (result local-copy remote-copy)
2786 (with-parsed-tramp-file-name filename nil
2787 (unwind-protect
2788 (if (not (file-exists-p filename))
2789 ;; We don't raise a Tramp error, because it might be
2790 ;; suppressed, like in `find-file-noselect-1'.
2791 (signal 'file-error
2792 (list "File not found on remote host" filename))
2793
2794 (if (and (tramp-local-host-p v)
2795 (let (file-name-handler-alist)
2796 (file-readable-p localname)))
2797 ;; Short track: if we are on the local host, we can
2798 ;; run directly.
2799 (setq result
2800 (tramp-run-real-handler
2801 'insert-file-contents
2802 (list localname visit beg end replace)))
2803
2804 ;; When we shall insert only a part of the file, we copy
2805 ;; this part.
2806 (when (or beg end)
2807 (setq remote-copy (tramp-make-tramp-temp-file v))
2808 ;; This is defined in tramp-sh.el. Let's assume this
2809 ;; is loaded already.
2810 (tramp-compat-funcall 'tramp-send-command
2811 v
2812 (cond
2813 ((and beg end)
2814 (format "tail -c +%d %s | head -c +%d >%s"
2815 (1+ beg) (tramp-shell-quote-argument localname)
2816 (- end beg) remote-copy))
2817 (beg
2818 (format "tail -c +%d %s >%s"
2819 (1+ beg) (tramp-shell-quote-argument localname)
2820 remote-copy))
2821 (end
2822 (format "head -c +%d %s >%s"
2823 (1+ end) (tramp-shell-quote-argument localname)
2824 remote-copy)))))
2825
2826 ;; `insert-file-contents-literally' takes care to avoid
2827 ;; calling jka-compr. By let-binding
2828 ;; `inhibit-file-name-operation', we propagate that care
2829 ;; to the `file-local-copy' operation.
2830 (setq local-copy
2831 (let ((inhibit-file-name-operation
2832 (when (eq inhibit-file-name-operation
2833 'insert-file-contents)
2834 'file-local-copy)))
2835 (cond
2836 ((stringp remote-copy)
2837 (file-local-copy
2838 (tramp-make-tramp-file-name
2839 method user host remote-copy)))
2840 ((stringp tramp-temp-buffer-file-name)
2841 (copy-file filename tramp-temp-buffer-file-name 'ok)
2842 tramp-temp-buffer-file-name)
2843 (t (file-local-copy filename)))))
2844
2845 ;; When the file is not readable for the owner, it
d68b0220
MA
2846 ;; cannot be inserted, even if it is readable for the
2847 ;; group or for everybody.
4a93e698
MA
2848 (set-file-modes local-copy (tramp-compat-octal-to-decimal "0600"))
2849
2850 (when (and (null remote-copy)
2851 (tramp-get-method-parameter
2852 method 'tramp-copy-keep-tmpfile))
2853 ;; We keep the local file for performance reasons,
2854 ;; useful for "rsync".
d68b0220 2855 (setq tramp-temp-buffer-file-name local-copy))
4a93e698
MA
2856
2857 (with-progress-reporter
2858 v 3 (format "Inserting local temp file `%s'" local-copy)
2859 ;; We must ensure that `file-coding-system-alist'
2860 ;; matches `local-copy'.
2861 (let ((file-coding-system-alist
2862 (tramp-find-file-name-coding-system-alist
2863 filename local-copy)))
2864 (setq result
2865 (insert-file-contents
2866 local-copy nil nil nil replace))))))
2867
2868 ;; Save exit.
2869 (progn
2870 (when visit
2871 (setq buffer-file-name filename)
2872 (setq buffer-read-only (not (file-writable-p filename)))
2873 (set-visited-file-modtime)
e274eb13 2874 (set-buffer-modified-p nil))
4a93e698
MA
2875 (when (and (stringp local-copy)
2876 (or remote-copy (null tramp-temp-buffer-file-name)))
2877 (delete-file local-copy))
2878 (when (stringp remote-copy)
2879 (delete-file
2880 (tramp-make-tramp-file-name method user host remote-copy))))))
2881
2882 ;; Result.
2883 (list (expand-file-name filename)
2884 (cadr result))))
2885
2886(defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2887 "Like `load' for Tramp files."
2888 (with-parsed-tramp-file-name (expand-file-name file) nil
2889 (unless nosuffix
2890 (cond ((file-exists-p (concat file ".elc"))
2891 (setq file (concat file ".elc")))
2892 ((file-exists-p (concat file ".el"))
2893 (setq file (concat file ".el")))))
2894 (when must-suffix
2895 ;; The first condition is always true for absolute file names.
2896 ;; Included for safety's sake.
2897 (unless (or (file-name-directory file)
2898 (string-match "\\.elc?\\'" file))
2899 (tramp-error
2900 v 'file-error
2901 "File `%s' does not include a `.el' or `.elc' suffix" file)))
2902 (unless noerror
2903 (when (not (file-exists-p file))
2904 (tramp-error v 'file-error "Cannot load nonexistent file `%s'" file)))
2905 (if (not (file-exists-p file))
2906 nil
2907 (let ((tramp-message-show-message (not nomessage)))
2908 (with-progress-reporter v 0 (format "Loading %s" file)
2909 (let ((local-copy (file-local-copy file)))
2910 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2911 (unwind-protect
2912 (load local-copy noerror t t)
2913 (delete-file local-copy)))))
2914 t)))
2915
2916(defun tramp-handle-substitute-in-file-name (filename)
2917 "Like `substitute-in-file-name' for Tramp files.
2918\"//\" and \"/~\" substitute only in the local filename part.
2919If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
2920beginning of local filename are not substituted."
2921 ;; First, we must replace environment variables.
2922 (setq filename (tramp-replace-environment-variables filename))
2923 (with-parsed-tramp-file-name filename nil
2924 (if (equal tramp-syntax 'url)
2925 ;; We need to check localname only. The other parts cannot contain
2926 ;; "//" or "/~".
2927 (if (and (> (length localname) 1)
2928 (or (string-match "//" localname)
2929 (string-match "/~" localname 1)))
2930 (tramp-run-real-handler 'substitute-in-file-name (list filename))
2931 (tramp-make-tramp-file-name
2932 (when method (substitute-in-file-name method))
2933 (when user (substitute-in-file-name user))
2934 (when host (substitute-in-file-name host))
2935 (when localname
2936 (tramp-run-real-handler
2937 'substitute-in-file-name (list localname)))))
2938 ;; Ignore in LOCALNAME everything before "//" or "/~".
2939 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
2940 (setq filename
2941 (concat (file-remote-p filename)
2942 (replace-match "\\1" nil nil localname)))
2943 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
2944 (when (string-match "~$" filename)
2945 (setq filename (concat filename "/"))))
2946 (tramp-run-real-handler 'substitute-in-file-name (list filename)))))
2947
2948(defun tramp-handle-unhandled-file-name-directory (filename)
2949 "Like `unhandled-file-name-directory' for Tramp files."
2950 ;; With Emacs 23, we could simply return `nil'. But we must keep it
2951 ;; for backward compatibility.
2952 (expand-file-name "~/"))
b88f2d0a 2953
4a93e698 2954;;; Functions for establishing connection:
fb7933a3 2955
ac474af1
KG
2956;; The following functions are actions to be taken when seeing certain
2957;; prompts from the remote host. See the variable
2958;; `tramp-actions-before-shell' for usage of these functions.
2959
00d6fd04 2960(defun tramp-action-login (proc vec)
ac474af1 2961 "Send the login name."
00d6fd04
MA
2962 (when (not (stringp tramp-current-user))
2963 (save-window-excursion
2964 (let ((enable-recursive-minibuffers t))
2965 (pop-to-buffer (tramp-get-connection-buffer vec))
2966 (setq tramp-current-user (read-string (match-string 0))))))
2967 (tramp-message vec 3 "Sending login name `%s'" tramp-current-user)
2968 (with-current-buffer (tramp-get-connection-buffer vec)
2969 (tramp-message vec 6 "\n%s" (buffer-string)))
2970 (tramp-send-string vec tramp-current-user))
2971
2972(defun tramp-action-password (proc vec)
ac474af1 2973 "Query the user for a password."
70c11b0b
MA
2974 (with-current-buffer (process-buffer proc)
2975 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
bb6aba9c
MA
2976 (tramp-message vec 3 "Sending %s" (match-string 1))
2977 (tramp-enter-password proc)
2978 ;; Hide password prompt.
2979 (narrow-to-region (point-max) (point-max))))
00d6fd04
MA
2980
2981(defun tramp-action-succeed (proc vec)
ac474af1 2982 "Signal success in finding shell prompt."
ac474af1
KG
2983 (throw 'tramp-action 'ok))
2984
00d6fd04 2985(defun tramp-action-permission-denied (proc vec)
ac474af1 2986 "Signal permission denied."
00d6fd04 2987 (kill-process proc)
ac474af1
KG
2988 (throw 'tramp-action 'permission-denied))
2989
00d6fd04 2990(defun tramp-action-yesno (proc vec)
3cdaec13
KG
2991 "Ask the user for confirmation using `yes-or-no-p'.
2992Send \"yes\" to remote process on confirmation, abort otherwise.
2993See also `tramp-action-yn'."
ac474af1 2994 (save-window-excursion
00d6fd04
MA
2995 (let ((enable-recursive-minibuffers t))
2996 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
2997 (unless (yes-or-no-p (match-string 0))
2998 (kill-process proc)
2999 (throw 'tramp-action 'permission-denied))
3000 (with-current-buffer (tramp-get-connection-buffer vec)
3001 (tramp-message vec 6 "\n%s" (buffer-string)))
3002 (tramp-send-string vec "yes"))))
3003
3004(defun tramp-action-yn (proc vec)
3cdaec13
KG
3005 "Ask the user for confirmation using `y-or-n-p'.
3006Send \"y\" to remote process on confirmation, abort otherwise.
3007See also `tramp-action-yesno'."
3008 (save-window-excursion
00d6fd04
MA
3009 (let ((enable-recursive-minibuffers t))
3010 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
3011 (unless (y-or-n-p (match-string 0))
3012 (kill-process proc)
3013 (throw 'tramp-action 'permission-denied))
3014 (with-current-buffer (tramp-get-connection-buffer vec)
3015 (tramp-message vec 6 "\n%s" (buffer-string)))
3016 (tramp-send-string vec "y"))))
3017
3018(defun tramp-action-terminal (proc vec)
487f4fb7
KG
3019 "Tell the remote host which terminal type to use.
3020The terminal type can be configured with `tramp-terminal-type'."
00d6fd04 3021 (tramp-message vec 5 "Setting `%s' as terminal type." tramp-terminal-type)
7e780ff1
MA
3022 (with-current-buffer (tramp-get-connection-buffer vec)
3023 (tramp-message vec 6 "\n%s" (buffer-string)))
00d6fd04 3024 (tramp-send-string vec tramp-terminal-type))
487f4fb7 3025
00d6fd04 3026(defun tramp-action-process-alive (proc vec)
a94d821f 3027 "Check, whether a process has finished."
00d6fd04 3028 (unless (memq (process-status proc) '(run open))
19a87064
MA
3029 (throw 'tramp-action 'process-died)))
3030
00d6fd04 3031(defun tramp-action-out-of-band (proc vec)
a94d821f 3032 "Check, whether an out-of-band copy has finished."
00d6fd04
MA
3033 (cond ((and (memq (process-status proc) '(stop exit))
3034 (zerop (process-exit-status proc)))
3035 (tramp-message vec 3 "Process has finished.")
38c65fca 3036 (throw 'tramp-action 'ok))
00d6fd04
MA
3037 ((or (and (memq (process-status proc) '(stop exit))
3038 (not (zerop (process-exit-status proc))))
3039 (memq (process-status proc) '(signal)))
01917a18
MA
3040 ;; `scp' could have copied correctly, but set modes could have failed.
3041 ;; This can be ignored.
00d6fd04
MA
3042 (with-current-buffer (process-buffer proc)
3043 (goto-char (point-min))
3044 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
3045 (progn
3046 (tramp-message vec 5 "'set mode' error ignored.")
3047 (tramp-message vec 3 "Process has finished.")
3048 (throw 'tramp-action 'ok))
3049 (tramp-message vec 3 "Process has died.")
3050 (throw 'tramp-action 'process-died))))
38c65fca
KG
3051 (t nil)))
3052
4a93e698 3053;;; Functions for processing the actions:
ac474af1 3054
00d6fd04 3055(defun tramp-process-one-action (proc vec actions)
ac474af1 3056 "Wait for output from the shell and perform one action."
00d6fd04 3057 (let (found todo item pattern action)
e6466697 3058 (while (not found)
00d6fd04
MA
3059 ;; Reread output once all actions have been performed.
3060 ;; Obviously, the output was not complete.
3061 (tramp-accept-process-output proc 1)
e6466697
MA
3062 (setq todo actions)
3063 (while todo
e6466697 3064 (setq item (pop todo))
95d610cb 3065 (setq pattern (format "\\(%s\\)\\'" (symbol-value (nth 0 item))))
e6466697 3066 (setq action (nth 1 item))
00d6fd04
MA
3067 (tramp-message
3068 vec 5 "Looking for regexp \"%s\" from remote shell" pattern)
3069 (when (tramp-check-for-regexp proc pattern)
3070 (tramp-message vec 5 "Call `%s'" (symbol-name action))
3071 (setq found (funcall action proc vec)))))
e6466697
MA
3072 found))
3073
00d6fd04 3074(defun tramp-process-actions (proc vec actions &optional timeout)
e6466697 3075 "Perform actions until success or TIMEOUT."
9e021389 3076 ;; Preserve message for `progress-reporter'.
6139f995 3077 (tramp-compat-with-temp-message ""
9e021389
MA
3078 ;; Enable auth-source and password-cache.
3079 (tramp-set-connection-property vec "first-password-request" t)
158d5945
MA
3080 (save-restriction
3081 (let (exit)
3082 (while (not exit)
3083 (tramp-message proc 3 "Waiting for prompts from remote shell")
3084 (setq exit
3085 (catch 'tramp-action
3086 (if timeout
3087 (with-timeout (timeout)
3088 (tramp-process-one-action proc vec actions))
3089 (tramp-process-one-action proc vec actions)))))
3090 (with-current-buffer (tramp-get-connection-buffer vec)
3091 (widen)
3092 (tramp-message vec 6 "\n%s" (buffer-string)))
3093 (unless (eq exit 'ok)
3094 (tramp-clear-passwd vec)
3095 (tramp-error-with-buffer
3096 nil vec 'file-error
3097 (cond
3098 ((eq exit 'permission-denied) "Permission denied")
3099 ((eq exit 'process-died) "Process died")
3100 (t "Login failed"))))))))
fb7933a3 3101
4a93e698 3102:;; Utility functions:
fb7933a3 3103
00d6fd04 3104(defun tramp-accept-process-output (&optional proc timeout timeout-msecs)
d2a2c17f
MA
3105 "Like `accept-process-output' for Tramp processes.
3106This is needed in order to hide `last-coding-system-used', which is set
3107for process communication also."
00d6fd04
MA
3108 (with-current-buffer (process-buffer proc)
3109 (tramp-message proc 10 "%s %s" proc (process-status proc))
3110 (let (buffer-read-only last-coding-system-used)
3111 ;; Under Windows XP, accept-process-output doesn't return
3112 ;; sometimes. So we add an additional timeout.
3113 (with-timeout ((or timeout 1))
3114 (accept-process-output proc timeout timeout-msecs)))
3115 (tramp-message proc 10 "\n%s" (buffer-string))))
3116
3117(defun tramp-check-for-regexp (proc regexp)
a94d821f 3118 "Check, whether REGEXP is contained in process buffer of PROC.
00d6fd04
MA
3119Erase echoed commands if exists."
3120 (with-current-buffer (process-buffer proc)
3121 (goto-char (point-min))
674da028 3122
00d6fd04
MA
3123 ;; Check whether we need to remove echo output.
3124 (when (and (tramp-get-connection-property proc "check-remote-echo" nil)
3125 (re-search-forward tramp-echoed-echo-mark-regexp nil t))
3126 (let ((begin (match-beginning 0)))
3127 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t)
3128 ;; Discard echo from remote output.
3129 (tramp-set-connection-property proc "check-remote-echo" nil)
3130 (tramp-message proc 5 "echo-mark found")
b533bc97 3131 (forward-line 1)
00d6fd04
MA
3132 (delete-region begin (point))
3133 (goto-char (point-min)))))
674da028 3134
68712eb6
MA
3135 (when (or (not (tramp-get-connection-property proc "check-remote-echo" nil))
3136 ;; Sometimes, the echo string is suppressed on the remote side.
3137 (not (string-equal
0d5852cf
MA
3138 (tramp-compat-funcall
3139 'substring-no-properties tramp-echo-mark-marker
68712eb6 3140 0 (min tramp-echo-mark-marker-length (1- (point-max))))
0d5852cf
MA
3141 (tramp-compat-funcall
3142 'buffer-substring-no-properties
68712eb6 3143 1 (min (1+ tramp-echo-mark-marker-length) (point-max))))))
70c11b0b 3144 ;; No echo to be handled, now we can look for the regexp.
674da028 3145 (goto-char (point-min))
00d6fd04 3146 (re-search-forward regexp nil t))))
d2a2c17f 3147
fb7933a3
KG
3148(defun tramp-wait-for-regexp (proc timeout regexp)
3149 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
3150Expects the output of PROC to be sent to the current buffer. Returns
3151the string that matched, or nil. Waits indefinitely if TIMEOUT is
3152nil."
00d6fd04
MA
3153 (with-current-buffer (process-buffer proc)
3154 (let ((found (tramp-check-for-regexp proc regexp))
3155 (start-time (current-time)))
3156 (cond (timeout
3157 ;; Work around a bug in XEmacs 21, where the timeout
3158 ;; expires faster than it should. This degenerates
3159 ;; to polling for buggy XEmacsen, but oh, well.
3160 (while (and (not found)
3161 (< (tramp-time-diff (current-time) start-time)
3162 timeout))
3163 (with-timeout (timeout)
3164 (while (not found)
3165 (tramp-accept-process-output proc 1)
3166 (unless (memq (process-status proc) '(run open))
3167 (tramp-error-with-buffer
3168 nil proc 'file-error "Process has died"))
3169 (setq found (tramp-check-for-regexp proc regexp))))))
3170 (t
3171 (while (not found)
3172 (tramp-accept-process-output proc 1)
3173 (unless (memq (process-status proc) '(run open))
3174 (tramp-error-with-buffer
3175 nil proc 'file-error "Process has died"))
3176 (setq found (tramp-check-for-regexp proc regexp)))))
3177 (tramp-message proc 6 "\n%s" (buffer-string))
fb7933a3 3178 (when (not found)
00d6fd04
MA
3179 (if timeout
3180 (tramp-error
3181 proc 'file-error "[[Regexp `%s' not found in %d secs]]"
3182 regexp timeout)
3183 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
3184 found)))
fb7933a3 3185
7e780ff1
MA
3186;; We don't call `tramp-send-string' in order to hide the password
3187;; from the debug buffer, and because end-of-line handling of the
3188;; string.
3189(defun tramp-enter-password (proc)
00d6fd04
MA
3190 "Prompt for a password and send it to the remote end."
3191 (process-send-string
7e780ff1
MA
3192 proc (concat (tramp-read-passwd proc)
3193 (or (tramp-get-method-parameter
3194 tramp-current-method
3195 'tramp-password-end-of-line)
3196 tramp-default-password-end-of-line))))
00d6fd04 3197
7432277c
KG
3198;; It seems that Tru64 Unix does not like it if long strings are sent
3199;; to it in one go. (This happens when sending the Perl
3200;; `file-attributes' implementation, for instance.) Therefore, we
27e813fe 3201;; have this function which sends the string in chunks.
00d6fd04
MA
3202(defun tramp-send-string (vec string)
3203 "Send the STRING via connection VEC.
7432277c
KG
3204
3205The STRING is expected to use Unix line-endings, but the lines sent to
3206the remote host use line-endings as defined in the variable
00d6fd04
MA
3207`tramp-rsh-end-of-line'. The communication buffer is erased before sending."
3208 (let* ((p (tramp-get-connection-process vec))
3209 (chunksize (tramp-get-connection-property p "chunksize" nil)))
3210 (unless p
3211 (tramp-error
3212 vec 'file-error "Can't send string to remote host -- not logged in"))
3213 (tramp-set-connection-property p "last-cmd-time" (current-time))
3214 (tramp-message vec 10 "%s" string)
3215 (with-current-buffer (tramp-get-connection-buffer vec)
3216 ;; Clean up the buffer. We cannot call `erase-buffer' because
3217 ;; narrowing might be in effect.
3218 (let (buffer-read-only) (delete-region (point-min) (point-max)))
27e813fe 3219 ;; Replace "\n" by `tramp-rsh-end-of-line'.
00d6fd04
MA
3220 (setq string
3221 (mapconcat 'identity
70c11b0b 3222 (tramp-compat-split-string string "\n")
00d6fd04
MA
3223 tramp-rsh-end-of-line))
3224 (unless (or (string= string "")
3225 (string-equal (substring string -1) tramp-rsh-end-of-line))
3226 (setq string (concat string tramp-rsh-end-of-line)))
27e813fe 3227 ;; Send the string.
00d6fd04
MA
3228 (if (and chunksize (not (zerop chunksize)))
3229 (let ((pos 0)
3230 (end (length string)))
3231 (while (< pos end)
3232 (tramp-message
3233 vec 10 "Sending chunk from %s to %s"
3234 pos (min (+ pos chunksize) end))
3235 (process-send-string
3236 p (substring string pos (min (+ pos chunksize) end)))
3237 (setq pos (+ pos chunksize))))
3238 (process-send-string p string)))))
fb7933a3 3239
ce3f516f 3240(defun tramp-get-inode (vec)
00d6fd04
MA
3241 "Returns the virtual inode number.
3242If it doesn't exist, generate a new one."
ce3f516f
MA
3243 (let ((string (tramp-make-tramp-file-name
3244 (tramp-file-name-method vec)
3245 (tramp-file-name-user vec)
3246 (tramp-file-name-host vec)
3247 "")))
00d6fd04
MA
3248 (unless (assoc string tramp-inodes)
3249 (add-to-list 'tramp-inodes
3250 (list string (length tramp-inodes))))
3251 (nth 1 (assoc string tramp-inodes))))
3252
3253(defun tramp-get-device (vec)
c82c5727
LH
3254 "Returns the virtual device number.
3255If it doesn't exist, generate a new one."
00d6fd04
MA
3256 (let ((string (tramp-make-tramp-file-name
3257 (tramp-file-name-method vec)
3258 (tramp-file-name-user vec)
3259 (tramp-file-name-host vec)
3260 "")))
c82c5727
LH
3261 (unless (assoc string tramp-devices)
3262 (add-to-list 'tramp-devices
3263 (list string (length tramp-devices))))
b946a456 3264 (cons -1 (nth 1 (assoc string tramp-devices)))))
fb7933a3 3265
00d6fd04 3266(defun tramp-equal-remote (file1 file2)
a94d821f 3267 "Check, whether the remote parts of FILE1 and FILE2 are identical.
00d6fd04
MA
3268The check depends on method, user and host name of the files. If
3269one of the components is missing, the default values are used.
3270The local file name parts of FILE1 and FILE2 are not taken into
3271account.
fb7933a3 3272
00d6fd04
MA
3273Example:
3274
3275 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
3276
3277would yield `t'. On the other hand, the following check results in nil:
3278
3279 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
9e6ab520
MA
3280 (and (stringp (file-remote-p file1))
3281 (stringp (file-remote-p file2))
94be87e8 3282 (string-equal (file-remote-p file1) (file-remote-p file2))))
00d6fd04 3283
00d6fd04 3284(defun tramp-get-method-parameter (method param)
c951aecb 3285 "Return the method parameter PARAM.
6a29a838 3286If the `tramp-methods' entry does not exist, return nil."
00d6fd04
MA
3287 (let ((entry (assoc param (assoc method tramp-methods))))
3288 (when entry (cadr entry))))
90f8dc03 3289
4a93e698
MA
3290(defun tramp-mode-string-to-int (mode-string)
3291 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
3292 (let* (case-fold-search
3293 (mode-chars (string-to-vector mode-string))
3294 (owner-read (aref mode-chars 1))
3295 (owner-write (aref mode-chars 2))
3296 (owner-execute-or-setid (aref mode-chars 3))
3297 (group-read (aref mode-chars 4))
3298 (group-write (aref mode-chars 5))
3299 (group-execute-or-setid (aref mode-chars 6))
3300 (other-read (aref mode-chars 7))
3301 (other-write (aref mode-chars 8))
3302 (other-execute-or-sticky (aref mode-chars 9)))
3303 (save-match-data
3304 (logior
3305 (cond
3306 ((char-equal owner-read ?r) (tramp-compat-octal-to-decimal "00400"))
3307 ((char-equal owner-read ?-) 0)
3308 (t (error "Second char `%c' must be one of `r-'" owner-read)))
3309 (cond
3310 ((char-equal owner-write ?w) (tramp-compat-octal-to-decimal "00200"))
3311 ((char-equal owner-write ?-) 0)
3312 (t (error "Third char `%c' must be one of `w-'" owner-write)))
3313 (cond
3314 ((char-equal owner-execute-or-setid ?x)
3315 (tramp-compat-octal-to-decimal "00100"))
3316 ((char-equal owner-execute-or-setid ?S)
3317 (tramp-compat-octal-to-decimal "04000"))
3318 ((char-equal owner-execute-or-setid ?s)
3319 (tramp-compat-octal-to-decimal "04100"))
3320 ((char-equal owner-execute-or-setid ?-) 0)
3321 (t (error "Fourth char `%c' must be one of `xsS-'"
3322 owner-execute-or-setid)))
3323 (cond
3324 ((char-equal group-read ?r) (tramp-compat-octal-to-decimal "00040"))
3325 ((char-equal group-read ?-) 0)
3326 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
3327 (cond
3328 ((char-equal group-write ?w) (tramp-compat-octal-to-decimal "00020"))
3329 ((char-equal group-write ?-) 0)
3330 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
3331 (cond
3332 ((char-equal group-execute-or-setid ?x)
3333 (tramp-compat-octal-to-decimal "00010"))
3334 ((char-equal group-execute-or-setid ?S)
3335 (tramp-compat-octal-to-decimal "02000"))
3336 ((char-equal group-execute-or-setid ?s)
3337 (tramp-compat-octal-to-decimal "02010"))
3338 ((char-equal group-execute-or-setid ?-) 0)
3339 (t (error "Seventh char `%c' must be one of `xsS-'"
3340 group-execute-or-setid)))
3341 (cond
3342 ((char-equal other-read ?r)
3343 (tramp-compat-octal-to-decimal "00004"))
3344 ((char-equal other-read ?-) 0)
3345 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
3346 (cond
3347 ((char-equal other-write ?w) (tramp-compat-octal-to-decimal "00002"))
3348 ((char-equal other-write ?-) 0)
3349 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
3350 (cond
3351 ((char-equal other-execute-or-sticky ?x)
3352 (tramp-compat-octal-to-decimal "00001"))
3353 ((char-equal other-execute-or-sticky ?T)
3354 (tramp-compat-octal-to-decimal "01000"))
3355 ((char-equal other-execute-or-sticky ?t)
3356 (tramp-compat-octal-to-decimal "01001"))
3357 ((char-equal other-execute-or-sticky ?-) 0)
3358 (t (error "Tenth char `%c' must be one of `xtT-'"
3359 other-execute-or-sticky)))))))
3360
3361(defun tramp-local-host-p (vec)
3362 "Return t if this points to the local host, nil otherwise."
3363 ;; We cannot use `tramp-file-name-real-host'. A port is an
3364 ;; indication for an ssh tunnel or alike.
3365 (let ((host (tramp-file-name-host vec)))
3366 (and
3367 (stringp host)
3368 (string-match tramp-local-host-regexp host)
3369 ;; The method shall be applied to one of the shell file name
3370 ;; handler. `tramp-local-host-p' is also called for "smb" and
3371 ;; alike, where it must fail.
3372 (tramp-get-method-parameter
3373 (tramp-file-name-method vec) 'tramp-login-program)
3374 ;; The local temp directory must be writable for the other user.
3375 (file-writable-p
3376 (tramp-make-tramp-file-name
3377 (tramp-file-name-method vec)
3378 (tramp-file-name-user vec)
3379 host
3380 (tramp-compat-temporary-file-directory)))
3381 ;; On some systems, chown runs only for root.
3382 (or (zerop (user-uid))
3383 ;; This is defined in tramp-sh.el. Let's assume this is
3384 ;; loaded already.
3385 (zerop (tramp-compat-funcall 'tramp-get-remote-uid vec 'integer))))))
3386
3387(defun tramp-make-tramp-temp-file (vec)
3388 "Create a temporary file on the remote host identified by VEC.
3389Return the local name of the temporary file."
3390 (let ((prefix
3391 (tramp-make-tramp-file-name
3392 (tramp-file-name-method vec)
3393 (tramp-file-name-user vec)
3394 (tramp-file-name-host vec)
3395 (tramp-drop-volume-letter
3396 (expand-file-name
3397 tramp-temp-name-prefix
3398 ;; This is defined in tramp-sh.el. Let's assume this is
3399 ;; loaded already.
3400 (tramp-compat-funcall 'tramp-get-remote-tmpdir vec)))))
3401 result)
3402 (while (not result)
3403 ;; `make-temp-file' would be the natural choice for
3404 ;; implementation. But it calls `write-region' internally,
3405 ;; which also needs a temporary file - we would end in an
3406 ;; infinite loop.
3407 (setq result (make-temp-name prefix))
3408 (if (file-exists-p result)
3409 (setq result nil)
3410 ;; This creates the file by side effect.
3411 (set-file-times result)
3412 (set-file-modes result (tramp-compat-octal-to-decimal "0700"))))
3413
3414 ;; Return the local part.
3415 (with-parsed-tramp-file-name result nil localname)))
fb7933a3 3416
4a93e698
MA
3417(defun tramp-delete-temp-file-function ()
3418 "Remove temporary files related to current buffer."
3419 (when (stringp tramp-temp-buffer-file-name)
3420 (ignore-errors (delete-file tramp-temp-buffer-file-name))))
3421
3422(add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
3423(add-hook 'tramp-cache-unload-hook
3424 (lambda ()
3425 (remove-hook 'kill-buffer-hook
3426 'tramp-delete-temp-file-function)))
3427
3428;;; Auto saving to a special directory:
c1105d05
MA
3429
3430(unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
3431 (defadvice make-auto-save-file-name
3432 (around tramp-advice-make-auto-save-file-name () activate)
03c1ad43 3433 "Invoke `tramp-*-handle-make-auto-save-file-name' for Tramp files."
b533bc97 3434 (if (tramp-tramp-file-p (buffer-file-name))
7f49fe46
MA
3435 ;; We cannot call `tramp-handle-make-auto-save-file-name'
3436 ;; directly, because this would bypass the locking mechanism.
3437 (setq ad-return-value
3438 (tramp-file-name-handler 'make-auto-save-file-name))
a69c01a0 3439 ad-do-it))
191bb792
MA
3440 (add-hook
3441 'tramp-unload-hook
3442 (lambda ()
3443 (ad-remove-advice
3444 'make-auto-save-file-name
d7ec1df7
MA
3445 'around 'tramp-advice-make-auto-save-file-name)
3446 (ad-activate 'make-auto-save-file-name))))
fb7933a3 3447
b533bc97
MA
3448;; In XEmacs < 21.5, autosaved remote files have permission 0666 minus
3449;; umask. This is a security threat.
414da5ab
MA
3450
3451(defun tramp-set-auto-save-file-modes ()
3452 "Set permissions of autosaved remote files to the original permissions."
3453 (let ((bfn (buffer-file-name)))
b533bc97 3454 (when (and (tramp-tramp-file-p bfn)
b50dd0d2 3455 (buffer-modified-p)
414da5ab 3456 (stringp buffer-auto-save-file-name)
340b8d4f
MA
3457 (not (equal bfn buffer-auto-save-file-name)))
3458 (unless (file-exists-p buffer-auto-save-file-name)
3459 (write-region "" nil buffer-auto-save-file-name))
3460 ;; Permissions should be set always, because there might be an old
3461 ;; auto-saved file belonging to another original file. This could
3462 ;; be a security threat.
0f34aa77
MA
3463 (set-file-modes
3464 buffer-auto-save-file-name
3465 (or (file-modes bfn) (tramp-compat-octal-to-decimal "0600"))))))
414da5ab 3466
b533bc97
MA
3467(unless (and (featurep 'xemacs)
3468 (= emacs-major-version 21)
3469 (> emacs-minor-version 4))
a69c01a0
MA
3470 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
3471 (add-hook 'tramp-unload-hook
aa485f7c
MA
3472 (lambda ()
3473 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
414da5ab 3474
fb7933a3
KG
3475(defun tramp-subst-strs-in-string (alist string)
3476 "Replace all occurrences of the string FROM with TO in STRING.
3477ALIST is of the form ((FROM . TO) ...)."
3478 (save-match-data
3479 (while alist
3480 (let* ((pr (car alist))
3481 (from (car pr))
3482 (to (cdr pr)))
3483 (while (string-match (regexp-quote from) string)
3484 (setq string (replace-match to t t string)))
3485 (setq alist (cdr alist))))
3486 string))
3487
4a93e698 3488;;; Compatibility functions section:
fb7933a3 3489
00d6fd04 3490(defun tramp-read-passwd (proc &optional prompt)
fb7933a3 3491 "Read a password from user (compat function).
5615d63f 3492Consults the auth-source package.
5ec2cc41 3493Invokes `password-read' if available, `read-passwd' else."
00d6fd04
MA
3494 (let* ((key (tramp-make-tramp-file-name
3495 tramp-current-method tramp-current-user
3496 tramp-current-host ""))
3497 (pw-prompt
3498 (or prompt
3499 (with-current-buffer (process-buffer proc)
3500 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
3501 (format "%s for %s " (capitalize (match-string 1)) key)))))
7540f029
MA
3502 (with-parsed-tramp-file-name key nil
3503 (prog1
3504 (or
3505 ;; See if auth-sources contains something useful, if it's bound.
3506 (and (boundp 'auth-sources)
3507 (tramp-get-connection-property v "first-password-request" nil)
3508 ;; Try with Tramp's current method.
0d5852cf
MA
3509 (tramp-compat-funcall
3510 'auth-source-user-or-password
3511 "password" tramp-current-host tramp-current-method))
7540f029
MA
3512 ;; Try the password cache.
3513 (when (functionp 'password-read)
3514 (unless (tramp-get-connection-property
3515 v "first-password-request" nil)
0d5852cf 3516 (tramp-compat-funcall 'password-cache-remove key))
7540f029 3517 (let ((password
0d5852cf
MA
3518 (tramp-compat-funcall 'password-read pw-prompt key)))
3519 (tramp-compat-funcall 'password-cache-add key password)
7540f029
MA
3520 password))
3521 ;; Else, get the password interactively.
3522 (read-passwd pw-prompt))
3523 (tramp-set-connection-property v "first-password-request" nil)))))
00d6fd04 3524
9c13938d
MA
3525(defun tramp-clear-passwd (vec)
3526 "Clear password cache for connection related to VEC."
0d5852cf
MA
3527 (tramp-compat-funcall
3528 'password-cache-remove
3529 (tramp-make-tramp-file-name
3530 (tramp-file-name-method vec)
3531 (tramp-file-name-user vec)
3532 (tramp-file-name-host vec)
3533 "")))
00d6fd04
MA
3534
3535;; Snarfed code from time-date.el and parse-time.el
3536
3537(defconst tramp-half-a-year '(241 17024)
3538"Evaluated by \"(days-to-time 183)\".")
3539
3540(defconst tramp-parse-time-months
3541 '(("jan" . 1) ("feb" . 2) ("mar" . 3)
3542 ("apr" . 4) ("may" . 5) ("jun" . 6)
3543 ("jul" . 7) ("aug" . 8) ("sep" . 9)
3544 ("oct" . 10) ("nov" . 11) ("dec" . 12))
3545 "Alist mapping month names to integers.")
3546
3547(defun tramp-time-less-p (t1 t2)
3548 "Say whether time value T1 is less than time value T2."
3549 (unless t1 (setq t1 '(0 0)))
3550 (unless t2 (setq t2 '(0 0)))
3551 (or (< (car t1) (car t2))
3552 (and (= (car t1) (car t2))
3553 (< (nth 1 t1) (nth 1 t2)))))
3554
3555(defun tramp-time-subtract (t1 t2)
3556 "Subtract two time values.
3557Return the difference in the format of a time value."
3558 (unless t1 (setq t1 '(0 0)))
3559 (unless t2 (setq t2 '(0 0)))
3560 (let ((borrow (< (cadr t1) (cadr t2))))
3561 (list (- (car t1) (car t2) (if borrow 1 0))
3562 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
fb7933a3
KG
3563
3564(defun tramp-time-diff (t1 t2)
3565 "Return the difference between the two times, in seconds.
1a762140 3566T1 and T2 are time values (as returned by `current-time' for example)."
ea9d1443
KG
3567 (cond ((and (fboundp 'subtract-time)
3568 (fboundp 'float-time))
0d5852cf
MA
3569 (tramp-compat-funcall
3570 'float-time (tramp-compat-funcall 'subtract-time t1 t2)))
ea9d1443
KG
3571 ((and (fboundp 'subtract-time)
3572 (fboundp 'time-to-seconds))
0d5852cf
MA
3573 (tramp-compat-funcall
3574 'time-to-seconds (tramp-compat-funcall 'subtract-time t1 t2)))
fb7933a3 3575 ((fboundp 'itimer-time-difference)
0d5852cf
MA
3576 (tramp-compat-funcall
3577 'itimer-time-difference
3578 (if (< (length t1) 3) (append t1 '(0)) t1)
3579 (if (< (length t2) 3) (append t2 '(0)) t2)))
fb7933a3 3580 (t
00d6fd04 3581 (let ((time (tramp-time-subtract t1 t2)))
ea9d1443
KG
3582 (+ (* (car time) 65536.0)
3583 (cadr time)
3584 (/ (or (nth 2 time) 0) 1000000.0))))))
fb7933a3 3585
fb7933a3
KG
3586;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
3587;; does not deal well with newline characters. Newline is replaced by
3588;; backslash newline. But if, say, the string `a backslash newline b'
3589;; is passed to a shell, the shell will expand this into "ab",
3590;; completely omitting the newline. This is not what was intended.
3591;; It does not appear to be possible to make the function
3592;; `shell-quote-argument' work with newlines without making it
3593;; dependent on the shell used. But within this package, we know that
3594;; we will always use a Bourne-like shell, so we use an approach which
3595;; groks newlines.
3596;;
3597;; The approach is simple: we call `shell-quote-argument', then
3598;; massage the newline part of the result.
3599;;
3600;; This function should produce a string which is grokked by a Unix
3601;; shell, even if the Emacs is running on Windows. Since this is the
3602;; kludges section, we bind `system-type' in such a way that
3603;; `shell-quote-arguments' behaves as if on Unix.
3604;;
3605;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
3606;; function to work with Bourne-like shells.
3607;;
3608;; CCC: This function should be rewritten so that
3609;; `shell-quote-argument' is not used. This way, we are safe from
3610;; changes in `shell-quote-argument'.
0f34aa77 3611;;;###tramp-autoload
fb7933a3
KG
3612(defun tramp-shell-quote-argument (s)
3613 "Similar to `shell-quote-argument', but groks newlines.
3614Only works for Bourne-like shells."
3615 (let ((system-type 'not-windows))
3616 (save-match-data
3617 (let ((result (shell-quote-argument s))
3618 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
3619 (when (and (>= (length result) 2)
3620 (string= (substring result 0 2) "\\~"))
3621 (setq result (substring result 1)))
3622 (while (string-match nl result)
3623 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
3624 t t result)))
3625 result))))
3626
a69c01a0
MA
3627;; Checklist for `tramp-unload-hook'
3628;; - Unload all `tramp-*' packages
3629;; - Reset `file-name-handler-alist'
3630;; - Cleanup hooks where Tramp functions are in
3631;; - Cleanup advised functions
3632;; - Cleanup autoloads
3633;;;###autoload
3634(defun tramp-unload-tramp ()
08b1eb21 3635 "Discard Tramp from loading remote files."
a69c01a0 3636 (interactive)
a69c01a0 3637 ;; ange-ftp settings must be enabled.
0d5852cf 3638 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp)
0f34aa77 3639 ;; Maybe it's not loaded yet.
03c1ad43 3640 (ignore-errors (unload-feature 'tramp 'force)))
ccb4a481 3641
fb7933a3
KG
3642(provide 'tramp)
3643
fb7933a3
KG
3644;;; TODO:
3645
fb7933a3 3646;; * Rewrite `tramp-shell-quote-argument' to abstain from using
b1d06e75 3647;; `shell-quote-argument'.
fb7933a3
KG
3648;; * In Emacs 21, `insert-directory' shows total number of bytes used
3649;; by the files in that directory. Add this here.
3650;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
3651;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
fb7933a3 3652;; * abbreviate-file-name
8e754ea2 3653;; * Better error checking. At least whenever we see something
fb7933a3
KG
3654;; strange when doing zerop, we should kill the process and start
3655;; again. (Greg Stark)
3cdaec13 3656;; * Username and hostname completion.
6c4e47fa 3657;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
8daea7fc 3658;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
16674e4f 3659;; Code is nearly identical.
00d6fd04 3660;; * Make `tramp-default-user' obsolete.
3e2fa353
MA
3661;; * Implement a general server-local-variable mechanism, as there are
3662;; probably other variables that need different values for different
3663;; servers too. The user could then configure a variable (such as
3664;; tramp-server-local-variable-alist) to define any such variables
3665;; that they need to, which would then be let bound as appropriate
6e4f5731 3666;; in tramp functions. (Jason Rumney)
7e5686f0
MA
3667;; * IMHO, it's a drawback that currently Tramp doesn't support
3668;; Unicode in Dired file names by default. Is it possible to
3669;; improve Tramp to set LC_ALL to "C" only for commands where Tramp
3670;; expects English? Or just to set LC_MESSAGES to "C" if Tramp
6e4f5731 3671;; expects only English messages? (Juri Linkov)
7e5686f0 3672;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
44ffae96
MA
3673;; * I was wondering it it would be possible to use tramp even if I'm
3674;; actually using sshfs. But when I launch a command I would like
3675;; to get it executed on the remote machine where the files really
3676;; are. (Andrea Crotti)
3677;; * Run emerge on two remote files. Bug is described here:
3678;; <http://www.mail-archive.com/tramp-devel@nongnu.org/msg01041.html>.
3679;; (Bug#6850)
fb7933a3
KG
3680
3681;; Functions for file-name-handler-alist:
3682;; diff-latest-backup-file -- in diff.el
fb7933a3
KG
3683
3684;;; tramp.el ends here
57671b72
MA
3685
3686;; Local Variables:
3687;; mode: Emacs-Lisp
3688;; coding: utf-8
3689;; End: