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