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