1 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998-2017 Free Software Foundation, Inc.
5 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
6 ;; Michael Albinus <michael.albinus@gmx.de>
7 ;; Maintainer: Michael Albinus <michael.albinus@gmx.de>
8 ;; Keywords: comm, processes
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
28 ;; This package provides remote file editing, similar to ange-ftp.
29 ;; The difference is that ange-ftp uses FTP to transfer files between
30 ;; the local and the remote host, whereas tramp.el uses a combination
31 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
33 ;; For more detailed instructions, please see the info file.
38 ;; This package only works for Emacs 24.1 and higher.
40 ;; Also see the todo list at the bottom of this file.
42 ;; The current version of Tramp can be retrieved from the following URL:
43 ;; https://ftp.gnu.org/gnu/tramp/
45 ;; There's a mailing list for this, as well. Its name is:
46 ;; tramp-devel@gnu.org
47 ;; You can use the Web to subscribe, under the following URL:
48 ;; https://lists.gnu.org/mailman/listinfo/tramp-devel
50 ;; For the adventurous, the current development sources are available
51 ;; via Git. You can find instructions about this at the following URL:
52 ;; https://savannah.gnu.org/projects/tramp/
54 ;; Don't forget to put on your asbestos longjohns, first!
58 (require 'tramp-compat
)
60 ;; Pacify byte-compiler.
62 (defvar auto-save-file-name-transforms
)
63 (defvar eshell-path-env
)
64 (defvar ls-lisp-use-insert-directory-program
)
65 (defvar outline-regexp
)
67 ;;; User Customizable Internal Variables:
70 "Edit remote files with a combination of ssh, scp, etc."
73 :link
'(custom-manual "(tramp)Top")
76 ;; Maybe we need once a real Tramp mode, with key bindings etc.
78 (defcustom tramp-mode t
79 "Whether Tramp is enabled.
80 If it is set to nil, all remote file names are used literally."
85 (defcustom tramp-verbose
3
86 "Verbosity level for Tramp messages.
87 Any level x includes messages for all levels 1 .. x-1. The levels are
89 0 silent (no tramp messages at all)
92 3 connection to remote hosts (default level)
95 6 sent and received strings
97 8 connection properties
104 (defcustom tramp-backup-directory-alist nil
105 "Alist of filename patterns and backup directory names.
106 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
107 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
108 is a local file name, the backup directory is prepended with Tramp file
109 name prefix \(method, user, host) of file.
111 \(setq tramp-backup-directory-alist backup-directory-alist)
113 gives the same backup policy for Tramp files on their hosts like the
114 policy for local files."
116 :type
'(repeat (cons (regexp :tag
"Regexp matching filename")
117 (directory :tag
"Backup directory name")))
120 (defcustom tramp-auto-save-directory nil
121 "Put auto-save files in this directory, if set.
122 The idea is to use a local directory so that auto-saving is faster.
123 This setting has precedence over `auto-save-file-name-transforms'."
125 :type
'(choice (const :tag
"Use default" nil
)
126 (directory :tag
"Auto save directory name"))
129 (defcustom tramp-encoding-shell
130 (or (tramp-compat-funcall 'w32-shell-name
) "/bin/sh")
131 "Use this program for encoding and decoding commands on the local host.
132 This shell is used to execute the encoding and decoding command on the
133 local host, so if you want to use `~' in those commands, you should
134 choose a shell here which groks tilde expansion. `/bin/sh' normally
135 does not understand tilde expansion.
137 For encoding and decoding, commands like the following are executed:
139 /bin/sh -c COMMAND < INPUT > OUTPUT
141 This variable can be used to change the \"/bin/sh\" part. See the
142 variable `tramp-encoding-command-switch' for the \"-c\" part.
144 If the shell must be forced to be interactive, see
145 `tramp-encoding-command-interactive'.
147 Note that this variable is not used for remote commands. There are
148 mechanisms in tramp.el which automatically determine the right shell to
149 use for the remote host."
151 :type
'(file :must-match t
)
154 (defcustom tramp-encoding-command-switch
155 (if (tramp-compat-funcall 'w32-shell-dos-semantics
) "/c" "-c")
156 "Use this switch together with `tramp-encoding-shell' for local commands.
157 See the variable `tramp-encoding-shell' for more information."
162 (defcustom tramp-encoding-command-interactive
163 (unless (tramp-compat-funcall 'w32-shell-dos-semantics
) "-i")
164 "Use this switch together with `tramp-encoding-shell' for interactive shells.
165 See the variable `tramp-encoding-shell' for more information."
168 :type
'(choice (const nil
) string
)
172 (defvar tramp-methods nil
173 "Alist of methods for remote files.
174 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
175 Each NAME stands for a remote access method. Each PARAM is a
176 pair of the form (KEY VALUE). The following KEYs are defined:
177 * `tramp-remote-shell'
178 This specifies the shell to use on the remote host. This
179 MUST be a Bourne-like shell. It is normally not necessary to
180 set this to any value other than \"/bin/sh\": Tramp wants to
181 use a shell which groks tilde expansion, but it can search
182 for it. Also note that \"/bin/sh\" exists on all Unixen,
183 this might not be true for the value that you decide to use.
184 You Have Been Warned.
185 * `tramp-remote-shell-login'
186 This specifies the arguments to let `tramp-remote-shell' run
187 as a login shell. It defaults to (\"-l\"), but some shells,
188 like ksh, require another argument. See
189 `tramp-connection-properties' for a way to overwrite the
191 * `tramp-remote-shell-args'
192 For implementation of `shell-command', this specifies the
193 arguments to let `tramp-remote-shell' run a single command.
194 * `tramp-login-program'
195 This specifies the name of the program to use for logging in to the
196 remote host. This may be the name of rsh or a workalike program,
197 or the name of telnet or a workalike, or the name of su or a workalike.
199 This specifies the list of arguments to pass to the above
200 mentioned program. Please note that this is a list of list of arguments,
201 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
202 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
203 There are some patterns: \"%h\" in this list is replaced by the host
204 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
205 port number, and \"%%\" can be used to obtain a literal percent character.
206 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
207 expansion (i.e. no host or no user specified), this list is not used as
208 argument. By this, arguments like (\"-l\" \"%u\") are optional.
209 \"%t\" is replaced by the temporary file name produced with
210 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
211 parameter of a program, if exists. \"%c\" adds additional
212 `tramp-ssh-controlmaster-options' options for the first hop.
214 A list of environment variables and their values, which will
215 be set when calling `tramp-login-program'.
217 When an asynchronous process is started, we know already that
218 the connection works. Therefore, we can pass additional
219 parameters to suppress diagnostic messages, in order not to
220 tamper the process output.
221 * `tramp-copy-program'
222 This specifies the name of the program to use for remotely copying
223 the file; this might be the absolute filename of scp or the name of
224 a workalike program. It is always applied on the local host.
226 This specifies the list of parameters to pass to the above mentioned
227 program, the hints for `tramp-login-args' also apply here.
229 A list of environment variables and their values, which will
230 be set when calling `tramp-copy-program'.
231 * `tramp-remote-copy-program'
232 The listener program to be applied on remote side, if needed.
233 * `tramp-remote-copy-args'
234 The list of parameters to pass to the listener program, the hints
235 for `tramp-login-args' also apply here. Additionally, \"%r\" could
236 be used here and in `tramp-copy-args'. It denotes a randomly
237 chosen port for the remote listener.
238 * `tramp-copy-keep-date'
239 This specifies whether the copying program when the preserves the
240 timestamp of the original file.
241 * `tramp-copy-keep-tmpfile'
242 This specifies whether a temporary local file shall be kept
243 for optimization reasons (useful for \"rsync\" methods).
244 * `tramp-copy-recursive'
245 Whether the operation copies directories recursively.
246 * `tramp-default-port'
247 The default port of a method.
249 A directory on the remote host for temporary files. If not
250 specified, \"/tmp\" is taken as default.
251 * `tramp-connection-timeout'
252 This is the maximum time to be spent for establishing a connection.
253 In general, the global default value shall be used, but for
254 some methods, like \"su\" or \"sudo\", a shorter timeout
256 * `tramp-case-insensitive'
257 Whether the remote file system handles file names case insensitive.
258 Only a non-nil value counts, the default value nil means to
259 perform further checks on the remote host. See
260 `tramp-connection-properties' for a way to overwrite this.
262 What does all this mean? Well, you should specify `tramp-login-program'
263 for all methods; this program is used to log in to the remote site. Then,
264 there are two ways to actually transfer the files between the local and the
265 remote side. One way is using an additional scp-like program. If you want
266 to do this, set `tramp-copy-program' in the method.
268 Another possibility for file transfer is inline transfer, i.e. the
269 file is passed through the same buffer used by `tramp-login-program'. In
270 this case, the file contents need to be protected since the
271 `tramp-login-program' might use escape codes or the connection might not
272 be eight-bit clean. Therefore, file contents are encoded for transit.
273 See the variables `tramp-local-coding-commands' and
274 `tramp-remote-coding-commands' for details.
276 So, to summarize: if the method is an out-of-band method, then you
277 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
278 inline method, then these two parameters should be nil.
282 When using `su' or `sudo' the phrase \"open connection to a remote
283 host\" sounds strange, but it is used nevertheless, for consistency.
284 No connection is opened to a remote host, but `su' or `sudo' is
285 started on the local host. You should specify a remote host
286 `localhost' or the name of the local host. Another host name is
287 useful only in combination with `tramp-default-proxies-alist'.")
289 (defcustom tramp-default-method
290 ;; An external copy method seems to be preferred, because it performs
291 ;; much better for large files, and it hasn't too serious delays
292 ;; for small files. But it must be ensured that there aren't
293 ;; permanent password queries. Either a password agent like
294 ;; "ssh-agent" or "Pageant" shall run, or the optional
295 ;; password-cache.el or auth-sources.el packages shall be active for
296 ;; password caching. If we detect that the user is running OpenSSH
297 ;; 4.0 or newer, we could reuse the connection, which calls also for
298 ;; an external method.
300 ;; PuTTY is installed. We don't take it, if it is installed on a
301 ;; non-windows system, or pscp from the pssh (parallel ssh) package
303 ((and (eq system-type
'windows-nt
) (executable-find "pscp")) "pscp")
304 ;; There is an ssh installation.
305 ((executable-find "scp") "scp")
308 "Default method to use for transferring files.
309 See `tramp-methods' for possibilities.
310 Also see `tramp-default-method-alist'."
316 (defcustom tramp-default-method-alist nil
317 "Default method to use for specific host/user pairs.
318 This is an alist of items (HOST USER METHOD). The first matching item
319 specifies the method to use for a file name which does not specify a
320 method. HOST and USER are regular expressions or nil, which is
321 interpreted as a regular expression which always matches. If no entry
322 matches, the variable `tramp-default-method' takes effect.
324 If the file name does not specify the user, lookup is done using the
325 empty string for the user name.
327 See `tramp-methods' for a list of possibilities for METHOD."
329 :type
'(repeat (list (choice :tag
"Host regexp" regexp sexp
)
330 (choice :tag
"User regexp" regexp sexp
)
331 (choice :tag
"Method name" string
(const nil
))))
334 (defconst tramp-default-method-marker
"-"
335 "Marker for default method in remote file names.")
337 (defcustom tramp-default-user nil
338 "Default user to use for transferring files.
339 It is nil by default; otherwise settings in configuration files like
340 \"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
342 This variable is regarded as obsolete, and will be removed soon."
344 :type
'(choice (const nil
) string
)
348 (defcustom tramp-default-user-alist nil
349 "Default user to use for specific method/host pairs.
350 This is an alist of items (METHOD HOST USER). The first matching item
351 specifies the user to use for a file name which does not specify a
352 user. METHOD and USER are regular expressions or nil, which is
353 interpreted as a regular expression which always matches. If no entry
354 matches, the variable `tramp-default-user' takes effect.
356 If the file name does not specify the method, lookup is done using the
357 empty string for the method name."
359 :type
'(repeat (list (choice :tag
"Method regexp" regexp sexp
)
360 (choice :tag
" Host regexp" regexp sexp
)
361 (choice :tag
" User name" string
(const nil
))))
364 (defcustom tramp-default-host
(system-name)
365 "Default host to use for transferring files.
366 Useful for su and sudo methods mostly."
372 (defcustom tramp-default-host-alist nil
373 "Default host to use for specific method/user pairs.
374 This is an alist of items (METHOD USER HOST). The first matching item
375 specifies the host to use for a file name which does not specify a
376 host. METHOD and HOST are regular expressions or nil, which is
377 interpreted as a regular expression which always matches. If no entry
378 matches, the variable `tramp-default-host' takes effect.
380 If the file name does not specify the method, lookup is done using the
381 empty string for the method name."
384 :type
'(repeat (list (choice :tag
"Method regexp" regexp sexp
)
385 (choice :tag
" User regexp" regexp sexp
)
386 (choice :tag
" Host name" string
(const nil
))))
389 (defcustom tramp-default-proxies-alist nil
390 "Route to be followed for specific host/user pairs.
391 This is an alist of items (HOST USER PROXY). The first matching
392 item specifies the proxy to be passed for a file name located on
393 a remote target matching USER@HOST. HOST and USER are regular
394 expressions. PROXY must be a Tramp filename without a localname
395 part. Method and user name on PROXY are optional, which is
396 interpreted with the default values. PROXY can contain the
397 patterns %h and %u, which are replaced by the strings matching
398 HOST or USER, respectively.
400 HOST, USER or PROXY could also be Lisp forms, which will be
401 evaluated. The result must be a string or nil, which is
402 interpreted as a regular expression which always matches."
404 :type
'(repeat (list (choice :tag
"Host regexp" regexp sexp
)
405 (choice :tag
"User regexp" regexp sexp
)
406 (choice :tag
" Proxy name" string
(const nil
))))
409 (defcustom tramp-save-ad-hoc-proxies nil
410 "Whether to save ad-hoc proxies persistently."
416 (defcustom tramp-restricted-shell-hosts-alist
417 (when (memq system-type
'(windows-nt))
418 (list (concat "\\`" (regexp-quote (system-name)) "\\'")))
419 "List of hosts, which run a restricted shell.
420 This is a list of regular expressions, which denote hosts running
421 a registered shell like \"rbash\". Those hosts can be used as
422 proxies only, see `tramp-default-proxies-alist'. If the local
423 host runs a registered shell, it shall be added to this list, too."
426 :type
'(repeat (regexp :tag
"Host regexp"))
430 (defconst tramp-local-host-regexp
434 (list "localhost" "localhost6" (system-name) "127.0.0.1" "::1") t
)
436 "Host names which are regarded as local host.")
438 (defvar tramp-completion-function-alist nil
439 "Alist of methods for remote files.
440 This is a list of entries of the form \(NAME PAIR1 PAIR2 ...).
441 Each NAME stands for a remote access method. Each PAIR is of the form
442 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
443 names from FILE for completion. The following predefined FUNCTIONs exists:
445 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
446 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
447 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
448 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
449 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
450 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
451 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
452 * `tramp-parse-etc-group' for \"/etc/group\" like files.
453 * `tramp-parse-netrc' for \"~/.netrc\" like files.
454 * `tramp-parse-putty' for PuTTY registered sessions.
456 FUNCTION can also be a user defined function. For more details see
459 (defconst tramp-echo-mark-marker
"_echo"
460 "String marker to surround echoed commands.")
462 (defconst tramp-echo-mark-marker-length
(length tramp-echo-mark-marker
)
463 "String length of `tramp-echo-mark-marker'.")
465 (defconst tramp-echo-mark
466 (concat tramp-echo-mark-marker
467 (make-string tramp-echo-mark-marker-length ?
\b))
468 "String mark to be transmitted around shell commands.
469 Used to separate their echo from the output they produce. This
470 will only be used if we cannot disable remote echo via stty.
471 This string must have no effect on the remote shell except for
472 producing some echo which can later be detected by
473 `tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
474 followed by an equal number of backspaces to erase them will
477 (defconst tramp-echoed-echo-mark-regexp
478 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
479 tramp-echo-mark-marker tramp-echo-mark-marker-length
)
480 "Regexp which matches `tramp-echo-mark' as it gets echoed by
483 (defcustom tramp-local-end-of-line
484 (if (memq system-type
'(windows-nt)) "\r\n" "\n")
485 "String used for end of line in local processes."
491 (defcustom tramp-rsh-end-of-line
"\n"
492 "String used for end of line in rsh connections.
493 I don't think this ever needs to be changed, so please tell me about it
494 if you need to change this."
499 (defcustom tramp-login-prompt-regexp
500 ".*\\(user\\|login\\)\\( .*\\)?: *"
501 "Regexp matching login-like prompts.
502 The regexp should match at end of buffer.
504 Sometimes the prompt is reported to look like \"login as:\"."
509 (defcustom tramp-shell-prompt-pattern
510 ;; Allow a prompt to start right after a ^M since it indeed would be
511 ;; displayed at the beginning of the line (and Zsh uses it). This
512 ;; regexp works only for GNU Emacs.
513 ;; Allow also [] style prompts. They can appear only during
514 ;; connection initialization; Tramp redefines the prompt afterwards.
515 (concat "\\(?:^\\|\r\\)"
516 "[^]#$%>\n]*#?[]#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*")
517 "Regexp to match prompts from remote shell.
518 Normally, Tramp expects you to configure `shell-prompt-pattern'
519 correctly, but sometimes it happens that you are connecting to a
520 remote host which sends a different kind of shell prompt. Therefore,
521 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
522 and also things matched by this variable. The default value of this
523 variable is similar to the default value of `shell-prompt-pattern',
524 which should work well in many cases.
526 This regexp must match both `tramp-initial-end-of-output' and
527 `tramp-end-of-output'."
532 (defcustom tramp-password-prompt-regexp
533 (format "^.*\\(%s\\).*:\^@? *"
534 ;; `password-word-equivalents' has been introduced with Emacs 24.4.
535 (regexp-opt (or (bound-and-true-p password-word-equivalents
)
536 '("password" "passphrase"))))
537 "Regexp matching password-like prompts.
538 The regexp should match at end of buffer.
540 The `sudo' program appears to insert a `^@' character into the prompt."
546 (defcustom tramp-wrong-passwd-regexp
548 ;; These strings should be on the last line
549 (regexp-opt '("Permission denied"
554 "Timeout, server not responding."
556 "Name or service not known"
557 "Host key verification failed."
558 "No supported authentication methods left to try!")
563 ;; Here comes a list of regexes, separated by \\|
564 "Received signal [0-9]+"
566 "Regexp matching a `login failed' message.
567 The regexp should match at end of buffer."
572 (defcustom tramp-yesno-prompt-regexp
574 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t
)
576 "Regular expression matching all yes/no queries which need to be confirmed.
577 The confirmation should be done with yes or no.
578 The regexp should match at end of buffer.
579 See also `tramp-yn-prompt-regexp'."
584 (defcustom tramp-yn-prompt-regexp
586 (regexp-opt '("Store key in cache? (y/n)"
587 "Update cached key? (y/n, Return cancels connection)")
590 "Regular expression matching all y/n queries which need to be confirmed.
591 The confirmation should be done with y or n.
592 The regexp should match at end of buffer.
593 See also `tramp-yesno-prompt-regexp'."
598 (defcustom tramp-terminal-prompt-regexp
602 "Terminal type\\? \\[.*\\]"
604 "Regular expression matching all terminal setting prompts.
605 The regexp should match at end of buffer.
606 The answer will be provided by `tramp-action-terminal', which see."
611 (defcustom tramp-operation-not-permitted-regexp
612 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
613 (regexp-opt '("Operation not permitted") t
))
614 "Regular expression matching keep-date problems in (s)cp operations.
615 Copying has been performed successfully already, so this message can
621 (defcustom tramp-copy-failed-regexp
623 (regexp-opt '("Permission denied"
626 "No such file or directory")
629 "Regular expression matching copy problems in (s)cp operations."
634 (defcustom tramp-process-alive-regexp
636 "Regular expression indicating a process has finished.
637 In fact this expression is empty by intention, it will be used only to
638 check regularly the status of the associated process.
639 The answer will be provided by `tramp-action-process-alive',
640 `tramp-action-out-of-band', which see."
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
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
)
664 (defcustom tramp-syntax
'default
665 "Tramp filename syntax to be used.
667 It can have the following values:
669 `default' -- Default syntax
670 `simplified' -- Ange-FTP like syntax
671 `separate' -- Syntax as defined for XEmacs originally
673 Do not change the value by `setq', it must be changed only via
674 Customize. See also `tramp-change-syntax'."
677 :package-version
'(Tramp .
"2.3.3")
678 :type
'(choice (const :tag
"Default" default
)
679 (const :tag
"Ange-FTP" simplified
)
680 (const :tag
"XEmacs" separate
))
682 :initialize
'custom-initialize-set
683 :set
'tramp-set-syntax
)
685 (defun tramp-set-syntax (symbol value
)
686 "Set SYMBOL to value VALUE.
687 Used in user option `tramp-syntax'. There are further variables
688 to be set, depending on VALUE."
689 ;; Check allowed values.
690 (unless (memq value
(tramp-syntax-values))
691 (tramp-compat-user-error "Wrong `tramp-syntax' %s" tramp-syntax
))
692 ;; Cleanup existing buffers.
693 (unless (eq (symbol-value symbol
) value
)
694 (tramp-cleanup-all-buffers))
696 (set-default symbol value
)
697 ;; Reset the depending variables.
699 (setq tramp-prefix-format
(tramp-build-prefix-format)
700 tramp-prefix-regexp
(tramp-build-prefix-regexp)
701 tramp-method-regexp
(tramp-build-method-regexp)
702 tramp-postfix-method-format
(tramp-build-postfix-method-format)
703 tramp-postfix-method-regexp
(tramp-build-postfix-method-regexp)
704 tramp-prefix-ipv6-format
(tramp-build-prefix-ipv6-format)
705 tramp-prefix-ipv6-regexp
(tramp-build-prefix-ipv6-regexp)
706 tramp-postfix-ipv6-format
(tramp-build-postfix-ipv6-format)
707 tramp-postfix-ipv6-regexp
(tramp-build-postfix-ipv6-regexp)
708 tramp-postfix-host-format
(tramp-build-postfix-host-format)
709 tramp-postfix-host-regexp
(tramp-build-postfix-host-regexp)
710 tramp-remote-file-name-spec-regexp
711 (tramp-build-remote-file-name-spec-regexp)
712 tramp-file-name-structure
(tramp-build-file-name-structure)
713 tramp-file-name-regexp
(tramp-build-file-name-regexp)
714 tramp-completion-file-name-regexp
715 (tramp-build-completion-file-name-regexp)))
716 ;; Rearrange file name handlers.
717 (tramp-register-file-name-handlers))
719 ;; Initialize the Tramp syntax variables. We want to override initial
720 ;; value of `tramp-file-name-regexp'. Other Tramp syntax variables
721 ;; must be initialized as well to proper values. We do not call
722 ;; `custom-set-variable', this would load Tramp via custom.el.
723 (eval-after-load 'tramp
724 '(tramp-set-syntax 'tramp-syntax
(tramp-compat-tramp-syntax)))
726 (defun tramp-syntax-values ()
727 "Return possible values of `tramp-syntax', a list"
728 (let ((values (cdr (get 'tramp-syntax
'custom-type
))))
729 (setq values
(mapcar 'last values
)
730 values
(mapcar 'car values
))))
732 (defun tramp-lookup-syntax (alist)
733 "Look up a syntax string in ALIST according to `tramp-compat-tramp-syntax.'
734 Raise an error if `tramp-syntax' is invalid."
735 (or (cdr (assq (tramp-compat-tramp-syntax) alist
))
736 (error "Wrong `tramp-syntax' %s" tramp-syntax
)))
738 (defconst tramp-prefix-format-alist
742 "Alist mapping Tramp syntax to strings beginning Tramp file names.")
744 (defun tramp-build-prefix-format ()
745 (tramp-lookup-syntax tramp-prefix-format-alist
))
747 (defvar tramp-prefix-format
(tramp-build-prefix-format)
748 "String matching the very beginning of Tramp file names.
749 Used in `tramp-make-tramp-file-name'.")
751 (defun tramp-build-prefix-regexp ()
752 (concat "^" (regexp-quote tramp-prefix-format
)))
754 (defvar tramp-prefix-regexp
(tramp-build-prefix-regexp)
755 "Regexp matching the very beginning of Tramp file names.
756 Should always start with \"^\". Derived from `tramp-prefix-format'.")
758 (defconst tramp-method-regexp-alist
759 '((default .
"[a-zA-Z0-9-]+")
761 (separate .
"[a-zA-Z0-9-]*"))
762 "Alist mapping Tramp syntax to regexps matching methods identifiers.")
764 (defun tramp-build-method-regexp ()
765 (tramp-lookup-syntax tramp-method-regexp-alist
))
767 (defvar tramp-method-regexp
(tramp-build-method-regexp)
768 "Regexp matching methods identifiers.
769 The `ftp' syntax does not support methods.")
771 (defconst tramp-postfix-method-format-alist
775 "Alist mapping Tramp syntax to the delimiter after the method.")
777 (defun tramp-build-postfix-method-format ()
778 (tramp-lookup-syntax tramp-postfix-method-format-alist
))
780 (defvar tramp-postfix-method-format
(tramp-build-postfix-method-format)
781 "String matching delimiter between method and user or host names.
782 The `ftp' syntax does not support methods.
783 Used in `tramp-make-tramp-file-name'.")
785 (defun tramp-build-postfix-method-regexp ()
786 (regexp-quote tramp-postfix-method-format
))
788 (defvar tramp-postfix-method-regexp
(tramp-build-postfix-method-regexp)
789 "Regexp matching delimiter between method and user or host names.
790 Derived from `tramp-postfix-method-format'.")
792 (defconst tramp-user-regexp
"[^/|: \t]+"
793 "Regexp matching user names.")
796 (defconst tramp-prefix-domain-format
"%"
797 "String matching delimiter between user and domain names.")
800 (defconst tramp-prefix-domain-regexp
(regexp-quote tramp-prefix-domain-format
)
801 "Regexp matching delimiter between user and domain names.
802 Derived from `tramp-prefix-domain-format'.")
804 (defconst tramp-domain-regexp
"[a-zA-Z0-9_.-]+"
805 "Regexp matching domain names.")
807 (defconst tramp-user-with-domain-regexp
808 (concat "\\(" tramp-user-regexp
"\\)"
809 tramp-prefix-domain-regexp
810 "\\(" tramp-domain-regexp
"\\)")
811 "Regexp matching user names with domain names.")
813 (defconst tramp-postfix-user-format
"@"
814 "String matching delimiter between user and host names.
815 Used in `tramp-make-tramp-file-name'.")
817 (defconst tramp-postfix-user-regexp
(regexp-quote tramp-postfix-user-format
)
818 "Regexp matching delimiter between user and host names.
819 Derived from `tramp-postfix-user-format'.")
821 (defconst tramp-host-regexp
"[a-zA-Z0-9_.%-]+"
822 "Regexp matching host names.")
824 (defconst tramp-prefix-ipv6-format-alist
828 "Alist mapping Tramp syntax to strings prefixing IPv6 addresses.")
830 (defun tramp-build-prefix-ipv6-format ()
831 (tramp-lookup-syntax tramp-prefix-ipv6-format-alist
))
833 (defvar tramp-prefix-ipv6-format
(tramp-build-prefix-ipv6-format)
834 "String matching left hand side of IPv6 addresses.
835 Used in `tramp-make-tramp-file-name'.")
837 (defun tramp-build-prefix-ipv6-regexp ()
838 (regexp-quote tramp-prefix-ipv6-format
))
840 (defvar tramp-prefix-ipv6-regexp
(tramp-build-prefix-ipv6-regexp)
841 "Regexp matching left hand side of IPv6 addresses.
842 Derived from `tramp-prefix-ipv6-format'.")
844 ;; The following regexp is a bit sloppy. But it shall serve our
845 ;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
846 ;; "::ffff:192.168.0.1".
847 (defconst tramp-ipv6-regexp
"\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
848 "Regexp matching IPv6 addresses.")
850 (defconst tramp-postfix-ipv6-format-alist
854 "Alist mapping Tramp syntax to suffix for IPv6 addresses.")
856 (defun tramp-build-postfix-ipv6-format ()
857 (tramp-lookup-syntax tramp-postfix-ipv6-format-alist
))
859 (defvar tramp-postfix-ipv6-format
(tramp-build-postfix-ipv6-format)
860 "String matching right hand side of IPv6 addresses.
861 Used in `tramp-make-tramp-file-name'.")
863 (defun tramp-build-postfix-ipv6-regexp ()
864 (regexp-quote tramp-postfix-ipv6-format
))
866 (defvar tramp-postfix-ipv6-regexp
(tramp-build-postfix-ipv6-regexp)
867 "Regexp matching right hand side of IPv6 addresses.
868 Derived from `tramp-postfix-ipv6-format'.")
870 (defconst tramp-prefix-port-format
"#"
871 "String matching delimiter between host names and port numbers.")
873 (defconst tramp-prefix-port-regexp
(regexp-quote tramp-prefix-port-format
)
874 "Regexp matching delimiter between host names and port numbers.
875 Derived from `tramp-prefix-port-format'.")
877 (defconst tramp-port-regexp
"[0-9]+"
878 "Regexp matching port numbers.")
880 (defconst tramp-host-with-port-regexp
881 (concat "\\(" tramp-host-regexp
"\\)"
882 tramp-prefix-port-regexp
883 "\\(" tramp-port-regexp
"\\)")
884 "Regexp matching host names with port numbers.")
886 (defconst tramp-postfix-hop-format
"|"
887 "String matching delimiter after ad-hoc hop definitions.")
889 (defconst tramp-postfix-hop-regexp
(regexp-quote tramp-postfix-hop-format
)
890 "Regexp matching delimiter after ad-hoc hop definitions.
891 Derived from `tramp-postfix-hop-format'.")
893 (defconst tramp-postfix-host-format-alist
897 "Alist mapping Tramp syntax to strings between host and local names.")
899 (defun tramp-build-postfix-host-format ()
900 (tramp-lookup-syntax tramp-postfix-host-format-alist
))
902 (defvar tramp-postfix-host-format
(tramp-build-postfix-host-format)
903 "String matching delimiter between host names and localnames.
904 Used in `tramp-make-tramp-file-name'.")
906 (defun tramp-build-postfix-host-regexp ()
907 (regexp-quote tramp-postfix-host-format
))
909 (defvar tramp-postfix-host-regexp
(tramp-build-postfix-host-regexp)
910 "Regexp matching delimiter between host names and localnames.
911 Derived from `tramp-postfix-host-format'.")
913 (defconst tramp-localname-regexp
".*$"
914 "Regexp matching localnames.")
916 (defconst tramp-unknown-id-string
"UNKNOWN"
917 "String used to denote an unknown user or group")
919 (defconst tramp-unknown-id-integer -
1
920 "Integer used to denote an unknown user or group")
922 ;;; File name format:
924 (defun tramp-build-remote-file-name-spec-regexp ()
925 "Construct a regexp matching a Tramp file name for a Tramp syntax.
926 It is expected, that `tramp-syntax' has the proper value."
928 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
929 "\\(?:" "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
"\\)?"
930 "\\(" "\\(?:" tramp-host-regexp
"\\|"
931 tramp-prefix-ipv6-regexp
"\\(?:" tramp-ipv6-regexp
"\\)?"
932 tramp-postfix-ipv6-regexp
"\\)"
933 "\\(?:" tramp-prefix-port-regexp tramp-port-regexp
"\\)?" "\\)?"))
935 (defvar tramp-remote-file-name-spec-regexp
936 (tramp-build-remote-file-name-spec-regexp)
937 "Regular expression matching a Tramp file name between prefix and postfix.")
939 (defun tramp-build-file-name-structure ()
940 "Construct the Tramp file name structure for a Tramp syntax.
941 It is expected, that `tramp-syntax' has the proper value.
942 See `tramp-file-name-structure'."
946 "\\(" "\\(?:" tramp-remote-file-name-spec-regexp
947 tramp-postfix-hop-regexp
"\\)+" "\\)?"
948 tramp-remote-file-name-spec-regexp tramp-postfix-host-regexp
949 "\\(" tramp-localname-regexp
"\\)")
952 (defvar tramp-file-name-structure
(tramp-build-file-name-structure)
953 "List of six elements (REGEXP METHOD USER HOST FILE HOP), detailing \
954 the Tramp file name structure.
956 The first element REGEXP is a regular expression matching a Tramp file
957 name. The regex should contain parentheses around the method name,
958 the user name, the host name, and the file name parts.
960 The second element METHOD is a number, saying which pair of
961 parentheses matches the method name. The third element USER is
962 similar, but for the user name. The fourth element HOST is similar,
963 but for the host name. The fifth element FILE is for the file name.
964 The last element HOP is the ad-hoc hop definition, which could be a
965 cascade of several hops.
967 These numbers are passed directly to `match-string', which see. That
968 means the opening parentheses are counted to identify the pair.
970 See also `tramp-file-name-regexp'.")
972 (defun tramp-build-file-name-regexp ()
973 (car tramp-file-name-structure
))
976 (defconst tramp-initial-file-name-regexp
"\\`/.+:.*:"
977 "Value for `tramp-file-name-regexp' for autoload.
978 It must match the initial `tramp-syntax' settings.")
981 (defvar tramp-file-name-regexp tramp-initial-file-name-regexp
982 "Regular expression matching file names handled by Tramp.
983 This regexp should match Tramp file names but no other file
984 names. When calling `tramp-register-file-name-handlers', the
985 initial value is overwritten by the car of `tramp-file-name-structure'.")
987 (defconst tramp-completion-file-name-regexp-default
990 ;; Optional multi hop.
991 "\\([^/|:]+:[^/|:]*|\\)*"
993 (if (memq system-type
'(cygwin windows-nt
))
994 ;; The method is either "-", or at least two characters.
995 "\\(-\\|[^/|:]\\{2,\\}\\)"
996 ;; At least one character for method.
998 ;; Method separator, user name and host name.
1001 "Value for `tramp-completion-file-name-regexp' for default remoting.
1002 See `tramp-file-name-structure' for more explanations.
1004 On W32 systems, the volume letter must be ignored.")
1006 (defconst tramp-completion-file-name-regexp-simplified
1009 ;; Optional multi hop.
1012 (if (memq system-type
'(cygwin windows-nt
))
1013 ;; At least two characters.
1015 ;; At least one character.
1018 "Value for `tramp-completion-file-name-regexp' for simplified style remoting.
1019 See `tramp-file-name-structure' for more explanations.
1021 On W32 systems, the volume letter must be ignored.")
1023 (defconst tramp-completion-file-name-regexp-separate
1024 "\\`/\\(\\[[^]]*\\)?\\'"
1025 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1026 See `tramp-file-name-structure' for more explanations.")
1028 (defconst tramp-completion-file-name-regexp-alist
1029 `((default .
,tramp-completion-file-name-regexp-default
)
1030 (simplified .
,tramp-completion-file-name-regexp-simplified
)
1031 (separate .
,tramp-completion-file-name-regexp-separate
))
1032 "Alist mapping incomplete Tramp file names.")
1034 (defun tramp-build-completion-file-name-regexp ()
1035 (tramp-lookup-syntax tramp-completion-file-name-regexp-alist
))
1037 (defvar tramp-completion-file-name-regexp
1038 (tramp-build-completion-file-name-regexp)
1039 "Regular expression matching file names handled by Tramp completion.
1040 This regexp should match partial Tramp file names only.
1042 Please note that the entry in `file-name-handler-alist' is made when
1043 this file \(tramp.el) is loaded. This means that this variable must be set
1044 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1045 updated after changing this variable.
1047 Also see `tramp-file-name-structure'.")
1050 (defconst tramp-autoload-file-name-regexp
1053 (if (memq system-type
'(cygwin windows-nt
))
1054 ;; The method is either "-", or at least two characters.
1055 "\\(-\\|[^/|:]\\{2,\\}\\)"
1056 ;; At least one character for method.
1059 "Regular expression matching file names handled by Tramp autoload.
1060 It must match the initial `tramp-syntax' settings. It should not
1061 match file names at root of the underlying local file system,
1062 like \"/sys\" or \"/C:\".")
1064 ;; Chunked sending kludge. We set this to 500 for black-listed constellations
1065 ;; known to have a bug in `process-send-string'; some ssh connections appear
1066 ;; to drop bytes when data is sent too quickly. There is also a connection
1067 ;; buffer local variable, which is computed depending on remote host properties
1068 ;; when `tramp-chunksize' is zero or nil.
1069 (defcustom tramp-chunksize
(when (memq system-type
'(hpux)) 500)
1070 ;; Parentheses in docstring starting at beginning of line are escaped.
1071 ;; Fontification is messed up when
1072 ;; `open-paren-in-column-0-is-defun-start' set to t.
1073 "If non-nil, chunksize for sending input to local process.
1074 It is necessary only on systems which have a buggy `process-send-string'
1075 implementation. The necessity, whether this variable must be set, can be
1076 checked via the following code:
1079 (let* ((user \"xxx\") (host \"yyy\")
1081 (sent init) (received init))
1082 (while (= sent received)
1083 (setq sent (+ sent step))
1085 (let ((proc (start-process (buffer-name) (current-buffer)
1086 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1087 (when (process-live-p proc)
1088 (process-send-string proc (make-string sent ?\\ ))
1089 (process-send-eof proc)
1090 (process-send-eof proc))
1091 (while (not (progn (goto-char (point-min))
1092 (re-search-forward \"\\\\w+\" (point-max) t)))
1093 (accept-process-output proc 1))
1094 (when (process-live-p proc)
1095 (setq received (string-to-number (match-string 0)))
1096 (delete-process proc)
1097 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1099 (if (> sent (+ init step))
1100 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1102 (message \"Test does not work\")
1103 (display-buffer (current-buffer))
1106 In the Emacs normally running Tramp, evaluate the above code
1107 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1108 respectively). You can do this, for example, by pasting it into
1109 the `*scratch*' buffer and then hitting C-j with the cursor after the
1110 last closing parenthesis. Note that it works only if you have configured
1111 \"ssh\" to run without password query, see ssh-agent(1).
1113 You will see the number of bytes sent successfully to the remote host.
1114 If that number exceeds 1000, you can stop the execution by hitting
1115 C-g, because your Emacs is likely clean.
1117 When it is necessary to set `tramp-chunksize', you might consider to
1118 use an out-of-the-band method \(like \"scp\") instead of an internal one
1119 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1122 If your Emacs is buggy, the code stops and gives you an indication
1123 about the value `tramp-chunksize' should be set. Maybe you could just
1124 experiment a bit, e.g. changing the values of `init' and `step'
1125 in the third line of the code.
1127 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1128 this variable to be set as well."
1130 :type
'(choice (const nil
) integer
)
1133 ;; Logging in to a remote host normally requires obtaining a pty. But
1134 ;; Emacs on macOS has process-connection-type set to nil by default,
1135 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1136 ;; for an override of the system default.
1137 (defcustom tramp-process-connection-type t
1138 "Overrides `process-connection-type' for connections from Tramp.
1139 Tramp binds `process-connection-type' to the value given here before
1140 opening a connection to a remote host."
1142 :type
'(choice (const nil
) (const t
) (const pty
))
1145 (defcustom tramp-connection-timeout
60
1146 "Defines the max time to wait for establishing a connection (in seconds).
1147 This can be overwritten for different connection types in `tramp-methods'.
1149 The timeout does not include the time reading a password."
1155 (defcustom tramp-connection-min-time-diff
5
1156 "Defines seconds between two consecutive connection attempts.
1157 This is necessary as self defense mechanism, in order to avoid
1158 yo-yo connection attempts when the remote host is unavailable.
1160 A value of 0 or nil suppresses this check. This might be
1161 necessary, when several out-of-order copy operations are
1162 performed, or when several asynchronous processes will be started
1163 in a short time frame. In those cases it is recommended to
1164 let-bind this variable."
1167 :type
'(choice (const nil
) integer
)
1170 (defcustom tramp-completion-reread-directory-timeout
10
1171 "Defines seconds since last remote command before rereading a directory.
1172 A remote directory might have changed its contents. In order to
1173 make it visible during file name completion in the minibuffer,
1174 Tramp flushes its cache and rereads the directory contents when
1175 more than `tramp-completion-reread-directory-timeout' seconds
1176 have been gone since last remote command execution. A value of t
1177 would require an immediate reread during filename completion, nil
1178 means to use always cached values for the directory contents."
1180 :type
'(choice (const nil
) (const t
) integer
)
1183 ;;; Internal Variables:
1185 (defvar tramp-current-connection nil
1186 "Last connection timestamp.")
1188 (defconst tramp-completion-file-name-handler-alist
1189 '((file-name-all-completions
1190 . tramp-completion-handle-file-name-all-completions
)
1191 (file-name-completion . tramp-completion-handle-file-name-completion
))
1192 "Alist of completion handler functions.
1193 Used for file names matching `tramp-completion-file-name-regexp'.
1194 Operations not mentioned here will be handled by Tramp's file
1195 name handler functions, or the normal Emacs functions.")
1197 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1198 ;;;###tramp-autoload
1199 (defvar tramp-foreign-file-name-handler-alist nil
1200 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1201 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1204 ;;; Internal functions which must come first:
1206 ;; Conversion functions between external representation and
1207 ;; internal data structure. Convenience functions for internal
1210 ;; The basic structure for remote file names. We use a list :type,
1211 ;; in order to be compatible with Emacs 24 and 25.
1212 (cl-defstruct (tramp-file-name (:type list
) :named
)
1213 method user domain host port localname hop
)
1215 (defun tramp-file-name-user-domain (vec)
1216 "Return user and domain components of VEC."
1217 (when (or (tramp-file-name-user vec
) (tramp-file-name-domain vec
))
1218 (concat (tramp-file-name-user vec
)
1219 (and (tramp-file-name-domain vec
)
1220 tramp-prefix-domain-format
)
1221 (tramp-file-name-domain vec
))))
1223 (defun tramp-file-name-host-port (vec)
1224 "Return host and port components of VEC."
1225 (when (or (tramp-file-name-host vec
) (tramp-file-name-port vec
))
1226 (concat (tramp-file-name-host vec
)
1227 (and (tramp-file-name-port vec
)
1228 tramp-prefix-port-format
)
1229 (tramp-file-name-port vec
))))
1231 (defun tramp-file-name-port-or-default (vec)
1232 "Return port component of VEC.
1233 If nil, return `tramp-default-port'."
1234 (or (tramp-file-name-port vec
)
1235 (tramp-get-method-parameter vec
'tramp-default-port
)))
1237 (defun tramp-file-name-equal-p (vec1 vec2
)
1238 "Check, whether VEC1 and VEC2 denote the same `tramp-file-name'."
1239 (and (tramp-file-name-p vec1
) (tramp-file-name-p vec2
)
1240 (string-equal (tramp-file-name-method vec1
)
1241 (tramp-file-name-method vec2
))
1242 (string-equal (tramp-file-name-user-domain vec1
)
1243 (tramp-file-name-user-domain vec2
))
1244 (string-equal (tramp-file-name-host-port vec1
)
1245 (tramp-file-name-host-port vec2
))))
1247 (defun tramp-get-method-parameter (vec param
)
1248 "Return the method parameter PARAM.
1249 If VEC is a vector, check first in connection properties.
1250 Afterwards, check in `tramp-methods'. If the `tramp-methods'
1251 entry does not exist, return nil."
1253 (replace-regexp-in-string "^tramp-" "" (symbol-name param
))))
1254 (if (tramp-connection-property-p vec hash-entry
)
1255 ;; We use the cached property.
1256 (tramp-get-connection-property vec hash-entry nil
)
1257 ;; Use the static value from `tramp-methods'.
1258 (let ((methods-entry
1259 (assoc param
(assoc (tramp-file-name-method vec
) tramp-methods
))))
1260 (when methods-entry
(cadr methods-entry
))))))
1262 ;; The localname can be quoted with "/:". Extract this.
1263 (defun tramp-file-name-unquote-localname (vec)
1264 "Return unquoted localname component of VEC."
1265 (tramp-compat-file-name-unquote (tramp-file-name-localname vec
)))
1267 ;;;###tramp-autoload
1268 (defun tramp-tramp-file-p (name)
1269 "Return t if NAME is a string with Tramp file name syntax."
1271 ;; No "/:" and "/c:". This is not covered by `tramp-file-name-regexp'.
1272 (not (string-match-p
1273 (if (memq system-type
'(cygwin windows-nt
))
1274 "^/[[:alpha:]]?:" "^/:")
1276 (string-match-p tramp-file-name-regexp name
)
1279 (defun tramp-find-method (method user host
)
1280 "Return the right method string to use.
1281 This is METHOD, if non-nil. Otherwise, do a lookup in
1282 `tramp-default-method-alist'."
1284 (or (string-equal method
"")
1285 (string-equal method tramp-default-method-marker
)))
1289 (let ((choices tramp-default-method-alist
)
1292 (setq item
(pop choices
))
1293 (when (and (string-match (or (nth 0 item
) "") (or host
""))
1294 (string-match (or (nth 1 item
) "") (or user
"")))
1295 (setq lmethod
(nth 2 item
))
1296 (setq choices nil
)))
1298 tramp-default-method
)))
1299 ;; We must mark, whether a default value has been used.
1300 (if (or method
(null result
))
1302 (propertize result
'tramp-default t
))))
1304 (defun tramp-find-user (method user host
)
1305 "Return the right user string to use.
1306 This is USER, if non-nil. Otherwise, do a lookup in
1307 `tramp-default-user-alist'."
1310 (let ((choices tramp-default-user-alist
)
1313 (setq item
(pop choices
))
1314 (when (and (string-match (or (nth 0 item
) "") (or method
""))
1315 (string-match (or (nth 1 item
) "") (or host
"")))
1316 (setq luser
(nth 2 item
))
1317 (setq choices nil
)))
1319 tramp-default-user
)))
1320 ;; We must mark, whether a default value has been used.
1321 (if (or user
(null result
))
1323 (propertize result
'tramp-default t
))))
1325 (defun tramp-find-host (method user host
)
1326 "Return the right host string to use.
1327 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
1328 (or (and (> (length host
) 0) host
)
1329 (let ((choices tramp-default-host-alist
)
1332 (setq item
(pop choices
))
1333 (when (and (string-match (or (nth 0 item
) "") (or method
""))
1334 (string-match (or (nth 1 item
) "") (or user
"")))
1335 (setq lhost
(nth 2 item
))
1336 (setq choices nil
)))
1338 tramp-default-host
))
1340 (defun tramp-dissect-file-name (name &optional nodefault
)
1341 "Return a `tramp-file-name' structure.
1342 The structure consists of remote method, remote user, remote host,
1343 localname (file name on remote host) and hop. If NODEFAULT is
1344 non-nil, the file name parts are not expanded to their default
1347 (unless (tramp-tramp-file-p name
)
1348 (tramp-compat-user-error nil
"Not a Tramp file name: \"%s\"" name
))
1349 (if (not (string-match (nth 0 tramp-file-name-structure
) name
))
1350 (error "`tramp-file-name-structure' didn't match!")
1351 (let ((method (match-string (nth 1 tramp-file-name-structure
) name
))
1352 (user (match-string (nth 2 tramp-file-name-structure
) name
))
1353 (host (match-string (nth 3 tramp-file-name-structure
) name
))
1354 (localname (match-string (nth 4 tramp-file-name-structure
) name
))
1355 (hop (match-string (nth 5 tramp-file-name-structure
) name
))
1358 (when (string-match tramp-user-with-domain-regexp user
)
1359 (setq domain
(match-string 2 user
)
1360 user
(match-string 1 user
))))
1363 (when (string-match tramp-host-with-port-regexp host
)
1364 (setq port
(match-string 2 host
)
1365 host
(match-string 1 host
)))
1366 (when (string-match tramp-prefix-ipv6-regexp host
)
1367 (setq host
(replace-match "" nil t host
)))
1368 (when (string-match tramp-postfix-ipv6-regexp host
)
1369 (setq host
(replace-match "" nil t host
))))
1372 (setq method
(tramp-find-method method user host
)
1373 user
(tramp-find-user method user host
)
1374 host
(tramp-find-host method user host
)))
1376 (make-tramp-file-name
1377 :method method
:user user
:domain domain
:host host
:port port
1378 :localname localname
:hop hop
)))))
1380 (defun tramp-buffer-name (vec)
1381 "A name for the connection buffer VEC."
1382 (let ((method (tramp-file-name-method vec
))
1383 (user-domain (tramp-file-name-user-domain vec
))
1384 (host-port (tramp-file-name-host-port vec
)))
1385 (if (not (zerop (length user-domain
)))
1386 (format "*tramp/%s %s@%s*" method user-domain host-port
)
1387 (format "*tramp/%s %s*" method host-port
))))
1389 (defun tramp-make-tramp-file-name (&rest args
)
1390 "Construct a Tramp file name from ARGS.
1392 ARGS could have two different signatures. The first one is of
1393 type (VEC &optional LOCALNAME HOP).
1394 If LOCALNAME is nil, the value in VEC is used. If it is a
1395 symbol, a null localname will be used. Otherwise, LOCALNAME is
1396 expected to be a string, which will be used.
1397 If HOP is nil, the value in VEC is used. If it is a symbol, a
1398 null hop will be used. Otherwise, HOP is expected to be a
1399 string, which will be used.
1401 The other signature exists for backward compatibility. It has
1402 the form (METHOD USER DOMAIN HOST PORT LOCALNAME &optional HOP)."
1403 (let (method user domain host port localname hop
)
1405 ((tramp-file-name-p (car args
))
1406 (setq method
(tramp-file-name-method (car args
))
1407 user
(tramp-file-name-user (car args
))
1408 domain
(tramp-file-name-domain (car args
))
1409 host
(tramp-file-name-host (car args
))
1410 port
(tramp-file-name-port (car args
))
1411 localname
(tramp-file-name-localname (car args
))
1412 hop
(tramp-file-name-hop (car args
)))
1414 (setq localname
(and (stringp (cadr args
)) (cadr args
))))
1415 (when (cl-caddr args
)
1416 (setq hop
(and (stringp (cl-caddr args
)) (cl-caddr args
)))))
1418 (t (setq method
(nth 0 args
)
1423 localname
(nth 5 args
)
1426 (concat tramp-prefix-format hop
1427 (unless (or (zerop (length method
))
1428 (zerop (length tramp-postfix-method-format
)))
1429 (concat method tramp-postfix-method-format
))
1431 (unless (zerop (length domain
))
1432 (concat tramp-prefix-domain-format domain
))
1433 (unless (zerop (length user
))
1434 tramp-postfix-user-format
)
1436 (if (string-match tramp-ipv6-regexp host
)
1438 tramp-prefix-ipv6-format host tramp-postfix-ipv6-format
)
1440 (unless (zerop (length port
))
1441 (concat tramp-prefix-port-format port
))
1442 tramp-postfix-host-format
1445 (defun tramp-completion-make-tramp-file-name (method user host localname
)
1446 "Construct a Tramp file name from METHOD, USER, HOST and LOCALNAME.
1447 It must not be a complete Tramp file name, but as long as there are
1448 necessary only. This function will be used in file name completion."
1449 (concat tramp-prefix-format
1450 (unless (or (zerop (length method
))
1451 (zerop (length tramp-postfix-method-format
)))
1452 (concat method tramp-postfix-method-format
))
1453 (unless (zerop (length user
))
1454 (concat user tramp-postfix-user-format
))
1455 (unless (zerop (length host
))
1457 (if (string-match tramp-ipv6-regexp host
)
1459 tramp-prefix-ipv6-format host tramp-postfix-ipv6-format
)
1461 tramp-postfix-host-format
))
1462 (when localname localname
)))
1464 (defun tramp-get-buffer (vec)
1465 "Get the connection buffer to be used for VEC."
1466 (or (get-buffer (tramp-buffer-name vec
))
1467 (with-current-buffer (get-buffer-create (tramp-buffer-name vec
))
1468 ;; We use the existence of connection property "process-buffer"
1469 ;; as indication, whether a connection is active.
1470 (tramp-set-connection-property
1471 vec
"process-buffer"
1472 (tramp-get-connection-property vec
"process-buffer" nil
))
1473 (setq buffer-undo-list t
1474 default-directory
(tramp-make-tramp-file-name vec
"/" 'nohop
))
1477 (defun tramp-get-connection-buffer (vec)
1478 "Get the connection buffer to be used for VEC.
1479 In case a second asynchronous communication has been started, it is different
1480 from `tramp-get-buffer'."
1481 (or (tramp-get-connection-property vec
"process-buffer" nil
)
1482 (tramp-get-buffer vec
)))
1484 (defun tramp-get-connection-name (vec)
1485 "Get the connection name to be used for VEC.
1486 In case a second asynchronous communication has been started, it is different
1487 from the default one."
1488 (or (tramp-get-connection-property vec
"process-name" nil
)
1489 (tramp-buffer-name vec
)))
1491 (defun tramp-get-connection-process (vec)
1492 "Get the connection process to be used for VEC.
1493 In case a second asynchronous communication has been started, it is different
1494 from the default one."
1495 (and (tramp-file-name-p vec
) (get-process (tramp-get-connection-name vec
))))
1497 (defun tramp-set-connection-local-variables (vec)
1498 "Set connection-local variables in the connection buffer used for VEC.
1499 If connection-local variables are not supported by this Emacs
1500 version, the function does nothing."
1501 (with-current-buffer (tramp-get-connection-buffer vec
)
1502 ;; `hack-connection-local-variables-apply' exists since Emacs 26.1.
1503 (tramp-compat-funcall
1504 'hack-connection-local-variables-apply
1505 `(:application tramp
1506 :protocol
,(tramp-file-name-method vec
)
1507 :user
,(tramp-file-name-user-domain vec
)
1508 :machine
,(tramp-file-name-host-port vec
)))))
1510 (defun tramp-set-connection-local-variables-for-buffer ()
1511 "Set connection-local variables in the current buffer.
1512 If connection-local variables are not supported by this Emacs
1513 version, the function does nothing."
1514 (when (file-remote-p default-directory
)
1515 ;; `hack-connection-local-variables-apply' exists since Emacs 26.1.
1516 (tramp-compat-funcall
1517 'hack-connection-local-variables-apply
1518 `(:application tramp
1519 :protocol
,(file-remote-p default-directory
'method
)
1520 :user
,(file-remote-p default-directory
'user
)
1521 :machine
,(file-remote-p default-directory
'host
)))))
1523 (defun tramp-debug-buffer-name (vec)
1524 "A name for the debug buffer for VEC."
1525 (let ((method (tramp-file-name-method vec
))
1526 (user-domain (tramp-file-name-user-domain vec
))
1527 (host-port (tramp-file-name-host-port vec
)))
1528 (if (not (zerop (length user-domain
)))
1529 (format "*debug tramp/%s %s@%s*" method user-domain host-port
)
1530 (format "*debug tramp/%s %s*" method host-port
))))
1532 (defconst tramp-debug-outline-regexp
1533 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #"
1534 "Used for highlighting Tramp debug buffers in `outline-mode'.")
1536 (defun tramp-debug-outline-level ()
1537 "Return the depth to which a statement is nested in the outline.
1538 Point must be at the beginning of a header line.
1540 The outline level is equal to the verbosity of the Tramp message."
1541 (1+ (string-to-number (match-string 1))))
1543 (defun tramp-get-debug-buffer (vec)
1544 "Get the debug buffer for VEC."
1545 (with-current-buffer
1546 (get-buffer-create (tramp-debug-buffer-name vec
))
1548 (setq buffer-undo-list t
)
1549 ;; So it does not get loaded while `outline-regexp' is let-bound.
1551 ;; Activate `outline-mode'. This runs `text-mode-hook' and
1552 ;; `outline-mode-hook'. We must prevent that local processes
1553 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
1554 ;; Furthermore, `outline-regexp' must have the correct value
1555 ;; already, because it is used by `font-lock-compile-keywords'.
1556 (let ((default-directory (tramp-compat-temporary-file-directory))
1557 (outline-regexp tramp-debug-outline-regexp
))
1559 (set (make-local-variable 'outline-regexp
) tramp-debug-outline-regexp
)
1560 (set (make-local-variable 'outline-level
) 'tramp-debug-outline-level
))
1563 (defsubst tramp-debug-message
(vec fmt-string
&rest arguments
)
1564 "Append message to debug buffer.
1565 Message is formatted with FMT-STRING as control string and the remaining
1566 ARGUMENTS to actually emit the message (if applicable)."
1567 (with-current-buffer (tramp-get-debug-buffer vec
)
1568 (goto-char (point-max))
1573 ";; Emacs: %s Tramp: %s -*- mode: outline; -*-"
1574 emacs-version tramp-version
))
1575 (when (>= tramp-verbose
10)
1578 "\n;; Location: %s Git: %s"
1579 (locate-library "tramp") (tramp-repository-get-version)))))
1583 (let ((now (current-time)))
1584 (insert (format-time-string "%T." now
))
1585 (insert (format "%06d " (nth 2 now
))))
1586 ;; Calling Tramp function. We suppress compat and trace functions
1587 ;; from being displayed.
1588 (let ((btn 1) btf fn
)
1590 (setq btf
(nth 1 (backtrace-frame btn
)))
1594 (setq fn
(symbol-name btf
))
1597 (string-match "^tramp" fn
)
1604 "tramp-compat-funcall"
1605 "tramp-compat-user-error"
1606 "tramp-condition-case-unless-debug"
1607 "tramp-debug-message"
1609 "tramp-error-with-buffer"
1615 (setq btn
(1+ btn
))))
1616 ;; The following code inserts filename and line number. Should
1617 ;; be inactive by default, because it is time consuming.
1618 ; (let ((ffn (find-function-noselect (intern fn))))
1622 ; (file-name-nondirectory (buffer-file-name (car ffn)))
1623 ; (with-current-buffer (car ffn)
1624 ; (1+ (count-lines (point-min) (cdr ffn)))))))
1625 (insert (format "%s " fn
)))
1627 (insert (apply #'format-message fmt-string arguments
))))
1629 (defvar tramp-message-show-message t
1630 "Show Tramp message in the minibuffer.
1631 This variable is used to disable messages from `tramp-error'.
1632 The messages are visible anyway, because an error is raised.")
1634 (defsubst tramp-message
(vec-or-proc level fmt-string
&rest arguments
)
1635 "Emit a message depending on verbosity level.
1636 VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
1637 vector or a process. LEVEL says to be quiet if `tramp-verbose' is
1638 less than LEVEL. The message is emitted only if `tramp-verbose' is
1639 greater than or equal to LEVEL.
1641 The message is also logged into the debug buffer when `tramp-verbose'
1642 is greater than or equal 4.
1644 Calls functions `message' and `tramp-debug-message' with FMT-STRING as
1645 control string and the remaining ARGUMENTS to actually emit the message (if
1648 (when (<= level tramp-verbose
)
1649 ;; Match data must be preserved!
1651 ;; Display only when there is a minimum level.
1652 (when (and tramp-message-show-message
(<= level
3))
1658 ((= level
2) "Warning: ")
1662 ;; Log only when there is a minimum level.
1663 (when (>= tramp-verbose
4)
1664 ;; Translate proc to vec.
1665 (when (processp vec-or-proc
)
1666 (let ((tramp-verbose 0))
1668 (tramp-get-connection-property vec-or-proc
"vector" nil
))))
1669 ;; Append connection buffer for error messages.
1671 (let ((tramp-verbose 0))
1672 (with-current-buffer (tramp-get-connection-buffer vec-or-proc
)
1673 (setq fmt-string
(concat fmt-string
"\n%s")
1674 arguments
(append arguments
(list (buffer-string)))))))
1676 (when (tramp-file-name-p vec-or-proc
)
1677 (apply 'tramp-debug-message
1679 (concat (format "(%d) # " level
) fmt-string
)
1682 (defsubst tramp-backtrace
(&optional vec-or-proc
)
1683 "Dump a backtrace into the debug buffer.
1684 If VEC-OR-PROC is nil, the buffer *debug tramp* is used. This
1685 function is meant for debugging purposes."
1687 (tramp-message vec-or-proc
10 "\n%s" (with-output-to-string (backtrace)))
1688 (if (>= tramp-verbose
10)
1689 (with-output-to-temp-buffer "*debug tramp*" (backtrace)))))
1691 (defsubst tramp-error
(vec-or-proc signal fmt-string
&rest arguments
)
1693 VEC-OR-PROC identifies the connection to use, SIGNAL is the
1694 signal identifier to be raised, remaining arguments passed to
1695 `tramp-message'. Finally, signal SIGNAL is raised."
1696 (let (tramp-message-show-message)
1697 (tramp-backtrace vec-or-proc
)
1699 ;; FMT-STRING could be just a file name, as in
1700 ;; `file-already-exists' errors. It could contain the ?\%
1701 ;; character, as in smb domain spec.
1702 (setq arguments
(list fmt-string
)
1707 (error-message-string
1709 (get signal
'error-message
)
1710 (apply #'format-message fmt-string arguments
)))))
1711 (signal signal
(list (apply #'format-message fmt-string arguments
)))))
1713 (defsubst tramp-error-with-buffer
1714 (buf vec-or-proc signal fmt-string
&rest arguments
)
1715 "Emit an error, and show BUF.
1716 If BUF is nil, show the connection buf. Wait for 30\", or until
1717 an input event arrives. The other arguments are passed to `tramp-error'."
1718 (save-window-excursion
1719 (let* ((buf (or (and (bufferp buf
) buf
)
1720 (and (processp vec-or-proc
) (process-buffer vec-or-proc
))
1721 (and (tramp-file-name-p vec-or-proc
)
1722 (tramp-get-connection-buffer vec-or-proc
))))
1723 (vec (or (and (tramp-file-name-p vec-or-proc
) vec-or-proc
)
1724 (and buf
(with-current-buffer buf
1725 (tramp-dissect-file-name default-directory
))))))
1727 (apply 'tramp-error vec-or-proc signal fmt-string arguments
)
1730 tramp-message-show-message
1731 (not (zerop tramp-verbose
))
1732 ;; Do not show when flagged from outside.
1733 (not (tramp-completion-mode-p))
1734 ;; Show only when Emacs has started already.
1736 (let ((enable-recursive-minibuffers t
))
1737 ;; `tramp-error' does not show messages. So we must do it
1739 (apply 'message fmt-string arguments
)
1744 ;; Reset timestamp. It would be wrong after waiting for a while.
1745 (when (tramp-file-name-equal-p vec
(car tramp-current-connection
))
1746 (setcdr tramp-current-connection
(current-time)))))))
1748 (defmacro tramp-with-demoted-errors
(vec-or-proc format
&rest body
)
1749 "Execute BODY while redirecting the error message to `tramp-message'.
1750 BODY is executed like wrapped by `with-demoted-errors'. FORMAT
1751 is a format-string containing a %-sequence meaning to substitute
1752 the resulting error message."
1753 (declare (debug (symbolp body
))
1755 (let ((err (make-symbol "err")))
1756 `(condition-case-unless-debug ,err
1758 (error (tramp-message ,vec-or-proc
3 ,format
,err
) nil
))))
1760 (defmacro with-parsed-tramp-file-name
(filename var
&rest body
)
1761 "Parse a Tramp filename and make components available in the body.
1763 First arg FILENAME is evaluated and dissected into its components.
1764 Second arg VAR is a symbol. It is used as a variable name to hold
1765 the filename structure. It is also used as a prefix for the variables
1766 holding the components. For example, if VAR is the symbol `foo', then
1767 `foo' will be bound to the whole structure, `foo-method' will be bound to
1768 the method component, and so on for `foo-user', `foo-domain', `foo-host',
1769 `foo-port', `foo-localname', `foo-hop'.
1771 Remaining args are Lisp expressions to be evaluated (inside an implicit
1774 If VAR is nil, then we bind `v' to the structure and `method', `user',
1775 `domain', `host', `port', `localname', `hop' to the components."
1777 (mapcar (lambda (elem)
1778 `(,(if var
(intern (format "%s-%s" var elem
)) elem
)
1779 (,(intern (format "tramp-file-name-%s" elem
))
1781 `,(tramp-compat-tramp-file-name-slots))))
1782 `(let* ((,(or var
'v
) (tramp-dissect-file-name ,filename
))
1784 ;; We don't know which of those vars will be used, so we bind them all,
1785 ;; and then add here a dummy use of all those variables, so we don't get
1786 ;; flooded by warnings about those vars `body' didn't use.
1787 (ignore ,@(mapcar #'car bindings
))
1790 (put 'with-parsed-tramp-file-name
'lisp-indent-function
2)
1791 (put 'with-parsed-tramp-file-name
'edebug-form-spec
'(form symbolp body
))
1792 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-parsed-tramp-file-name\\>"))
1794 (defun tramp-progress-reporter-update (reporter &optional value
)
1795 "Report progress of an operation for Tramp."
1796 (let* ((parameters (cdr reporter
))
1797 (message (aref parameters
3)))
1798 (when (string-match message
(or (current-message) ""))
1799 (progress-reporter-update reporter value
))))
1801 (defmacro with-tramp-progress-reporter
(vec level message
&rest body
)
1802 "Executes BODY, spinning a progress reporter with MESSAGE.
1803 If LEVEL does not fit for visible messages, there are only traces
1804 without a visible progress reporter."
1805 (declare (indent 3) (debug t
))
1807 (tramp-message ,vec
,level
"%s..." ,message
)
1808 (let ((cookie "failed")
1810 ;; We start a pulsing progress reporter after 3 seconds.
1811 (when (and tramp-message-show-message
1812 ;; Display only when there is a minimum level.
1813 (<= ,level
(min tramp-verbose
3)))
1814 (let ((pr (make-progress-reporter ,message nil nil
)))
1817 3 0.1 #'tramp-progress-reporter-update pr
))))))
1819 ;; Execute the body.
1820 (prog1 (progn ,@body
) (setq cookie
"done"))
1821 ;; Stop progress reporter.
1822 (if tm
(cancel-timer tm
))
1823 (tramp-message ,vec
,level
"%s...%s" ,message cookie
)))))
1825 (font-lock-add-keywords
1826 'emacs-lisp-mode
'("\\<with-tramp-progress-reporter\\>"))
1828 (defmacro with-tramp-file-property
(vec file property
&rest body
)
1829 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
1830 FILE must be a local file name on a connection identified via VEC."
1831 `(if (file-name-absolute-p ,file
)
1832 (let ((value (tramp-get-file-property ,vec
,file
,property
'undef
)))
1833 (when (eq value
'undef
)
1834 ;; We cannot pass @body as parameter to
1835 ;; `tramp-set-file-property' because it mangles our
1837 (setq value
(progn ,@body
))
1838 (tramp-set-file-property ,vec
,file
,property value
))
1842 (put 'with-tramp-file-property
'lisp-indent-function
3)
1843 (put 'with-tramp-file-property
'edebug-form-spec t
)
1844 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-tramp-file-property\\>"))
1846 (defmacro with-tramp-connection-property
(key property
&rest body
)
1847 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
1848 `(let ((value (tramp-get-connection-property ,key
,property
'undef
)))
1849 (when (eq value
'undef
)
1850 ;; We cannot pass ,@body as parameter to
1851 ;; `tramp-set-connection-property' because it mangles our debug
1853 (setq value
(progn ,@body
))
1854 (tramp-set-connection-property ,key
,property value
))
1857 (put 'with-tramp-connection-property
'lisp-indent-function
2)
1858 (put 'with-tramp-connection-property
'edebug-form-spec t
)
1859 (font-lock-add-keywords
1860 'emacs-lisp-mode
'("\\<with-tramp-connection-property\\>"))
1862 (defun tramp-drop-volume-letter (name)
1863 "Cut off unnecessary drive letter from file NAME.
1864 The functions `tramp-*-handle-expand-file-name' call `expand-file-name'
1865 locally on a remote file name. When the local system is a W32 system
1866 but the remote system is Unix, this introduces a superfluous drive
1867 letter into the file name. This function removes it."
1870 (if (tramp-compat-file-name-quoted-p name
)
1871 'tramp-compat-file-name-quote
'identity
)
1872 (let ((name (tramp-compat-file-name-unquote name
)))
1873 (if (string-match "\\`[a-zA-Z]:/" name
)
1874 (replace-match "/" nil t name
)
1877 ;;; Config Manipulation Functions:
1879 ;;;###tramp-autoload
1880 (defun tramp-set-completion-function (method function-list
)
1881 "Sets the list of completion functions for METHOD.
1882 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1883 The FUNCTION is intended to parse FILE according its syntax.
1884 It might be a predefined FUNCTION, or a user defined FUNCTION.
1885 For the list of predefined FUNCTIONs see `tramp-completion-function-alist'.
1889 (tramp-set-completion-function
1891 \\='((tramp-parse-sconfig \"/etc/ssh_config\")
1892 (tramp-parse-sconfig \"~/.ssh/config\")))"
1894 (let ((r function-list
)
1896 (setq tramp-completion-function-alist
1897 (delete (assoc method tramp-completion-function-alist
)
1898 tramp-completion-function-alist
))
1901 ;; Remove double entries.
1902 (when (member (car v
) (cdr v
))
1903 (setcdr v
(delete (car v
) (cdr v
))))
1904 ;; Check for function and file or registry key.
1905 (unless (and (functionp (nth 0 (car v
)))
1907 ;; Windows registry.
1908 ((string-match "^HKEY_CURRENT_USER" (nth 1 (car v
)))
1909 (and (memq system-type
'(cygwin windows-nt
))
1912 v
"reg" nil nil nil
"query" (nth 1 (car v
))))))
1913 ;; Zeroconf service type.
1915 "^_[[:alpha:]]+\\._[[:alpha:]]+$" (nth 1 (car v
))))
1916 ;; Configuration file.
1917 (t (file-exists-p (nth 1 (car v
))))))
1918 (setq r
(delete (car v
) r
)))
1922 (add-to-list 'tramp-completion-function-alist
1925 (defun tramp-get-completion-function (method)
1926 "Returns a list of completion functions for METHOD.
1927 For definition of that list see `tramp-set-completion-function'."
1929 `(;; Default settings are taken into account.
1930 (tramp-parse-default-user-host ,method
)
1931 ;; Hosts visited once shall be remembered.
1932 (tramp-parse-connection-properties ,method
))
1933 ;; The method related defaults.
1934 (cdr (assoc method tramp-completion-function-alist
))))
1937 ;;; Fontification of `read-file-name':
1939 (defvar tramp-rfn-eshadow-overlay
)
1940 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay
)
1942 (defun tramp-rfn-eshadow-setup-minibuffer ()
1943 "Set up a minibuffer for `file-name-shadow-mode'.
1944 Adds another overlay hiding filename parts according to Tramp's
1945 special handling of `substitute-in-file-name'."
1946 (when (symbol-value 'minibuffer-completing-file-name
)
1947 (setq tramp-rfn-eshadow-overlay
1948 (make-overlay (minibuffer-prompt-end) (minibuffer-prompt-end)))
1949 ;; Copy rfn-eshadow-overlay properties.
1950 (let ((props (overlay-properties (symbol-value 'rfn-eshadow-overlay
))))
1952 ;; The `field' property prevents correct minibuffer
1953 ;; completion; we exclude it.
1954 (if (not (eq (car props
) 'field
))
1955 (overlay-put tramp-rfn-eshadow-overlay
(pop props
) (pop props
))
1956 (pop props
) (pop props
))))))
1958 (add-hook 'rfn-eshadow-setup-minibuffer-hook
1959 'tramp-rfn-eshadow-setup-minibuffer
)
1960 (add-hook 'tramp-unload-hook
1962 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
1963 'tramp-rfn-eshadow-setup-minibuffer
)))
1965 (defun tramp-rfn-eshadow-update-overlay-regexp ()
1966 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format
))
1968 (defun tramp-rfn-eshadow-update-overlay ()
1969 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
1970 This is intended to be used as a minibuffer `post-command-hook' for
1971 `file-name-shadow-mode'; the minibuffer should have already
1972 been set up by `rfn-eshadow-setup-minibuffer'."
1973 ;; In remote files name, there is a shadowing just for the local part.
1975 (let ((end (or (overlay-end (symbol-value 'rfn-eshadow-overlay
))
1976 (minibuffer-prompt-end)))
1977 ;; We do not want to send any remote command.
1981 (buffer-substring-no-properties end
(point-max)))
1985 (1+ (or (string-match
1986 (tramp-rfn-eshadow-update-overlay-regexp)
1987 (buffer-string) end
)
1990 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay
)
1991 (rfn-eshadow-update-overlay-hook nil
)
1992 file-name-handler-alist
)
1993 (move-overlay rfn-eshadow-overlay
(point-max) (point-max))
1994 (rfn-eshadow-update-overlay))))))))
1996 (add-hook 'rfn-eshadow-update-overlay-hook
1997 'tramp-rfn-eshadow-update-overlay
)
1998 (add-hook 'tramp-unload-hook
2000 (remove-hook 'rfn-eshadow-update-overlay-hook
2001 'tramp-rfn-eshadow-update-overlay
)))
2003 ;; Inodes don't exist for some file systems. Therefore we must
2004 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
2005 ;; applied might be not so efficient (Ange-FTP uses hashes). But
2006 ;; performance isn't the major issue given that file transfer will
2008 (defvar tramp-inodes
0
2009 "Keeps virtual inodes numbers.")
2011 ;; Devices must distinguish physical file systems. The device numbers
2012 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2013 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2014 ;; EFS use device number "-1". In order to be different, we use device number
2015 ;; (-1 . x), whereby "x" is unique for a given (method user host).
2016 (defvar tramp-devices
0
2017 "Keeps virtual device numbers.")
2019 (defun tramp-default-file-modes (filename)
2020 "Return file modes of FILENAME as integer.
2021 If the file modes of FILENAME cannot be determined, return the
2022 value of `default-file-modes', without execute permissions."
2023 (or (file-modes filename
)
2024 (logand (default-file-modes) (string-to-number "0666" 8))))
2026 (defun tramp-replace-environment-variables (filename)
2027 "Replace environment variables in FILENAME.
2028 Return the string with the replaced variables."
2030 ;; Optional arg has been introduced with Emacs 24.4.
2031 (tramp-compat-funcall 'substitute-env-vars filename
'only-defined
))
2032 ;; We need an own implementation.
2034 (let ((idx (string-match "$\\(\\w+\\)" filename
)))
2035 ;; `$' is coded as `$$'.
2037 (or (zerop idx
) (not (eq ?$
(aref filename
(1- idx
)))))
2038 (getenv (match-string 1 filename
)))
2041 (substitute-in-file-name (match-string 0 filename
))
2045 (defun tramp-find-file-name-coding-system-alist (filename tmpname
)
2046 "Like `find-operation-coding-system' for Tramp filenames.
2047 Tramp's `insert-file-contents' and `write-region' work over
2048 temporary file names. If `file-coding-system-alist' contains an
2049 expression, which matches more than the file name suffix, the
2050 coding system might not be determined. This function repairs it."
2052 (dolist (elt file-coding-system-alist
(nreverse result
))
2053 (when (and (consp elt
) (string-match (car elt
) filename
))
2054 ;; We found a matching entry in `file-coding-system-alist'.
2055 ;; So we add a similar entry, but with the temporary file name
2057 (push (cons (regexp-quote tmpname
) (cdr elt
)) result
)))))
2059 (defun tramp-run-real-handler (operation args
)
2060 "Invoke normal file name handler for OPERATION.
2061 First arg specifies the OPERATION, second arg is a list of arguments to
2062 pass to the OPERATION."
2063 (let* ((inhibit-file-name-handlers
2064 `(tramp-file-name-handler
2065 tramp-vc-file-name-handler
2066 tramp-completion-file-name-handler
2067 tramp-archive-file-name-handler
2068 cygwin-mount-name-hook-function
2069 cygwin-mount-map-drive-hook-function
2071 ,(and (eq inhibit-file-name-operation operation
)
2072 inhibit-file-name-handlers
)))
2073 (inhibit-file-name-operation operation
))
2074 (apply operation args
)))
2076 ;; We handle here all file primitives. Most of them have the file
2077 ;; name as first parameter; nevertheless we check for them explicitly
2078 ;; in order to be signaled if a new primitive appears. This
2079 ;; scenario is needed because there isn't a way to decide by
2080 ;; syntactical means whether a foreign method must be called. It would
2081 ;; ease the life if `file-name-handler-alist' would support a decision
2082 ;; function as well but regexp only.
2083 (defun tramp-file-name-for-operation (operation &rest args
)
2084 "Return file name related to OPERATION file primitive.
2085 ARGS are the arguments OPERATION has been called with."
2087 ;; FILE resp DIRECTORY.
2089 '(access-file byte-compiler-base-file-name delete-directory
2090 delete-file diff-latest-backup-file directory-file-name
2091 directory-files directory-files-and-attributes
2092 dired-compress-file dired-uncache file-acl
2093 file-accessible-directory-p file-attributes
2094 file-directory-p file-executable-p file-exists-p
2095 file-local-copy file-modes file-name-as-directory
2096 file-name-directory file-name-nondirectory
2097 file-name-sans-versions file-notify-add-watch
2098 file-ownership-preserved-p file-readable-p
2099 file-regular-p file-remote-p file-selinux-context
2100 file-symlink-p file-truename file-writable-p
2101 find-backup-file-name find-file-noselect get-file-buffer
2102 insert-directory insert-file-contents load
2103 make-directory make-directory-internal set-file-acl
2104 set-file-modes set-file-selinux-context set-file-times
2105 substitute-in-file-name unhandled-file-name-directory
2108 file-name-case-insensitive-p
2111 (if (file-name-absolute-p (nth 0 args
))
2114 ;; FILE DIRECTORY resp FILE1 FILE2.
2116 '(add-name-to-file copy-directory copy-file expand-file-name
2117 file-equal-p file-in-directory-p
2118 file-name-all-completions file-name-completion
2119 ;; Starting with Emacs 26.1, just the 2nd argument of
2120 ;; `make-symbolic-link' matters. For backward
2121 ;; compatibility, we still accept the first argument as
2122 ;; file name to be checked. Handled properly in
2123 ;; `tramp-handle-*-make-symbolic-link'.
2124 file-newer-than-file-p make-symbolic-link rename-file
))
2127 ((tramp-tramp-file-p (nth 0 args
)) (nth 0 args
))
2128 ((tramp-tramp-file-p (nth 1 args
)) (nth 1 args
))
2129 (t default-directory
))))
2131 ((eq operation
'write-region
)
2132 (if (file-name-absolute-p (nth 2 args
))
2137 '(make-auto-save-file-name
2138 set-visited-file-modtime verify-visited-file-modtime
))
2140 (if (bufferp (nth 0 args
)) (nth 0 args
) (current-buffer))))
2143 '(process-file shell-command start-file-process
2145 make-nearby-temp-file temporary-file-directory
))
2149 '(file-notify-rm-watch
2151 file-notify-valid-p
))
2152 (when (processp (nth 0 args
))
2153 (with-current-buffer (process-buffer (nth 0 args
))
2154 default-directory
)))
2155 ;; Unknown file primitive.
2156 (t (error "unknown file I/O primitive: %s" operation
))))
2158 (defun tramp-find-foreign-file-name-handler (filename &optional _operation
)
2159 "Return foreign file name handler if exists."
2160 (when (tramp-tramp-file-p filename
)
2161 (let ((handler tramp-foreign-file-name-handler-alist
)
2164 (setq elt
(car handler
)
2165 handler
(cdr handler
))
2166 (when (funcall (car elt
) filename
)
2171 (defvar tramp-debug-on-error nil
2172 "Like `debug-on-error' but used Tramp internal.")
2174 (defmacro tramp-condition-case-unless-debug
2175 (var bodyform
&rest handlers
)
2176 "Like `condition-case-unless-debug' but `tramp-debug-on-error'."
2177 `(let ((debug-on-error tramp-debug-on-error
))
2178 (condition-case-unless-debug ,var
,bodyform
,@handlers
)))
2180 ;; In Emacs, there is some concurrency due to timers. If a timer
2181 ;; interrupts Tramp and wishes to use the same connection buffer as
2182 ;; the "main" Emacs, then garbage might occur in the connection
2183 ;; buffer. Therefore, we need to make sure that a timer does not use
2184 ;; the same connection buffer as the "main" Emacs. We implement a
2185 ;; cheap global lock, instead of locking each connection buffer
2186 ;; separately. The global lock is based on two variables,
2187 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
2188 ;; (with setq) to indicate a lock. But Tramp also calls itself during
2189 ;; processing of a single file operation, so we need to allow
2190 ;; recursive calls. That's where the `tramp-locker' variable comes in
2191 ;; -- it is let-bound to t during the execution of the current
2192 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
2193 ;; then we should just proceed because we have been called
2194 ;; recursively. But if `tramp-locker' is nil, then we are a timer
2195 ;; interrupting the "main" Emacs, and then we signal an error.
2197 (defvar tramp-locked nil
2198 "If non-nil, then Tramp is currently busy.
2199 Together with `tramp-locker', this implements a locking mechanism
2200 preventing reentrant calls of Tramp.")
2202 (defvar tramp-locker nil
2203 "If non-nil, then a caller has locked Tramp.
2204 Together with `tramp-locked', this implements a locking mechanism
2205 preventing reentrant calls of Tramp.")
2208 (defun tramp-file-name-handler (operation &rest args
)
2209 "Invoke Tramp file name handler.
2210 Falls back to normal file name handler if no Tramp file name handler exists."
2211 (let ((filename (apply 'tramp-file-name-for-operation operation args
)))
2212 (if (and tramp-mode
(tramp-tramp-file-p filename
))
2214 (setq filename
(tramp-replace-environment-variables filename
))
2215 (with-parsed-tramp-file-name filename nil
2216 (let ((completion (tramp-completion-mode-p))
2218 (tramp-find-foreign-file-name-handler filename operation
))
2220 ;; Call the backend function.
2222 (tramp-condition-case-unless-debug err
2223 (let ((sf (symbol-function foreign
)))
2224 ;; Some packages set the default directory to a
2225 ;; remote path, before respective Tramp packages
2226 ;; are already loaded. This results in
2227 ;; recursive loading. Therefore, we load the
2228 ;; Tramp packages locally.
2229 (when (autoloadp sf
)
2230 (let ((default-directory
2231 (tramp-compat-temporary-file-directory)))
2232 (load (cadr sf
) 'noerror
'nomessage
)))
2233 ;; If `non-essential' is non-nil, Tramp shall
2234 ;; not open a new connection.
2235 ;; If Tramp detects that it shouldn't continue
2236 ;; to work, it throws the `suppress' event.
2237 ;; This could happen for example, when Tramp
2238 ;; tries to open the same connection twice in a
2239 ;; short time frame.
2240 ;; In both cases, we try the default handler then.
2242 (catch 'non-essential
2244 (when (and tramp-locked
(not tramp-locker
))
2245 (setq tramp-locked nil
)
2247 (car-safe tramp-current-connection
)
2249 "Forbidden reentrant call of Tramp"))
2250 (let ((tl tramp-locked
))
2251 (setq tramp-locked t
)
2253 (let ((tramp-locker t
))
2254 (apply foreign operation args
))
2255 (setq tramp-locked tl
))))))
2257 ((eq result
'non-essential
)
2259 v
5 "Non-essential received in operation %s"
2260 (cons operation args
))
2261 (tramp-run-real-handler operation args
))
2262 ((eq result
'suppress
)
2263 (let (tramp-message-show-message)
2265 v
1 "Suppress received in operation %s"
2266 (cons operation args
))
2267 (tramp-cleanup-connection v t
)
2268 (tramp-run-real-handler operation args
)))
2271 ;; Trace that somebody has interrupted the operation.
2273 (let (tramp-message-show-message)
2275 v
1 "Interrupt received in operation %s"
2276 (cons operation args
)))
2277 ;; Propagate the quit signal.
2278 (signal (car err
) (cdr err
)))
2280 ;; When we are in completion mode, some failed
2281 ;; operations shall return at least a default
2282 ;; value in order to give the user a chance to
2283 ;; correct the file name in the minibuffer.
2284 ;; In order to get a full backtrace, one could apply
2285 ;; (setq tramp-debug-on-error t)
2288 ((and completion
(zerop (length localname
))
2289 (memq operation
'(file-exists-p file-directory-p
)))
2291 ((and completion
(zerop (length localname
))
2293 '(expand-file-name file-name-as-directory
)))
2295 ;; Propagate the error.
2296 (t (signal (car err
) (cdr err
))))))
2298 ;; Nothing to do for us. However, since we are in
2299 ;; `tramp-mode', we must suppress the volume letter on
2301 (setq result
(tramp-run-real-handler operation args
))
2302 (if (stringp result
)
2303 (tramp-drop-volume-letter result
)
2306 ;; When `tramp-mode' is not enabled, or the file name is quoted,
2307 ;; we don't do anything.
2308 (tramp-run-real-handler operation args
))))
2310 (defun tramp-completion-file-name-handler (operation &rest args
)
2311 "Invoke Tramp file name completion handler.
2312 Falls back to normal file name handler if no Tramp file name handler exists."
2313 (let ((fn (assoc operation tramp-completion-file-name-handler-alist
)))
2314 (if (and fn tramp-mode
)
2315 (save-match-data (apply (cdr fn
) args
))
2316 (tramp-run-real-handler operation args
))))
2319 (progn (defun tramp-autoload-file-name-handler (operation &rest args
)
2320 "Load Tramp file name handler, and perform OPERATION."
2322 (let ((default-directory temporary-file-directory
))
2323 (load "tramp" 'noerror
'nomessage
))
2324 (tramp-unload-file-name-handlers))
2325 (apply operation args
)))
2327 ;; `tramp-autoload-file-name-handler' must be registered before
2328 ;; evaluation of site-start and init files, because there might exist
2329 ;; remote files already, f.e. files kept via recentf-mode.
2331 (progn (defun tramp-register-autoload-file-name-handlers ()
2332 "Add Tramp file name handlers to `file-name-handler-alist' during autoload."
2333 (add-to-list 'file-name-handler-alist
2334 (cons tramp-autoload-file-name-regexp
2335 'tramp-autoload-file-name-handler
))
2336 (put 'tramp-autoload-file-name-handler
'safe-magic t
)))
2338 ;;;###autoload (tramp-register-autoload-file-name-handlers)
2340 (defun tramp-use-absolute-autoload-file-names ()
2341 "Change Tramp autoload objects to use absolute file names.
2342 This avoids problems during autoload, when `load-path' contains
2344 ;; We expect all other Tramp files in the same directory as tramp.el.
2345 (let* ((dir (expand-file-name (file-name-directory (locate-library "tramp"))))
2351 'file-name-sans-extension
2352 (directory-files dir nil
"^tramp.+\\.elc?$"))
2356 (when (and (functionp atom
)
2357 (autoloadp (symbol-function atom
))
2358 (string-match files-regexp
(cadr (symbol-function atom
))))
2360 (setf (cadr (symbol-function atom
))
2361 (expand-file-name (cadr (symbol-function atom
)) dir
))))))))
2363 (eval-after-load 'tramp
(tramp-use-absolute-autoload-file-names))
2365 (defun tramp-register-file-name-handlers ()
2366 "Add Tramp file name handlers to `file-name-handler-alist'."
2367 ;; Remove autoloaded handlers from file name handler alist. Useful,
2368 ;; if `tramp-syntax' has been changed. We cannot call
2369 ;; `tramp-unload-file-name-handlers', this would result in recursive
2370 ;; loading of Tramp.
2371 (dolist (fnh '(tramp-file-name-handler
2372 tramp-completion-file-name-handler
2373 tramp-archive-file-name-handler
2374 tramp-autoload-file-name-handler
))
2375 (let ((a1 (rassq fnh file-name-handler-alist
)))
2376 (setq file-name-handler-alist
(delq a1 file-name-handler-alist
))))
2378 ;; Add the handlers. We do not add anything to the `operations'
2379 ;; property of `tramp-file-name-handler' and
2380 ;; `tramp-archive-file-name-handler', this shall be done by the
2381 ;; respective foreign handlers.
2382 (add-to-list 'file-name-handler-alist
2383 (cons tramp-file-name-regexp
'tramp-file-name-handler
))
2384 (put 'tramp-file-name-handler
'safe-magic t
)
2386 (add-to-list 'file-name-handler-alist
2387 (cons tramp-completion-file-name-regexp
2388 'tramp-completion-file-name-handler
))
2389 (put 'tramp-completion-file-name-handler
'safe-magic t
)
2390 ;; Mark `operations' the handler is responsible for.
2391 (put 'tramp-completion-file-name-handler
'operations
2392 (mapcar 'car tramp-completion-file-name-handler-alist
))
2394 (add-to-list 'file-name-handler-alist
2395 (cons tramp-archive-file-name-regexp
2396 'tramp-archive-file-name-handler
))
2397 (put 'tramp-archive-file-name-handler
'safe-magic t
)
2399 ;; If jka-compr or epa-file are already loaded, move them to the
2400 ;; front of `file-name-handler-alist'.
2401 (dolist (fnh '(epa-file-handler jka-compr-handler
))
2402 (let ((entry (rassoc fnh file-name-handler-alist
)))
2404 (setq file-name-handler-alist
2405 (cons entry
(delete entry file-name-handler-alist
)))))))
2407 (eval-after-load 'tramp
(tramp-register-file-name-handlers))
2409 ;;;###tramp-autoload
2410 (progn (defun tramp-register-foreign-file-name-handler
2411 (func handler
&optional append
)
2412 "Register (FUNC . HANDLER) in `tramp-foreign-file-name-handler-alist'.
2413 FUNC is the function, which determines whether HANDLER is to be called.
2414 Add operations defined in `HANDLER-alist' to `tramp-file-name-handler'."
2416 'tramp-foreign-file-name-handler-alist
`(,func .
,handler
) append
)
2417 ;; Mark `operations' the handler is responsible for.
2418 (put 'tramp-file-name-handler
2422 (get 'tramp-file-name-handler
'operations
)
2425 (symbol-value (intern (concat (symbol-name handler
) "-alist")))))))))
2427 (defun tramp-exists-file-name-handler (operation &rest args
)
2428 "Check, whether OPERATION runs a file name handler."
2429 ;; The file name handler is determined on base of either an
2430 ;; argument, `buffer-file-name', or `default-directory'.
2432 (let* ((buffer-file-name "/")
2433 (default-directory "/")
2434 (fnha file-name-handler-alist
)
2435 (check-file-name-operation operation
)
2436 (file-name-handler-alist
2439 (lambda (operation &rest args
)
2440 "Returns OPERATION if it is the one to be checked."
2441 (if (equal check-file-name-operation operation
)
2443 (let ((file-name-handler-alist fnha
))
2444 (apply operation args
))))))))
2445 (equal (apply operation args
) operation
))))
2448 (progn (defun tramp-unload-file-name-handlers ()
2449 "Unload Tramp file name handlers from `file-name-handler-alist'."
2450 (dolist (fnh '(tramp-file-name-handler
2451 tramp-completion-file-name-handler
2452 tramp-archive-file-name-handler
2453 tramp-autoload-file-name-handler
))
2454 (let ((a1 (rassq fnh file-name-handler-alist
)))
2455 (setq file-name-handler-alist
(delq a1 file-name-handler-alist
))))))
2457 (add-hook 'tramp-unload-hook
'tramp-unload-file-name-handlers
)
2459 ;;; File name handler functions for completion mode:
2462 (defvar tramp-completion-mode nil
2463 "If non-nil, external packages signal that they are in file name completion.")
2464 (make-obsolete-variable 'tramp-completion-mode
'non-essential
"26.1")
2466 (defun tramp-completion-mode-p ()
2467 "Check, whether method / user name / host name completion is active."
2469 ;; Signal from outside.
2471 ;; This variable has been obsoleted in Emacs 26.
2472 tramp-completion-mode
))
2474 (defun tramp-connectable-p (filename)
2475 "Check, whether it is possible to connect the remote host w/o side-effects.
2476 This is true, if either the remote host is already connected, or if we are
2477 not in completion mode."
2478 (let (tramp-verbose)
2479 (and (tramp-tramp-file-p filename
)
2480 (or (not (tramp-completion-mode-p))
2482 (tramp-get-connection-process
2483 (tramp-dissect-file-name filename
)))))))
2485 ;; Method, host name and user name completion.
2486 ;; `tramp-completion-dissect-file-name' returns a list of
2487 ;; `tramp-file-name' structures. For all of them we return possible
2489 (defun tramp-completion-handle-file-name-all-completions (filename directory
)
2490 "Like `file-name-all-completions' for partial Tramp files."
2493 (tramp-drop-volume-letter (expand-file-name filename directory
)))
2496 ;; Suppress hop from completion.
2500 "\\(" "\\(" tramp-remote-file-name-spec-regexp
2501 tramp-postfix-hop-regexp
2504 (setq hop
(match-string 1 fullname
)
2505 fullname
(replace-match "" nil nil fullname
1)))
2507 ;; Possible completion structures.
2508 (dolist (elt (tramp-completion-dissect-file-name fullname
))
2509 (let* ((method (tramp-file-name-method elt
))
2510 (user (tramp-file-name-user elt
))
2511 (host (tramp-file-name-host elt
))
2512 (localname (tramp-file-name-localname elt
))
2513 (m (tramp-find-method method user host
))
2516 (unless localname
;; Nothing to complete.
2520 ;; Method dependent user / host combinations.
2524 (setq all-user-hosts
2525 (append all-user-hosts
2526 (funcall (nth 0 x
) (nth 1 x
)))))
2527 (tramp-get-completion-function m
))
2533 (tramp-get-completion-user-host
2534 method user host
(nth 0 x
) (nth 1 x
)))
2535 (delq nil all-user-hosts
)))))
2537 ;; Possible methods.
2539 (append result
(tramp-get-completion-methods m
)))))))
2541 ;; Unify list, add hop, remove nil elements.
2542 (dolist (elt result
)
2544 (string-match tramp-prefix-regexp elt
)
2545 (setq elt
(replace-match (concat tramp-prefix-format hop
) nil nil elt
))
2547 (substring elt
(length (tramp-drop-volume-letter directory
)))
2550 ;; Complete local parts.
2554 (tramp-run-real-handler
2555 'file-name-all-completions
(list filename directory
))))))
2557 ;; Method, host name and user name completion for a file.
2558 (defun tramp-completion-handle-file-name-completion
2559 (filename directory
&optional predicate
)
2560 "Like `file-name-completion' for Tramp files."
2563 (mapcar 'list
(file-name-all-completions filename directory
))
2564 (when (and predicate
2565 (tramp-connectable-p (expand-file-name filename directory
)))
2566 (lambda (x) (funcall predicate
(expand-file-name (car x
) directory
))))))
2568 ;; I misuse a little bit the `tramp-file-name' structure in order to
2569 ;; handle completion possibilities for partial methods / user names /
2570 ;; host names. Return value is a list of `tramp-file-name' structures
2571 ;; according to possible completions. If "localname" is non-nil it
2572 ;; means there shouldn't be a completion anymore.
2574 ;; Expected results:
2577 ;; ["x" nil nil nil]
2579 ;; "/x:" "/[x/" "/x:y" "/[x/y" "/x:y:" "/[x/y]"
2580 ;; ["x" nil "" nil] ["x" nil "y" nil] ["x" nil "y" ""]
2581 ;; ["x" "" nil nil] ["x" "y" nil nil]
2583 ;; "/x:y@""/[x/y@" "/x:y@z" "/[x/y@z" "/x:y@z:" "/[x/y@z]"
2584 ;;["x" "y" nil nil] ["x" "y" "z" nil] ["x" "y" "z" ""]
2585 (defun tramp-completion-dissect-file-name (name)
2586 "Returns a list of `tramp-file-name' structures.
2587 They are collected by `tramp-completion-dissect-file-name1'."
2589 (let* ((x-nil "\\|\\(\\)")
2590 (tramp-completion-ipv6-regexp
2593 (if (zerop (length tramp-postfix-ipv6-format
))
2594 tramp-postfix-host-format
2595 tramp-postfix-ipv6-format
)))
2596 ;; "/method" "/[method"
2597 (tramp-completion-file-name-structure1
2601 "\\(" tramp-method-regexp x-nil
"\\)$")
2603 ;; "/method:user" "/[method/user"
2604 (tramp-completion-file-name-structure2
2608 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2609 "\\(" tramp-user-regexp x-nil
"\\)$")
2611 ;; "/method:host" "/[method/host"
2612 (tramp-completion-file-name-structure3
2616 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2617 "\\(" tramp-host-regexp x-nil
"\\)$")
2619 ;; "/method:[ipv6" "/[method/ipv6"
2620 (tramp-completion-file-name-structure4
2624 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2625 tramp-prefix-ipv6-regexp
2626 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
2628 ;; "/method:user@host" "/[method/user@host"
2629 (tramp-completion-file-name-structure5
2633 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2634 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
2635 "\\(" tramp-host-regexp x-nil
"\\)$")
2637 ;; "/method:user@[ipv6" "/[method/user@ipv6"
2638 (tramp-completion-file-name-structure6
2642 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2643 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
2644 tramp-prefix-ipv6-regexp
2645 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
2650 (lambda (structure) (tramp-completion-dissect-file-name1 structure name
))
2652 tramp-completion-file-name-structure1
2653 tramp-completion-file-name-structure2
2654 tramp-completion-file-name-structure3
2655 tramp-completion-file-name-structure4
2656 tramp-completion-file-name-structure5
2657 tramp-completion-file-name-structure6
)))))
2659 (defun tramp-completion-dissect-file-name1 (structure name
)
2660 "Returns a `tramp-file-name' structure matching STRUCTURE.
2661 The structure consists of remote method, remote user,
2662 remote host and localname (filename on remote host)."
2665 (when (string-match (nth 0 structure
) name
)
2666 (make-tramp-file-name
2667 :method
(and (nth 1 structure
)
2668 (match-string (nth 1 structure
) name
))
2669 :user
(and (nth 2 structure
)
2670 (match-string (nth 2 structure
) name
))
2671 :host
(and (nth 3 structure
)
2672 (match-string (nth 3 structure
) name
))))))
2674 ;; This function returns all possible method completions, adding the
2675 ;; trailing method delimiter.
2676 (defun tramp-get-completion-methods (partial-method)
2677 "Returns all method completions for PARTIAL-METHOD."
2681 (string-match (concat "^" (regexp-quote partial-method
)) method
)
2682 (tramp-completion-make-tramp-file-name method nil nil nil
)))
2683 (mapcar 'car tramp-methods
)))
2685 ;; Compares partial user and host names with possible completions.
2686 (defun tramp-get-completion-user-host
2687 (method partial-user partial-host user host
)
2688 "Returns the most expanded string for user and host name completion.
2689 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
2692 ((and partial-user partial-host
)
2694 (string-match (concat "^" (regexp-quote partial-host
)) host
)
2695 (string-equal partial-user
(or user partial-user
)))
2696 (setq user partial-user
)
2703 (and user
(string-match (concat "^" (regexp-quote partial-user
)) user
))
2709 (and host
(string-match (concat "^" (regexp-quote partial-host
)) host
))
2715 (unless (zerop (+ (length user
) (length host
)))
2716 (tramp-completion-make-tramp-file-name method user host nil
)))
2718 (defun tramp-parse-default-user-host (method)
2719 "Return a list of (user host) tuples allowed to access for METHOD.
2720 This function is added always in `tramp-get-completion-function'
2721 for all methods. Resulting data are derived from default settings."
2722 `((,(tramp-find-user method nil nil
) ,(tramp-find-host method nil nil
))))
2724 ;; Generic function.
2725 (defun tramp-parse-group (regexp match-level skip-regexp
)
2726 "Return a (user host) tuple allowed to access.
2727 User is always nil."
2729 (when (re-search-forward regexp
(point-at-eol) t
)
2730 (setq result
(list nil
(match-string match-level
))))
2732 (> (skip-chars-forward skip-regexp
) 0)
2736 ;; Generic function.
2737 (defun tramp-parse-file (filename function
)
2738 "Return a list of (user host) tuples allowed to access.
2739 User is always nil."
2740 ;; On Windows, there are problems in completion when
2741 ;; `default-directory' is remote.
2742 (let ((default-directory (tramp-compat-temporary-file-directory)))
2743 (when (file-readable-p filename
)
2745 (insert-file-contents filename
)
2746 (goto-char (point-min))
2747 (cl-loop while
(not (eobp)) collect
(funcall function
))))))
2749 ;;;###tramp-autoload
2750 (defun tramp-parse-rhosts (filename)
2751 "Return a list of (user host) tuples allowed to access.
2752 Either user or host may be nil."
2753 (tramp-parse-file filename
'tramp-parse-rhosts-group
))
2755 (defun tramp-parse-rhosts-group ()
2756 "Return a (user host) tuple allowed to access.
2757 Either user or host may be nil."
2761 "^\\(" tramp-host-regexp
"\\)"
2762 "\\([ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
2763 (when (re-search-forward regexp
(point-at-eol) t
)
2764 (setq result
(append (list (match-string 3) (match-string 1)))))
2768 ;;;###tramp-autoload
2769 (defun tramp-parse-shosts (filename)
2770 "Return a list of (user host) tuples allowed to access.
2771 User is always nil."
2772 (tramp-parse-file filename
'tramp-parse-shosts-group
))
2774 (defun tramp-parse-shosts-group ()
2775 "Return a (user host) tuple allowed to access.
2776 User is always nil."
2777 (tramp-parse-group (concat "^\\(" tramp-host-regexp
"\\)") 1 ","))
2779 ;;;###tramp-autoload
2780 (defun tramp-parse-sconfig (filename)
2781 "Return a list of (user host) tuples allowed to access.
2782 User is always nil."
2783 (tramp-parse-file filename
'tramp-parse-sconfig-group
))
2785 (defun tramp-parse-sconfig-group ()
2786 "Return a (user host) tuple allowed to access.
2787 User is always nil."
2789 (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp
"\\)") 1 ","))
2791 ;; Generic function.
2792 (defun tramp-parse-shostkeys-sknownhosts (dirname regexp
)
2793 "Return a list of (user host) tuples allowed to access.
2794 User is always nil."
2795 ;; On Windows, there are problems in completion when
2796 ;; `default-directory' is remote.
2797 (let* ((default-directory (tramp-compat-temporary-file-directory))
2798 (files (and (file-directory-p dirname
) (directory-files dirname
))))
2801 when
(and (not (string-match "^\\.\\.?$" f
)) (string-match regexp f
))
2802 collect
(list nil
(match-string 1 f
)))))
2804 ;;;###tramp-autoload
2805 (defun tramp-parse-shostkeys (dirname)
2806 "Return a list of (user host) tuples allowed to access.
2807 User is always nil."
2808 (tramp-parse-shostkeys-sknownhosts
2809 dirname
(concat "^key_[0-9]+_\\(" tramp-host-regexp
"\\)\\.pub$")))
2811 ;;;###tramp-autoload
2812 (defun tramp-parse-sknownhosts (dirname)
2813 "Return a list of (user host) tuples allowed to access.
2814 User is always nil."
2815 (tramp-parse-shostkeys-sknownhosts
2817 (concat "^\\(" tramp-host-regexp
"\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$")))
2819 ;;;###tramp-autoload
2820 (defun tramp-parse-hosts (filename)
2821 "Return a list of (user host) tuples allowed to access.
2822 User is always nil."
2823 (tramp-parse-file filename
'tramp-parse-hosts-group
))
2825 (defun tramp-parse-hosts-group ()
2826 "Return a (user host) tuple allowed to access.
2827 User is always nil."
2829 (concat "^\\(" tramp-ipv6-regexp
"\\|" tramp-host-regexp
"\\)") 1 " \t"))
2831 ;;;###tramp-autoload
2832 (defun tramp-parse-passwd (filename)
2833 "Return a list of (user host) tuples allowed to access.
2834 Host is always \"localhost\"."
2835 (with-tramp-connection-property nil
"parse-passwd"
2836 (if (executable-find "getent")
2838 (when (zerop (tramp-call-process nil
"getent" nil t nil
"passwd"))
2839 (goto-char (point-min))
2840 (cl-loop while
(not (eobp)) collect
2841 (tramp-parse-etc-group-group))))
2842 (tramp-parse-file filename
'tramp-parse-passwd-group
))))
2844 (defun tramp-parse-passwd-group ()
2845 "Return a (user host) tuple allowed to access.
2846 Host is always \"localhost\"."
2848 (regexp (concat "^\\(" tramp-user-regexp
"\\):")))
2849 (when (re-search-forward regexp
(point-at-eol) t
)
2850 (setq result
(list (match-string 1) "localhost")))
2854 ;;;###tramp-autoload
2855 (defun tramp-parse-etc-group (filename)
2856 "Return a list of (group host) tuples allowed to access.
2857 Host is always \"localhost\"."
2858 (with-tramp-connection-property nil
"parse-group"
2859 (if (executable-find "getent")
2861 (when (zerop (tramp-call-process nil
"getent" nil t nil
"group"))
2862 (goto-char (point-min))
2863 (cl-loop while
(not (eobp)) collect
2864 (tramp-parse-etc-group-group))))
2865 (tramp-parse-file filename
'tramp-parse-etc-group-group
))))
2867 (defun tramp-parse-etc-group-group ()
2868 "Return a (group host) tuple allowed to access.
2869 Host is always \"localhost\"."
2871 (split (split-string (buffer-substring (point) (point-at-eol)) ":")))
2872 (when (member (user-login-name) (split-string (nth 3 split
) "," 'omit
))
2873 (setq result
(list (nth 0 split
) "localhost")))
2877 ;;;###tramp-autoload
2878 (defun tramp-parse-netrc (filename)
2879 "Return a list of (user host) tuples allowed to access.
2881 (tramp-parse-file filename
'tramp-parse-netrc-group
))
2883 (defun tramp-parse-netrc-group ()
2884 "Return a (user host) tuple allowed to access.
2889 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp
"\\)"
2890 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
2891 (when (re-search-forward regexp
(point-at-eol) t
)
2892 (setq result
(list (match-string 3) (match-string 1))))
2896 ;;;###tramp-autoload
2897 (defun tramp-parse-putty (registry-or-dirname)
2898 "Return a list of (user host) tuples allowed to access.
2899 User is always nil."
2900 (if (memq system-type
'(windows-nt))
2901 (with-tramp-connection-property nil
"parse-putty"
2903 (when (zerop (tramp-call-process
2904 nil
"reg" nil t nil
"query" registry-or-dirname
))
2905 (goto-char (point-min))
2906 (cl-loop while
(not (eobp)) collect
2907 (tramp-parse-putty-group registry-or-dirname
)))))
2909 (tramp-parse-shostkeys-sknownhosts
2910 registry-or-dirname
(concat "^\\(" tramp-host-regexp
"\\)$"))))
2912 (defun tramp-parse-putty-group (registry)
2913 "Return a (user host) tuple allowed to access.
2914 User is always nil."
2916 (regexp (concat (regexp-quote registry
) "\\\\\\(.+\\)")))
2917 (when (re-search-forward regexp
(point-at-eol) t
)
2918 (setq result
(list nil
(match-string 1))))
2922 ;;; Common file name handler functions for different backends:
2924 (defvar tramp-handle-file-local-copy-hook nil
2925 "Normal hook to be run at the end of `tramp-*-handle-file-local-copy'.")
2927 (defvar tramp-handle-write-region-hook nil
2928 "Normal hook to be run at the end of `tramp-*-handle-write-region'.")
2930 (defun tramp-handle-add-name-to-file
2931 (filename newname
&optional ok-if-already-exists
)
2932 "Like `add-name-to-file' for Tramp files."
2933 (with-parsed-tramp-file-name
2934 (if (tramp-tramp-file-p newname
) newname filename
) nil
2935 (unless (tramp-equal-remote filename newname
)
2938 "add-name-to-file: %s"
2939 "only implemented for same method, same user, same host"))
2940 ;; Do the 'confirm if exists' thing.
2941 (when (file-exists-p newname
)
2943 (if (or (null ok-if-already-exists
) ; not allowed to exist
2944 (and (numberp ok-if-already-exists
)
2947 "File %s already exists; make it a link anyway? "
2949 (tramp-error v
'file-already-exists newname
)
2950 (delete-file newname
)))
2951 (tramp-flush-file-property v
(file-name-directory localname
))
2952 (tramp-flush-file-property v localname
)
2954 filename newname
'ok-if-already-exists
'keep-time
2955 'preserve-uid-gid
'preserve-permissions
)))
2957 (defun tramp-handle-directory-file-name (directory)
2958 "Like `directory-file-name' for Tramp files."
2959 ;; If localname component of filename is "/", leave it unchanged.
2960 ;; Otherwise, remove any trailing slash from localname component.
2961 ;; Method, host, etc, are unchanged.
2962 (while (with-parsed-tramp-file-name directory nil
2963 (and (not (zerop (length localname
)))
2964 (eq (aref localname
(1- (length localname
))) ?
/)
2965 (not (string= localname
"/"))))
2966 (setq directory
(substring directory
0 -
1)))
2969 (defun tramp-handle-directory-files (directory &optional full match nosort
)
2970 "Like `directory-files' for Tramp files."
2971 (when (file-directory-p directory
)
2972 (setq directory
(file-name-as-directory (expand-file-name directory
)))
2973 (let ((temp (nreverse (file-name-all-completions "" directory
)))
2977 (setq item
(directory-file-name (pop temp
)))
2978 (when (or (null match
) (string-match match item
))
2979 (push (if full
(concat directory item
) item
)
2981 (if nosort result
(sort result
'string
<)))))
2983 (defun tramp-handle-directory-files-and-attributes
2984 (directory &optional full match nosort id-format
)
2985 "Like `directory-files-and-attributes' for Tramp files."
2988 (cons x
(file-attributes
2989 (if full x
(expand-file-name x directory
)) id-format
)))
2990 (directory-files directory full match nosort
)))
2992 (defun tramp-handle-dired-uncache (dir)
2993 "Like `dired-uncache' for Tramp files."
2994 (with-parsed-tramp-file-name
2995 (if (file-directory-p dir
) dir
(file-name-directory dir
)) nil
2996 (tramp-flush-directory-property v localname
)))
2998 (defun tramp-handle-file-accessible-directory-p (filename)
2999 "Like `file-accessible-directory-p' for Tramp files."
3000 (and (file-directory-p filename
)
3001 (file-readable-p filename
)))
3003 (defun tramp-handle-file-directory-p (filename)
3004 "Like `file-directory-p' for Tramp files."
3005 (eq (tramp-compat-file-attribute-type
3006 (file-attributes (file-truename filename
)))
3009 (defun tramp-handle-file-equal-p (filename1 filename2
)
3010 "Like `file-equalp-p' for Tramp files."
3011 ;; Native `file-equalp-p' calls `file-truename', which requires a
3012 ;; remote connection. This can be avoided, if FILENAME1 and
3013 ;; FILENAME2 are not located on the same remote host.
3015 (file-remote-p (expand-file-name filename1
))
3016 (file-remote-p (expand-file-name filename2
)))
3017 (tramp-run-real-handler 'file-equal-p
(list filename1 filename2
))))
3019 (defun tramp-handle-file-exists-p (filename)
3020 "Like `file-exists-p' for Tramp files."
3021 (not (null (file-attributes filename
))))
3023 (defun tramp-handle-file-in-directory-p (filename directory
)
3024 "Like `file-in-directory-p' for Tramp files."
3025 ;; Native `file-in-directory-p' calls `file-truename', which
3026 ;; requires a remote connection. This can be avoided, if FILENAME
3027 ;; and DIRECTORY are not located on the same remote host.
3029 (file-remote-p (expand-file-name filename
))
3030 (file-remote-p (expand-file-name directory
)))
3031 (tramp-run-real-handler 'file-in-directory-p
(list filename directory
))))
3033 (defun tramp-handle-file-modes (filename)
3034 "Like `file-modes' for Tramp files."
3035 (let ((truename (or (file-truename filename
) filename
)))
3036 (when (file-exists-p truename
)
3037 (tramp-mode-string-to-int
3038 (tramp-compat-file-attribute-modes (file-attributes truename
))))))
3040 ;; Localname manipulation functions that grok Tramp localnames...
3041 (defun tramp-handle-file-name-as-directory (file)
3042 "Like `file-name-as-directory' but aware of Tramp files."
3043 ;; `file-name-as-directory' would be sufficient except localname is
3044 ;; the empty string.
3045 (let ((v (tramp-dissect-file-name file t
)))
3046 ;; Run the command on the localname portion only unless we are in
3048 (tramp-make-tramp-file-name
3049 v
(unless (and (zerop (length (tramp-file-name-localname v
)))
3050 (not (tramp-connectable-p file
)))
3051 (tramp-run-real-handler
3052 'file-name-as-directory
3053 (list (or (tramp-file-name-localname v
) "")))))))
3055 (defun tramp-handle-file-name-case-insensitive-p (filename)
3056 "Like `file-name-case-insensitive-p' for Tramp files."
3057 ;; We make it a connection property, assuming that all file systems
3058 ;; on the remote host behave similar. This might be wrong for
3059 ;; mounted NFS directories or SMB/AFP shares; such more granular
3060 ;; tests will be added in case they are needed.
3061 (setq filename
(expand-file-name filename
))
3062 (with-parsed-tramp-file-name filename nil
3063 (or ;; Maybe there is a default value.
3064 (tramp-get-method-parameter v
'tramp-case-insensitive
)
3066 ;; There isn't. So we must check, in case there's a connection already.
3067 (and (file-remote-p filename nil
'connected
)
3068 (with-tramp-connection-property v
"case-insensitive"
3070 (with-tramp-progress-reporter v
5 "Checking case-insensitive"
3071 ;; The idea is to compare a file with lower case
3072 ;; letters with the same file with upper case letters.
3074 (tramp-compat-file-name-unquote
3075 (directory-file-name filename
)))
3077 ;; Check, whether we find an existing file with
3078 ;; lower case letters. This avoids us to create a
3080 (while (and (string-match
3081 "[a-z]" (file-remote-p candidate
'localname
))
3082 (not (file-exists-p candidate
)))
3084 (directory-file-name
3085 (file-name-directory candidate
))))
3086 ;; Nothing found, so we must use a temporary file
3087 ;; for comparison. `make-nearby-temp-file' is added
3088 ;; to Emacs 26+ like `file-name-case-insensitive-p',
3089 ;; so there is no compatibility problem calling it.
3092 "[a-z]" (file-remote-p candidate
'localname
))
3094 (let ((default-directory
3095 (file-name-directory filename
)))
3096 (tramp-compat-funcall
3097 'make-nearby-temp-file
"tramp."))
3099 ;; Check for the existence of the same file with
3100 ;; upper case letters.
3104 (file-remote-p candidate
)
3105 (upcase (file-remote-p candidate
'localname
))))
3107 (when tmpfile
(delete-file tmpfile
)))))))))))
3109 (defun tramp-handle-file-name-completion
3110 (filename directory
&optional predicate
)
3111 "Like `file-name-completion' for Tramp files."
3112 (let (hits-ignored-extensions)
3115 filename
(file-name-all-completions filename directory
)
3117 (when (funcall (or predicate
'identity
) (expand-file-name x directory
))
3120 completion-ignored-extensions
3122 (concat (regexp-opt completion-ignored-extensions
'paren
) "$") x
)
3123 ;; We remember the hit.
3124 (push x hits-ignored-extensions
))))))
3125 ;; No match. So we try again for ignored files.
3126 (try-completion filename hits-ignored-extensions
))))
3128 (defun tramp-handle-file-name-directory (file)
3129 "Like `file-name-directory' but aware of Tramp files."
3130 ;; Everything except the last filename thing is the directory. We
3131 ;; cannot apply `with-parsed-tramp-file-name', because this expands
3132 ;; the remote file name parts. This is a problem when we are in
3133 ;; file name completion.
3134 (let ((v (tramp-dissect-file-name file t
)))
3135 ;; Run the command on the localname portion only.
3136 (tramp-make-tramp-file-name
3137 v
(tramp-run-real-handler
3138 'file-name-directory
(list (or (tramp-file-name-localname v
) ""))))))
3140 (defun tramp-handle-file-name-nondirectory (file)
3141 "Like `file-name-nondirectory' but aware of Tramp files."
3142 (with-parsed-tramp-file-name file nil
3143 (tramp-run-real-handler 'file-name-nondirectory
(list localname
))))
3145 (defun tramp-handle-file-newer-than-file-p (file1 file2
)
3146 "Like `file-newer-than-file-p' for Tramp files."
3148 ((not (file-exists-p file1
)) nil
)
3149 ((not (file-exists-p file2
)) t
)
3150 (t (time-less-p (tramp-compat-file-attribute-modification-time
3151 (file-attributes file2
))
3152 (tramp-compat-file-attribute-modification-time
3153 (file-attributes file1
))))))
3155 (defun tramp-handle-file-regular-p (filename)
3156 "Like `file-regular-p' for Tramp files."
3157 (and (file-exists-p filename
)
3159 (aref (tramp-compat-file-attribute-modes (file-attributes filename
))
3162 (defun tramp-handle-file-remote-p (filename &optional identification connected
)
3163 "Like `file-remote-p' for Tramp files."
3164 ;; We do not want traces in the debug buffer.
3165 (let ((tramp-verbose (min tramp-verbose
3)))
3166 (when (tramp-tramp-file-p filename
)
3167 (let* ((v (tramp-dissect-file-name filename
))
3168 (p (tramp-get-connection-process v
))
3169 (c (and (process-live-p p
)
3170 (tramp-get-connection-property p
"connected" nil
))))
3171 ;; We expand the file name only, if there is already a connection.
3172 (with-parsed-tramp-file-name
3173 (if c
(expand-file-name filename
) filename
) nil
3174 (and (or (not connected
) c
)
3176 ((eq identification
'method
) method
)
3177 ;; Domain and port are appended to user and host,
3179 ((eq identification
'user
) (tramp-file-name-user-domain v
))
3180 ((eq identification
'host
) (tramp-file-name-host-port v
))
3181 ((eq identification
'localname
) localname
)
3182 ((eq identification
'hop
) hop
)
3183 (t (tramp-make-tramp-file-name
3184 method user domain host port
"" hop
)))))))))
3186 (defun tramp-handle-file-selinux-context (_filename)
3187 "Like `file-selinux-context' for Tramp files."
3188 ;; Return nil context.
3191 (defun tramp-handle-file-symlink-p (filename)
3192 "Like `file-symlink-p' for Tramp files."
3193 (let ((x (tramp-compat-file-attribute-type (file-attributes filename
))))
3194 (and (stringp x
) x
)))
3196 (defun tramp-handle-file-truename (filename)
3197 "Like `file-truename' for Tramp files."
3198 (let ((result (expand-file-name filename
))
3200 ;; Don't make the following value larger than
3201 ;; necessary. People expect an error message in a
3202 ;; timely fashion when something is wrong;
3203 ;; otherwise they might think that Emacs is hung.
3204 ;; Of course, correctness has to come first.
3209 (with-parsed-tramp-file-name result v1
3210 (with-tramp-file-property v1 v1-localname
"file-truename"
3211 (while (and (setq symlink-target
(file-symlink-p result
))
3212 (< numchase numchase-limit
))
3213 (setq numchase
(1+ numchase
)
3215 (with-parsed-tramp-file-name (expand-file-name result
) v2
3216 (tramp-make-tramp-file-name
3217 v2-method v2-user v2-domain v2-host v2-port
3219 (if (tramp-compat-file-name-quoted-p v2-localname
)
3220 'tramp-compat-file-name-quote
'identity
)
3222 (if (stringp symlink-target
)
3223 (if (file-remote-p symlink-target
)
3224 (let (file-name-handler-alist)
3225 (tramp-compat-file-name-quote symlink-target
))
3227 symlink-target
(file-name-directory v2-localname
)))
3229 (when (>= numchase numchase-limit
)
3232 "Maximum number (%d) of symlinks exceeded" numchase-limit
)))
3233 (directory-file-name result
)))
3235 ;; Preserve trailing "/".
3236 (if (string-equal (file-name-nondirectory filename
) "") "/" ""))))
3238 (defun tramp-handle-find-backup-file-name (filename)
3239 "Like `find-backup-file-name' for Tramp files."
3240 (with-parsed-tramp-file-name filename nil
3241 (let ((backup-directory-alist
3242 (if tramp-backup-directory-alist
3247 (if (and (stringp (cdr x
))
3248 (file-name-absolute-p (cdr x
))
3249 (not (tramp-tramp-file-p (cdr x
))))
3250 (tramp-make-tramp-file-name
3251 method user domain host port
(cdr x
) hop
)
3253 tramp-backup-directory-alist
)
3254 backup-directory-alist
)))
3255 (tramp-run-real-handler 'find-backup-file-name
(list filename
)))))
3257 (defun tramp-handle-insert-directory
3258 (filename switches
&optional wildcard full-directory-p
)
3259 "Like `insert-directory' for Tramp files."
3260 (unless switches
(setq switches
""))
3261 ;; Mark trailing "/".
3262 (when (and (zerop (length (file-name-nondirectory filename
)))
3263 (not full-directory-p
))
3264 (setq switches
(concat switches
"F")))
3265 (with-parsed-tramp-file-name (expand-file-name filename
) nil
3266 (with-tramp-progress-reporter v
0 (format "Opening directory %s" filename
)
3268 (let (ls-lisp-use-insert-directory-program start
)
3269 (tramp-run-real-handler
3271 (list filename switches wildcard full-directory-p
))
3272 ;; `ls-lisp' always returns full listings. We must remove
3273 ;; superfluous parts.
3274 (unless (string-match "l" switches
)
3276 (goto-char (point-min))
3278 (text-property-not-all
3279 (point) (point-at-eol) 'dired-filename t
))
3282 (or (text-property-any start
(point-at-eol) 'dired-filename t
)
3284 (if (= (point-at-bol) (point-at-eol))
3286 (delete-region (point) (progn (forward-line) (point)))
3287 (forward-line)))))))))
3289 (defun tramp-handle-insert-file-contents
3290 (filename &optional visit beg end replace
)
3291 "Like `insert-file-contents' for Tramp files."
3292 (barf-if-buffer-read-only)
3293 (setq filename
(expand-file-name filename
))
3294 (let (result local-copy remote-copy
)
3295 (with-parsed-tramp-file-name filename nil
3297 (if (not (file-exists-p filename
))
3299 v tramp-file-missing
3300 "File `%s' not found on remote host" filename
)
3302 (with-tramp-progress-reporter
3303 v
3 (format-message "Inserting `%s'" filename
)
3305 (if (and (tramp-local-host-p v
)
3306 (let (file-name-handler-alist)
3307 (file-readable-p localname
)))
3308 ;; Short track: if we are on the local host, we can
3311 (tramp-run-real-handler
3312 'insert-file-contents
3313 (list localname visit beg end replace
)))
3315 ;; When we shall insert only a part of the file, we
3316 ;; copy this part. This works only for the shell file
3318 (when (and (or beg end
)
3319 (tramp-get-method-parameter
3320 v
'tramp-login-program
))
3321 (setq remote-copy
(tramp-make-tramp-temp-file v
))
3322 ;; This is defined in tramp-sh.el. Let's assume
3323 ;; this is loaded already.
3324 (tramp-compat-funcall
3329 (format "dd bs=1 skip=%d if=%s count=%d of=%s"
3330 beg
(tramp-shell-quote-argument localname
)
3331 (- end beg
) remote-copy
))
3333 (format "dd bs=1 skip=%d if=%s of=%s"
3334 beg
(tramp-shell-quote-argument localname
)
3337 (format "dd bs=1 count=%d if=%s of=%s"
3338 end
(tramp-shell-quote-argument localname
)
3340 (setq tramp-temp-buffer-file-name nil beg nil end nil
))
3342 ;; `insert-file-contents-literally' takes care to
3343 ;; avoid calling jka-compr.el and epa.el. By
3344 ;; let-binding `inhibit-file-name-operation', we
3345 ;; propagate that care to the `file-local-copy'
3348 (let ((inhibit-file-name-operation
3349 (when (eq inhibit-file-name-operation
3350 'insert-file-contents
)
3353 ((stringp remote-copy
)
3355 (tramp-make-tramp-file-name
3356 method user domain host port remote-copy
)))
3357 ((stringp tramp-temp-buffer-file-name
)
3359 filename tramp-temp-buffer-file-name
'ok
)
3360 tramp-temp-buffer-file-name
)
3361 (t (file-local-copy filename
)))))
3363 ;; When the file is not readable for the owner, it
3364 ;; cannot be inserted, even if it is readable for the
3365 ;; group or for everybody.
3366 (set-file-modes local-copy
(string-to-number "0600" 8))
3368 (when (and (null remote-copy
)
3369 (tramp-get-method-parameter
3370 v
'tramp-copy-keep-tmpfile
))
3371 ;; We keep the local file for performance reasons,
3372 ;; useful for "rsync".
3373 (setq tramp-temp-buffer-file-name local-copy
))
3375 ;; We must ensure that `file-coding-system-alist'
3376 ;; matches `local-copy'.
3377 (let ((file-coding-system-alist
3378 (tramp-find-file-name-coding-system-alist
3379 filename local-copy
)))
3381 (insert-file-contents
3382 local-copy visit beg end replace
))))
3384 (add-hook 'find-file-not-found-functions
3385 `(lambda () (signal ',(car err
) ',(cdr err
)))
3387 (signal (car err
) (cdr err
))))))
3392 (setq buffer-file-name filename
)
3393 (setq buffer-read-only
(not (file-writable-p filename
)))
3394 (set-visited-file-modtime)
3395 (set-buffer-modified-p nil
))
3396 (when (and (stringp local-copy
)
3397 (or remote-copy
(null tramp-temp-buffer-file-name
)))
3398 (delete-file local-copy
))
3399 (when (stringp remote-copy
)
3401 (tramp-make-tramp-file-name
3402 method user domain host port remote-copy
)))))
3405 (list (expand-file-name filename
)
3408 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix
)
3409 "Like `load' for Tramp files."
3410 (with-parsed-tramp-file-name (expand-file-name file
) nil
3412 (cond ((file-exists-p (concat file
".elc"))
3413 (setq file
(concat file
".elc")))
3414 ((file-exists-p (concat file
".el"))
3415 (setq file
(concat file
".el")))))
3417 ;; The first condition is always true for absolute file names.
3418 ;; Included for safety's sake.
3419 (unless (or (file-name-directory file
)
3420 (string-match "\\.elc?\\'" file
))
3423 "File `%s' does not include a `.el' or `.elc' suffix" file
)))
3425 (when (not (file-exists-p file
))
3427 v tramp-file-missing
"Cannot load nonexistent file `%s'" file
)))
3428 (if (not (file-exists-p file
))
3430 (let ((tramp-message-show-message (not nomessage
)))
3431 (with-tramp-progress-reporter v
0 (format "Loading %s" file
)
3432 (let ((local-copy (file-local-copy file
)))
3434 (load local-copy noerror t nosuffix must-suffix
)
3435 (delete-file local-copy
)))))
3438 (defun tramp-handle-make-symbolic-link
3439 (target linkname
&optional ok-if-already-exists
)
3440 "Like `make-symbolic-link' for Tramp files.
3441 This is the fallback implementation for backends which do not
3442 support symbolic links."
3443 (if (tramp-tramp-file-p (expand-file-name linkname
))
3445 (tramp-dissect-file-name (expand-file-name linkname
)) 'file-error
3446 "make-symbolic-link not supported")
3447 ;; This is needed prior Emacs 26.1, where TARGET has also be
3448 ;; checked for a file name handler.
3449 (tramp-run-real-handler
3450 'make-symbolic-link
(list target linkname ok-if-already-exists
))))
3452 (defun tramp-handle-shell-command
3453 (command &optional output-buffer error-buffer
)
3454 "Like `shell-command' for Tramp files."
3455 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
3456 ;; We cannot use `shell-file-name' and `shell-command-switch',
3457 ;; they are variables of the local host.
3460 (tramp-get-method-parameter
3461 (tramp-dissect-file-name default-directory
)
3462 'tramp-remote-shell
)
3463 (tramp-get-method-parameter
3464 (tramp-dissect-file-name default-directory
)
3465 'tramp-remote-shell-args
))
3466 (list (substring command
0 asynchronous
))))
3470 ((bufferp output-buffer
) output-buffer
)
3471 ((stringp output-buffer
) (get-buffer-create output-buffer
))
3473 (setq current-buffer-p t
)
3475 (t (get-buffer-create
3477 "*Async Shell Command*"
3478 "*Shell Command Output*")))))
3481 ((bufferp error-buffer
) error-buffer
)
3482 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
3484 (if (and (not asynchronous
) error-buffer
)
3485 (with-parsed-tramp-file-name default-directory nil
3486 (list output-buffer
(tramp-make-tramp-temp-file v
)))
3488 (p (get-buffer-process output-buffer
)))
3490 ;; Check whether there is another process running. Tramp does not
3491 ;; support 2 (asynchronous) processes in parallel.
3493 (if (yes-or-no-p "A command is running. Kill it? ")
3494 (ignore-errors (kill-process p
))
3495 (tramp-compat-user-error p
"Shell command in progress")))
3497 (if current-buffer-p
3499 (barf-if-buffer-read-only)
3501 (with-current-buffer output-buffer
3502 (setq buffer-read-only nil
)
3505 (if (and (not current-buffer-p
) (integerp asynchronous
))
3508 (setq p
(apply 'start-file-process
"*Async Shell*" buffer args
))
3510 (with-current-buffer output-buffer
3511 (display-buffer output-buffer
'(nil (allow-no-window . t
)))
3512 (setq mode-line-process
'(":%s"))
3514 (set-process-sentinel p
'shell-command-sentinel
)
3515 (set-process-filter p
'comint-output-filter
)))
3519 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
3520 ;; Insert error messages if they were separated.
3521 (when (listp buffer
)
3522 (with-current-buffer error-buffer
3523 (insert-file-contents (cadr buffer
)))
3524 (delete-file (cadr buffer
)))
3525 (if current-buffer-p
3526 ;; This is like exchange-point-and-mark, but doesn't
3527 ;; activate the mark. It is cleaner to avoid activation,
3528 ;; even though the command loop would deactivate the mark
3529 ;; because we inserted text.
3530 (goto-char (prog1 (mark t
)
3531 (set-marker (mark-marker) (point)
3533 ;; There's some output, display it.
3534 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
3535 (display-message-or-buffer output-buffer
)))))))
3537 (defun tramp-handle-substitute-in-file-name (filename)
3538 "Like `substitute-in-file-name' for Tramp files.
3539 \"//\" and \"/~\" substitute only in the local filename part."
3540 ;; Check, whether the local part is a quoted file name.
3541 (if (tramp-compat-file-name-quoted-p filename
)
3543 ;; First, we must replace environment variables.
3544 (setq filename
(tramp-replace-environment-variables filename
))
3545 (with-parsed-tramp-file-name filename nil
3546 ;; Ignore in LOCALNAME everything before "//" or "/~".
3547 (when (and (stringp localname
) (string-match ".+?/\\(/\\|~\\)" localname
))
3549 (concat (file-remote-p filename
)
3550 (replace-match "\\1" nil nil localname
)))
3551 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
3552 (when (string-match "~$" filename
)
3553 (setq filename
(concat filename
"/"))))
3554 ;; We do not want to replace environment variables, again.
3555 (let (process-environment)
3556 (tramp-run-real-handler 'substitute-in-file-name
(list filename
))))))
3558 (defun tramp-handle-set-visited-file-modtime (&optional time-list
)
3559 "Like `set-visited-file-modtime' for Tramp files."
3560 (unless (buffer-file-name)
3561 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
3564 (let ((remote-file-name-inhibit-cache t
))
3565 ;; '(-1 65535) means file doesn't exists yet.
3567 (or (tramp-compat-file-attribute-modification-time
3568 (file-attributes (buffer-file-name)))
3570 ;; We use '(0 0) as a don't-know value.
3571 (unless (equal time-list
'(0 0))
3572 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))))
3574 (defun tramp-handle-verify-visited-file-modtime (&optional buf
)
3575 "Like `verify-visited-file-modtime' for Tramp files.
3576 At the time `verify-visited-file-modtime' calls this function, we
3577 already know that the buffer is visiting a file and that
3578 `visited-file-modtime' does not return 0. Do not call this
3579 function directly, unless those two cases are already taken care
3581 (with-current-buffer (or buf
(current-buffer))
3582 (let ((f (buffer-file-name)))
3583 ;; There is no file visiting the buffer, or the buffer has no
3584 ;; recorded last modification time, or there is no established
3587 (eq (visited-file-modtime) 0)
3588 (not (file-remote-p f nil
'connected
)))
3590 (let* ((remote-file-name-inhibit-cache t
)
3591 (attr (file-attributes f
))
3592 (modtime (tramp-compat-file-attribute-modification-time attr
))
3593 (mt (visited-file-modtime)))
3596 ;; File exists, and has a known modtime.
3597 ((and attr
(not (equal modtime
'(0 0))))
3598 (< (abs (tramp-time-diff
3600 ;; For compatibility, deal with both the old
3601 ;; (HIGH . LOW) and the new (HIGH LOW) return
3602 ;; values of `visited-file-modtime'.
3604 (list (car mt
) (cdr mt
))
3607 ;; Modtime has the don't know value.
3609 ;; If file does not exist, say it is not modified if and
3610 ;; only if that agrees with the buffer's record.
3611 (t (equal mt
'(-1 65535)))))))))
3613 (defun tramp-handle-file-notify-add-watch (filename _flags _callback
)
3614 "Like `file-notify-add-watch' for Tramp files."
3615 ;; This is the default handler. tramp-gvfs.el and tramp-sh.el have
3617 (setq filename
(expand-file-name filename
))
3618 (with-parsed-tramp-file-name filename nil
3620 v
'file-notify-error
"File notification not supported for `%s'" filename
)))
3622 (defun tramp-handle-file-notify-rm-watch (proc)
3623 "Like `file-notify-rm-watch' for Tramp files."
3624 ;; The descriptor must be a process object.
3625 (unless (processp proc
)
3626 (tramp-error proc
'file-notify-error
"Not a valid descriptor %S" proc
))
3627 (tramp-message proc
6 "Kill %S" proc
)
3628 (delete-process proc
))
3630 (defun tramp-handle-file-notify-valid-p (proc)
3631 "Like `file-notify-valid-p' for Tramp files."
3632 (and (process-live-p proc
)
3633 ;; Sometimes, the process is still in status `run' when the
3634 ;; file or directory to be watched is deleted already.
3635 (with-current-buffer (process-buffer proc
)
3637 (concat (file-remote-p default-directory
)
3638 (process-get proc
'watch-name
))))))
3640 ;;; Functions for establishing connection:
3642 ;; The following functions are actions to be taken when seeing certain
3643 ;; prompts from the remote host. See the variable
3644 ;; `tramp-actions-before-shell' for usage of these functions.
3646 (defun tramp-action-login (_proc vec
)
3647 "Send the login name."
3648 (let ((user (or (tramp-file-name-user vec
)
3649 (with-tramp-connection-property vec
"login-as"
3650 (save-window-excursion
3651 (let ((enable-recursive-minibuffers t
))
3652 (pop-to-buffer (tramp-get-connection-buffer vec
))
3653 (read-string (match-string 0))))))))
3654 (with-current-buffer (tramp-get-connection-buffer vec
)
3655 (tramp-message vec
6 "\n%s" (buffer-string)))
3656 (tramp-message vec
3 "Sending login name `%s'" user
)
3657 (tramp-send-string vec
(concat user tramp-local-end-of-line
))))
3659 (defun tramp-action-password (proc vec
)
3660 "Query the user for a password."
3661 (with-current-buffer (process-buffer proc
)
3662 (let ((enable-recursive-minibuffers t
)
3663 (case-fold-search t
))
3664 ;; Let's check whether a wrong password has been sent already.
3665 ;; Sometimes, the process returns a new password request
3666 ;; immediately after rejecting the previous (wrong) one.
3667 (unless (tramp-get-connection-property vec
"first-password-request" nil
)
3668 (tramp-clear-passwd vec
))
3669 (goto-char (point-min))
3670 (tramp-check-for-regexp proc tramp-password-prompt-regexp
)
3671 (tramp-message vec
3 "Sending %s" (match-string 1))
3672 ;; We don't call `tramp-send-string' in order to hide the
3673 ;; password from the debug buffer.
3674 (process-send-string
3675 proc
(concat (tramp-read-passwd proc
) tramp-local-end-of-line
))
3676 ;; Hide password prompt.
3677 (narrow-to-region (point-max) (point-max)))))
3679 (defun tramp-action-succeed (_proc _vec
)
3680 "Signal success in finding shell prompt."
3681 (throw 'tramp-action
'ok
))
3683 (defun tramp-action-permission-denied (proc _vec
)
3684 "Signal permission denied."
3686 (throw 'tramp-action
'permission-denied
))
3688 (defun tramp-action-yesno (proc vec
)
3689 "Ask the user for confirmation using `yes-or-no-p'.
3690 Send \"yes\" to remote process on confirmation, abort otherwise.
3691 See also `tramp-action-yn'."
3692 (save-window-excursion
3693 (let ((enable-recursive-minibuffers t
))
3694 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec
)))
3695 (unless (yes-or-no-p (match-string 0))
3697 (throw 'tramp-action
'permission-denied
))
3698 (with-current-buffer (tramp-get-connection-buffer vec
)
3699 (tramp-message vec
6 "\n%s" (buffer-string)))
3700 (tramp-send-string vec
(concat "yes" tramp-local-end-of-line
)))))
3702 (defun tramp-action-yn (proc vec
)
3703 "Ask the user for confirmation using `y-or-n-p'.
3704 Send \"y\" to remote process on confirmation, abort otherwise.
3705 See also `tramp-action-yesno'."
3706 (save-window-excursion
3707 (let ((enable-recursive-minibuffers t
))
3708 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec
)))
3709 (unless (y-or-n-p (match-string 0))
3711 (throw 'tramp-action
'permission-denied
))
3712 (with-current-buffer (tramp-get-connection-buffer vec
)
3713 (tramp-message vec
6 "\n%s" (buffer-string)))
3714 (tramp-send-string vec
(concat "y" tramp-local-end-of-line
)))))
3716 (defun tramp-action-terminal (_proc vec
)
3717 "Tell the remote host which terminal type to use.
3718 The terminal type can be configured with `tramp-terminal-type'."
3719 (tramp-message vec
5 "Setting `%s' as terminal type." tramp-terminal-type
)
3720 (with-current-buffer (tramp-get-connection-buffer vec
)
3721 (tramp-message vec
6 "\n%s" (buffer-string)))
3722 (tramp-send-string vec
(concat tramp-terminal-type tramp-local-end-of-line
)))
3724 (defun tramp-action-process-alive (proc _vec
)
3725 "Check, whether a process has finished."
3726 (unless (process-live-p proc
)
3727 (throw 'tramp-action
'process-died
)))
3729 (defun tramp-action-out-of-band (proc vec
)
3730 "Check, whether an out-of-band copy has finished."
3731 ;; There might be pending output for the exit status.
3732 (tramp-accept-process-output proc
0.1)
3733 (cond ((and (not (process-live-p proc
))
3734 (zerop (process-exit-status proc
)))
3735 (tramp-message vec
3 "Process has finished.")
3736 (throw 'tramp-action
'ok
))
3737 ((or (and (memq (process-status proc
) '(stop exit
))
3738 (not (zerop (process-exit-status proc
))))
3739 (memq (process-status proc
) '(signal)))
3740 ;; `scp' could have copied correctly, but set modes could have failed.
3741 ;; This can be ignored.
3742 (with-current-buffer (process-buffer proc
)
3743 (goto-char (point-min))
3744 (if (re-search-forward tramp-operation-not-permitted-regexp nil t
)
3746 (tramp-message vec
5 "'set mode' error ignored.")
3747 (tramp-message vec
3 "Process has finished.")
3748 (throw 'tramp-action
'ok
))
3749 (tramp-message vec
3 "Process has died.")
3750 (throw 'tramp-action
'out-of-band-failed
))))
3753 ;;; Functions for processing the actions:
3755 (defun tramp-process-one-action (proc vec actions
)
3756 "Wait for output from the shell and perform one action."
3757 (let ((case-fold-search t
)
3758 found todo item pattern action
)
3760 ;; Reread output once all actions have been performed.
3761 ;; Obviously, the output was not complete.
3762 (tramp-accept-process-output proc
1)
3765 (setq item
(pop todo
))
3766 (setq pattern
(format "\\(%s\\)\\'" (symbol-value (nth 0 item
))))
3767 (setq action
(nth 1 item
))
3769 vec
5 "Looking for regexp \"%s\" from remote shell" pattern
)
3770 (when (tramp-check-for-regexp proc pattern
)
3771 (tramp-message vec
5 "Call `%s'" (symbol-name action
))
3772 (setq found
(funcall action proc vec
)))))
3775 (defun tramp-process-actions (proc vec pos actions
&optional timeout
)
3776 "Perform ACTIONS until success or TIMEOUT.
3777 PROC and VEC indicate the remote connection to be used. POS, if
3778 set, is the starting point of the region to be deleted in the
3780 ;; Enable `auth-source', unless "emacs -Q" has been called. We must
3781 ;; use the "password-vector" property in case we have several hops.
3782 (tramp-set-connection-property
3783 (tramp-get-connection-property
3784 proc
"password-vector"
3785 (tramp-get-connection-property proc
"vector" nil
))
3786 "first-password-request" tramp-cache-read-persistent-data
)
3788 (with-tramp-progress-reporter
3789 proc
3 "Waiting for prompts from remote shell"
3792 (with-timeout (timeout (setq exit
'timeout
))
3795 (catch 'tramp-action
3796 (tramp-process-one-action proc vec actions
)))))
3799 (catch 'tramp-action
3800 (tramp-process-one-action proc vec actions
)))))
3801 (with-current-buffer (tramp-get-connection-buffer vec
)
3803 (tramp-message vec
6 "\n%s" (buffer-string)))
3804 (unless (eq exit
'ok
)
3805 (tramp-clear-passwd vec
)
3806 (delete-process proc
)
3807 (tramp-error-with-buffer
3808 (tramp-get-connection-buffer vec
) vec
'file-error
3810 ((eq exit
'permission-denied
) "Permission denied")
3811 ((eq exit
'out-of-band-failed
)
3813 "Copy failed, see buffer `%s' for details"
3814 (tramp-get-connection-buffer vec
)))
3815 ((eq exit
'process-died
)
3816 (substitute-command-keys
3818 "Tramp failed to connect. If this happens repeatedly, try\n"
3819 " `\\[tramp-cleanup-this-connection]'")))
3822 "Timeout reached, see buffer `%s' for details"
3823 (tramp-get-connection-buffer vec
)))
3824 (t "Login failed")))))
3826 (with-current-buffer (tramp-get-connection-buffer vec
)
3827 (let (buffer-read-only) (delete-region pos
(point))))))))
3829 ;;; Utility functions:
3831 (defun tramp-accept-process-output (proc timeout
)
3832 "Like `accept-process-output' for Tramp processes.
3833 This is needed in order to hide `last-coding-system-used', which is set
3834 for process communication also."
3835 (with-current-buffer (process-buffer proc
)
3836 (let (buffer-read-only last-coding-system-used
)
3837 ;; Under Windows XP, `accept-process-output' doesn't return
3838 ;; sometimes. So we add an additional timeout. JUST-THIS-ONE
3839 ;; is set due to Bug#12145. It is an integer, in order to avoid
3840 ;; running timers as well.
3842 proc
10 "%s %s %s\n%s"
3843 proc
(process-status proc
)
3844 (with-timeout (timeout)
3845 (accept-process-output proc timeout nil
0))
3848 (defun tramp-check-for-regexp (proc regexp
)
3849 "Check, whether REGEXP is contained in process buffer of PROC.
3850 Erase echoed commands if exists."
3851 (with-current-buffer (process-buffer proc
)
3852 (goto-char (point-min))
3854 ;; Check whether we need to remove echo output.
3855 (when (and (tramp-get-connection-property proc
"check-remote-echo" nil
)
3856 (re-search-forward tramp-echoed-echo-mark-regexp nil t
))
3857 (let ((begin (match-beginning 0)))
3858 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t
)
3859 ;; Discard echo from remote output.
3860 (tramp-set-connection-property proc
"check-remote-echo" nil
)
3861 (tramp-message proc
5 "echo-mark found")
3863 (delete-region begin
(point))
3864 (goto-char (point-min)))))
3866 (when (or (not (tramp-get-connection-property proc
"check-remote-echo" nil
))
3867 ;; Sometimes, the echo string is suppressed on the remote side.
3869 (substring-no-properties
3870 tramp-echo-mark-marker
3871 0 (min tramp-echo-mark-marker-length
(1- (point-max))))
3872 (buffer-substring-no-properties
3874 (min (+ (point-min) tramp-echo-mark-marker-length
)
3876 ;; No echo to be handled, now we can look for the regexp.
3877 ;; Sometimes, lines are much too long, and we run into a "Stack
3878 ;; overflow in regexp matcher". For example, //DIRED// lines of
3879 ;; directory listings with some thousand files. Therefore, we
3880 ;; look from the end.
3881 (goto-char (point-max))
3882 (ignore-errors (re-search-backward regexp nil t
)))))
3884 (defun tramp-wait-for-regexp (proc timeout regexp
)
3885 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
3886 Expects the output of PROC to be sent to the current buffer. Returns
3887 the string that matched, or nil. Waits indefinitely if TIMEOUT is
3889 (with-current-buffer (process-buffer proc
)
3890 (let ((found (tramp-check-for-regexp proc regexp
)))
3892 (with-timeout (timeout)
3894 (tramp-accept-process-output proc
1)
3895 (unless (process-live-p proc
)
3896 (tramp-error-with-buffer
3897 nil proc
'file-error
"Process has died"))
3898 (setq found
(tramp-check-for-regexp proc regexp
)))))
3901 (tramp-accept-process-output proc
1)
3902 (unless (process-live-p proc
)
3903 (tramp-error-with-buffer
3904 nil proc
'file-error
"Process has died"))
3905 (setq found
(tramp-check-for-regexp proc regexp
)))))
3906 (tramp-message proc
6 "\n%s" (buffer-string))
3910 proc
'file-error
"[[Regexp `%s' not found in %d secs]]"
3912 (tramp-error proc
'file-error
"[[Regexp `%s' not found]]" regexp
)))
3915 ;; It seems that Tru64 Unix does not like it if long strings are sent
3916 ;; to it in one go. (This happens when sending the Perl
3917 ;; `file-attributes' implementation, for instance.) Therefore, we
3918 ;; have this function which sends the string in chunks.
3919 (defun tramp-send-string (vec string
)
3920 "Send the STRING via connection VEC.
3922 The STRING is expected to use Unix line-endings, but the lines sent to
3923 the remote host use line-endings as defined in the variable
3924 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
3925 (let* ((p (tramp-get-connection-process vec
))
3926 (chunksize (tramp-get-connection-property p
"chunksize" nil
)))
3929 vec
'file-error
"Can't send string to remote host -- not logged in"))
3930 (tramp-set-connection-property p
"last-cmd-time" (current-time))
3931 (tramp-message vec
10 "%s" string
)
3932 (with-current-buffer (tramp-get-connection-buffer vec
)
3933 ;; Clean up the buffer. We cannot call `erase-buffer' because
3934 ;; narrowing might be in effect.
3935 (let (buffer-read-only) (delete-region (point-min) (point-max)))
3936 ;; Replace "\n" by `tramp-rsh-end-of-line'.
3939 'identity
(split-string string
"\n") tramp-rsh-end-of-line
))
3940 (unless (or (string= string
"")
3941 (string-equal (substring string -
1) tramp-rsh-end-of-line
))
3942 (setq string
(concat string tramp-rsh-end-of-line
)))
3944 (if (and chunksize
(not (zerop chunksize
)))
3946 (end (length string
)))
3949 vec
10 "Sending chunk from %s to %s"
3950 pos
(min (+ pos chunksize
) end
))
3951 (process-send-string
3952 p
(substring string pos
(min (+ pos chunksize
) end
)))
3953 (setq pos
(+ pos chunksize
))))
3954 (process-send-string p string
)))))
3956 (defun tramp-get-inode (vec)
3957 "Returns the virtual inode number.
3958 If it doesn't exist, generate a new one."
3959 (with-tramp-file-property vec
(tramp-file-name-localname vec
) "inode"
3960 (setq tramp-inodes
(1+ tramp-inodes
))))
3962 (defun tramp-get-device (vec)
3963 "Returns the virtual device number.
3964 If it doesn't exist, generate a new one."
3965 (with-tramp-connection-property (tramp-get-connection-process vec
) "device"
3966 (cons -
1 (setq tramp-devices
(1+ tramp-devices
)))))
3968 (defun tramp-equal-remote (file1 file2
)
3969 "Check, whether the remote parts of FILE1 and FILE2 are identical.
3970 The check depends on method, user and host name of the files. If
3971 one of the components is missing, the default values are used.
3972 The local file name parts of FILE1 and FILE2 are not taken into
3977 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
3979 would yield t. On the other hand, the following check results in nil:
3981 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
3982 (and (tramp-tramp-file-p file1
)
3983 (tramp-tramp-file-p file2
)
3984 (string-equal (file-remote-p file1
) (file-remote-p file2
))))
3986 ;;;###tramp-autoload
3987 (defun tramp-mode-string-to-int (mode-string)
3988 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
3989 (let* (case-fold-search
3990 (mode-chars (string-to-vector mode-string
))
3991 (owner-read (aref mode-chars
1))
3992 (owner-write (aref mode-chars
2))
3993 (owner-execute-or-setid (aref mode-chars
3))
3994 (group-read (aref mode-chars
4))
3995 (group-write (aref mode-chars
5))
3996 (group-execute-or-setid (aref mode-chars
6))
3997 (other-read (aref mode-chars
7))
3998 (other-write (aref mode-chars
8))
3999 (other-execute-or-sticky (aref mode-chars
9)))
4003 ((char-equal owner-read ?r
) (string-to-number "00400" 8))
4004 ((char-equal owner-read ?-
) 0)
4005 (t (error "Second char `%c' must be one of `r-'" owner-read
)))
4007 ((char-equal owner-write ?w
) (string-to-number "00200" 8))
4008 ((char-equal owner-write ?-
) 0)
4009 (t (error "Third char `%c' must be one of `w-'" owner-write
)))
4011 ((char-equal owner-execute-or-setid ?x
) (string-to-number "00100" 8))
4012 ((char-equal owner-execute-or-setid ?S
) (string-to-number "04000" 8))
4013 ((char-equal owner-execute-or-setid ?s
) (string-to-number "04100" 8))
4014 ((char-equal owner-execute-or-setid ?-
) 0)
4015 (t (error "Fourth char `%c' must be one of `xsS-'"
4016 owner-execute-or-setid
)))
4018 ((char-equal group-read ?r
) (string-to-number "00040" 8))
4019 ((char-equal group-read ?-
) 0)
4020 (t (error "Fifth char `%c' must be one of `r-'" group-read
)))
4022 ((char-equal group-write ?w
) (string-to-number "00020" 8))
4023 ((char-equal group-write ?-
) 0)
4024 (t (error "Sixth char `%c' must be one of `w-'" group-write
)))
4026 ((char-equal group-execute-or-setid ?x
) (string-to-number "00010" 8))
4027 ((char-equal group-execute-or-setid ?S
) (string-to-number "02000" 8))
4028 ((char-equal group-execute-or-setid ?s
) (string-to-number "02010" 8))
4029 ((char-equal group-execute-or-setid ?-
) 0)
4030 (t (error "Seventh char `%c' must be one of `xsS-'"
4031 group-execute-or-setid
)))
4033 ((char-equal other-read ?r
) (string-to-number "00004" 8))
4034 ((char-equal other-read ?-
) 0)
4035 (t (error "Eighth char `%c' must be one of `r-'" other-read
)))
4037 ((char-equal other-write ?w
) (string-to-number "00002" 8))
4038 ((char-equal other-write ?-
) 0)
4039 (t (error "Ninth char `%c' must be one of `w-'" other-write
)))
4041 ((char-equal other-execute-or-sticky ?x
) (string-to-number "00001" 8))
4042 ((char-equal other-execute-or-sticky ?T
) (string-to-number "01000" 8))
4043 ((char-equal other-execute-or-sticky ?t
) (string-to-number "01001" 8))
4044 ((char-equal other-execute-or-sticky ?-
) 0)
4045 (t (error "Tenth char `%c' must be one of `xtT-'"
4046 other-execute-or-sticky
)))))))
4048 (defconst tramp-file-mode-type-map
4049 '((0 .
"-") ; Normal file (SVID-v2 and XPG2)
4051 (2 .
"c") ; character device
4052 (3 .
"m") ; multiplexed character device (v7)
4053 (4 .
"d") ; directory
4054 (5 .
"?") ; Named special file (XENIX)
4055 (6 .
"b") ; block device
4056 (7 .
"?") ; multiplexed block device (v7)
4057 (8 .
"-") ; regular file
4058 (9 .
"n") ; network special file (HP-UX)
4059 (10 .
"l") ; symlink
4060 (11 .
"?") ; ACL shadow inode (Solaris, not userspace)
4062 (13 .
"D") ; door special (Solaris)
4063 (14 .
"w")) ; whiteout (BSD)
4064 "A list of file types returned from the `stat' system call.
4065 This is used to map a mode number to a permission string.")
4067 ;;;###tramp-autoload
4068 (defun tramp-file-mode-from-int (mode)
4069 "Turn an integer representing a file mode into an ls(1)-like string."
4071 (assoc (logand (lsh mode -
12) 15) tramp-file-mode-type-map
)))
4072 (user (logand (lsh mode -
6) 7))
4073 (group (logand (lsh mode -
3) 7))
4074 (other (logand (lsh mode -
0) 7))
4075 (suid (> (logand (lsh mode -
9) 4) 0))
4076 (sgid (> (logand (lsh mode -
9) 2) 0))
4077 (sticky (> (logand (lsh mode -
9) 1) 0)))
4078 (setq user
(tramp-file-mode-permissions user suid
"s"))
4079 (setq group
(tramp-file-mode-permissions group sgid
"s"))
4080 (setq other
(tramp-file-mode-permissions other sticky
"t"))
4081 (concat type user group other
)))
4083 (defun tramp-file-mode-permissions (perm suid suid-text
)
4084 "Convert a permission bitset into a string.
4085 This is used internally by `tramp-file-mode-from-int'."
4086 (let ((r (> (logand perm
4) 0))
4087 (w (> (logand perm
2) 0))
4088 (x (> (logand perm
1) 0)))
4089 (concat (or (and r
"r") "-")
4090 (or (and w
"w") "-")
4091 (or (and suid x suid-text
) ; suid, execute
4092 (and suid
(upcase suid-text
)) ; suid, !execute
4093 (and x
"x") "-")))) ; !suid
4095 ;;;###tramp-autoload
4096 (defun tramp-get-local-uid (id-format)
4097 "The uid of the local user, in ID-FORMAT.
4098 ID-FORMAT valid values are `string' and `integer'."
4099 (if (equal id-format
'integer
) (user-uid) (user-login-name)))
4101 ;;;###tramp-autoload
4102 (defun tramp-get-local-gid (id-format)
4103 "The gid of the local user, in ID-FORMAT.
4104 ID-FORMAT valid values are `string' and `integer'."
4105 ;; `group-gid' has been introduced with Emacs 24.4.
4106 (if (and (fboundp 'group-gid
) (equal id-format
'integer
))
4107 (tramp-compat-funcall 'group-gid
)
4108 (tramp-compat-file-attribute-group-id (file-attributes "~/" id-format
))))
4110 (defun tramp-get-local-locale (&optional vec
)
4111 "Determine locale, supporting UTF8 if possible.
4112 VEC is used for tracing."
4113 ;; We use key nil for local connection properties.
4114 (with-tramp-connection-property nil
"locale"
4115 (let ((candidates '("en_US.utf8" "C.utf8" "en_US.UTF-8"))
4118 (unless (or (memq system-type
'(windows-nt))
4119 (not (zerop (tramp-call-process
4120 nil
"locale" nil t nil
"-a"))))
4122 (goto-char (point-min))
4123 (if (string-match (format "^%s\r?$" (regexp-quote (car candidates
)))
4125 (setq locale
(car candidates
)
4127 (setq candidates
(cdr candidates
))))))
4129 (when vec
(tramp-message vec
7 "locale %s" (or locale
"C")))
4132 ;;;###tramp-autoload
4133 (defun tramp-check-cached-permissions (vec access
)
4134 "Check `file-attributes' caches for VEC.
4135 Return t if according to the cache access type ACCESS is known to
4141 ((eq ?x access
) 3))))
4142 (dolist (suffix '("string" "integer") result
)
4149 (tramp-get-file-property
4150 vec
(tramp-file-name-localname vec
)
4151 (concat "file-attributes-" suffix
) nil
)
4153 (tramp-make-tramp-file-name vec
) (intern suffix
))))
4155 (tramp-get-connection-property
4156 vec
(concat "uid-" suffix
) nil
))
4158 (tramp-get-connection-property
4159 vec
(concat "gid-" suffix
) nil
))
4161 (if (string-equal suffix
"string")
4162 tramp-unknown-id-string tramp-unknown-id-integer
)))
4167 (eq t
(tramp-compat-file-attribute-type file-attr
))
4168 (null (tramp-compat-file-attribute-type file-attr
)))
4170 ;; World accessible.
4172 (aref (tramp-compat-file-attribute-modes file-attr
)
4174 ;; User accessible and owned by user.
4177 (aref (tramp-compat-file-attribute-modes file-attr
) offset
))
4178 (or (equal remote-uid
4179 (tramp-compat-file-attribute-user-id file-attr
))
4181 (tramp-compat-file-attribute-user-id file-attr
))))
4182 ;; Group accessible and owned by user's principal group.
4185 (aref (tramp-compat-file-attribute-modes file-attr
)
4187 (or (equal remote-gid
4188 (tramp-compat-file-attribute-group-id file-attr
))
4190 (tramp-compat-file-attribute-group-id
4191 file-attr
))))))))))))
4193 ;;;###tramp-autoload
4194 (defun tramp-local-host-p (vec)
4195 "Return t if this points to the local host, nil otherwise."
4196 (let ((host (tramp-file-name-host vec
))
4197 (port (tramp-file-name-port vec
)))
4200 (string-match tramp-local-host-regexp host
)
4201 ;; A port is an indication for an ssh tunnel or alike.
4203 ;; The method shall be applied to one of the shell file name
4204 ;; handlers. `tramp-local-host-p' is also called for "smb" and
4205 ;; alike, where it must fail.
4206 (tramp-get-method-parameter vec
'tramp-login-program
)
4207 ;; The local temp directory must be writable for the other user.
4209 (tramp-make-tramp-file-name
4210 vec
(tramp-compat-temporary-file-directory) 'nohop
))
4211 ;; On some systems, chown runs only for root.
4212 (or (zerop (user-uid))
4213 ;; This is defined in tramp-sh.el. Let's assume this is
4215 (zerop (tramp-compat-funcall 'tramp-get-remote-uid vec
'integer
))))))
4217 (defun tramp-get-remote-tmpdir (vec)
4218 "Return directory for temporary files on the remote host identified by VEC."
4219 (with-tramp-connection-property vec
"tmpdir"
4221 (tramp-make-tramp-file-name
4222 vec
(or (tramp-get-method-parameter vec
'tramp-tmpdir
) "/tmp"))))
4223 (or (and (file-directory-p dir
) (file-writable-p dir
)
4224 (file-remote-p dir
'localname
))
4225 (tramp-error vec
'file-error
"Directory %s not accessible" dir
))
4228 ;;;###tramp-autoload
4229 (defun tramp-make-tramp-temp-file (vec)
4230 "Create a temporary file on the remote host identified by VEC.
4231 Return the local name of the temporary file."
4232 (let ((prefix (expand-file-name
4233 tramp-temp-name-prefix
(tramp-get-remote-tmpdir vec
)))
4236 ;; `make-temp-file' would be the natural choice for
4237 ;; implementation. But it calls `write-region' internally,
4238 ;; which also needs a temporary file - we would end in an
4240 (setq result
(make-temp-name prefix
))
4241 (if (file-exists-p result
)
4243 ;; This creates the file by side effect.
4244 (set-file-times result
)
4245 (set-file-modes result
(string-to-number "0700" 8))))
4247 ;; Return the local part.
4248 (with-parsed-tramp-file-name result nil localname
)))
4250 (defun tramp-delete-temp-file-function ()
4251 "Remove temporary files related to current buffer."
4252 (when (stringp tramp-temp-buffer-file-name
)
4253 (ignore-errors (delete-file tramp-temp-buffer-file-name
))))
4255 (add-hook 'kill-buffer-hook
'tramp-delete-temp-file-function
)
4256 (add-hook 'tramp-unload-hook
4258 (remove-hook 'kill-buffer-hook
4259 'tramp-delete-temp-file-function
)))
4261 (defun tramp-handle-make-auto-save-file-name ()
4262 "Like `make-auto-save-file-name' for Tramp files.
4263 Returns a file name in `tramp-auto-save-directory' for autosaving
4264 this file, if that variable is non-nil."
4265 (when (stringp tramp-auto-save-directory
)
4266 (setq tramp-auto-save-directory
4267 (expand-file-name tramp-auto-save-directory
)))
4268 ;; Create directory.
4269 (unless (or (null tramp-auto-save-directory
)
4270 (file-exists-p tramp-auto-save-directory
))
4271 (make-directory tramp-auto-save-directory t
))
4274 (if (and (stringp tramp-auto-save-directory
)
4275 (file-remote-p tramp-auto-save-directory
))
4278 (auto-save-file-name-transforms
4279 (if (null tramp-auto-save-directory
)
4280 auto-save-file-name-transforms
))
4282 (if (null tramp-auto-save-directory
)
4285 (tramp-subst-strs-in-string
4292 (tramp-compat-file-name-unquote (buffer-file-name)))
4293 tramp-auto-save-directory
))))
4294 ;; Run plain `make-auto-save-file-name'.
4295 (tramp-run-real-handler 'make-auto-save-file-name nil
)))
4297 (defun tramp-subst-strs-in-string (alist string
)
4298 "Replace all occurrences of the string FROM with TO in STRING.
4299 ALIST is of the form ((FROM . TO) ...)."
4302 (let* ((pr (car alist
))
4305 (while (string-match (regexp-quote from
) string
)
4306 (setq string
(replace-match to t t string
)))
4307 (setq alist
(cdr alist
))))
4310 (defun tramp-handle-temporary-file-directory ()
4311 "Like `temporary-file-directory' for Tramp files."
4313 (dolist (dir `(,(ignore-errors
4314 (tramp-get-remote-tmpdir
4315 (tramp-dissect-file-name default-directory
)))
4316 ,default-directory
))
4317 (when (and (stringp dir
) (file-directory-p dir
) (file-writable-p dir
))
4318 (throw 'result
(expand-file-name dir
))))))
4320 (defun tramp-handle-make-nearby-temp-file (prefix &optional dir-flag suffix
)
4321 "Like `make-nearby-temp-file' for Tramp files."
4322 (let ((temporary-file-directory
4323 (tramp-compat-temporary-file-directory-function)))
4324 (make-temp-file prefix dir-flag suffix
)))
4326 ;;; Compatibility functions section:
4328 (defun tramp-call-process
4329 (vec program
&optional infile destination display
&rest args
)
4330 "Calls `call-process' on the local host.
4331 It always returns a return code. The Lisp error raised when
4332 PROGRAM is nil is trapped also, returning 1. Furthermore, traces
4333 are written with verbosity of 6."
4334 (let ((default-directory (tramp-compat-temporary-file-directory))
4335 (destination (if (eq destination t
) (current-buffer) destination
))
4336 output error result
)
4338 vec
6 "`%s %s' %s %s"
4339 program
(mapconcat 'identity args
" ") infile destination
)
4344 'call-process program infile
(or destination t
) display args
))
4345 ;; `result' could also be an error string.
4346 (when (stringp result
)
4349 (with-current-buffer
4350 (if (bufferp destination
) destination
(current-buffer))
4351 (setq output
(buffer-string))))
4353 (setq error
(error-message-string err
)
4355 (if (zerop (length error
))
4356 (tramp-message vec
6 "%d\n%s" result output
)
4357 (tramp-message vec
6 "%d\n%s\n%s" result output error
))
4360 (defun tramp-call-process-region
4361 (vec start end program
&optional delete buffer display
&rest args
)
4362 "Calls `call-process-region' on the local host.
4363 It always returns a return code. The Lisp error raised when
4364 PROGRAM is nil is trapped also, returning 1. Furthermore, traces
4365 are written with verbosity of 6."
4366 (let ((default-directory (tramp-compat-temporary-file-directory))
4367 (buffer (if (eq buffer t
) (current-buffer) buffer
))
4370 vec
6 "`%s %s' %s %s %s %s"
4371 program
(mapconcat 'identity args
" ") start end delete buffer
)
4376 'call-process-region
4377 start end program delete buffer display args
))
4378 ;; `result' could also be an error string.
4379 (when (stringp result
)
4380 (signal 'file-error
(list result
)))
4381 (with-current-buffer (if (bufferp buffer
) buffer
(current-buffer))
4383 (tramp-message vec
6 "%d" result
)
4384 (tramp-message vec
6 "%d\n%s" result
(buffer-string)))))
4387 (tramp-message vec
6 "%d\n%s" result
(error-message-string err
))))
4390 ;;;###tramp-autoload
4391 (defun tramp-read-passwd (proc &optional prompt
)
4392 "Read a password from user (compat function).
4393 Consults the auth-source package.
4394 Invokes `password-read' if available, `read-passwd' else."
4395 (let* ((case-fold-search t
)
4396 (key (tramp-make-tramp-file-name
4397 ;; In tramp-sh.el, we must use "password-vector" due to
4399 (tramp-get-connection-property
4400 proc
"password-vector"
4401 ;; All other backends simply use "vector".
4402 (tramp-get-connection-property proc
"vector" nil
))
4406 (with-current-buffer (process-buffer proc
)
4407 (tramp-check-for-regexp proc tramp-password-prompt-regexp
)
4408 (format "%s for %s " (capitalize (match-string 1)) key
))))
4409 ;; We suspend the timers while reading the password.
4410 (stimers (with-timeout-suspend))
4411 auth-info auth-passwd
)
4414 (with-parsed-tramp-file-name key nil
4416 (or user
(tramp-get-connection-property key
"login-as" nil
)))
4419 ;; See if auth-sources contains something useful.
4421 (and (tramp-get-connection-property
4422 v
"first-password-request" nil
)
4423 ;; Try with Tramp's current method.
4429 (concat user tramp-prefix-domain-format domain
)
4433 (concat host tramp-prefix-port-format port
)
4436 :require
(cons :secret
(and user
'(:user
))))
4437 auth-passwd
(plist-get
4438 (nth 0 auth-info
) :secret
)
4439 auth-passwd
(if (functionp auth-passwd
)
4440 (funcall auth-passwd
)
4442 ;; Try the password cache.
4443 (let ((password (password-read pw-prompt key
)))
4444 (password-cache-add key password
)
4446 ;; Else, get the password interactively.
4447 (read-passwd pw-prompt
))
4448 (tramp-set-connection-property v
"first-password-request" nil
)))
4449 ;; Reenable the timers.
4450 (with-timeout-unsuspend stimers
))))
4452 ;;;###tramp-autoload
4453 (defun tramp-clear-passwd (vec)
4454 "Clear password cache for connection related to VEC."
4455 (let ((method (tramp-file-name-method vec
))
4456 (user-domain (tramp-file-name-user-domain vec
))
4457 (host-port (tramp-file-name-host-port vec
))
4458 (hop (tramp-file-name-hop vec
)))
4460 ;; Clear also the passwords of the hops.
4462 (tramp-dissect-file-name
4465 (replace-regexp-in-string
4466 (concat tramp-postfix-hop-regexp
"$")
4467 tramp-postfix-host-format hop
)))))
4469 `(:max
1 ,(and user-domain
:user
) ,user-domain
4470 :host
,host-port
:port
,method
))
4471 (password-cache-remove (tramp-make-tramp-file-name vec
'noloc
'nohop
))))
4473 ;; Snarfed code from time-date.el.
4475 (defconst tramp-half-a-year
'(241 17024)
4476 "Evaluated by \"(days-to-time 183)\".")
4478 ;;;###tramp-autoload
4479 (defun tramp-time-diff (t1 t2
)
4480 "Return the difference between the two times, in seconds.
4481 T1 and T2 are time values (as returned by `current-time' for example)."
4482 ;; Starting with Emacs 25.1, we could change this to use `time-subtract'.
4483 (float-time (tramp-compat-funcall 'subtract-time t1 t2
)))
4485 (defun tramp-unquote-shell-quote-argument (s)
4486 "Remove quotation prefix \"/:\" from string S, and quote it then for shell."
4487 (shell-quote-argument (tramp-compat-file-name-unquote s
)))
4489 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
4490 ;; does not deal well with newline characters. Newline is replaced by
4491 ;; backslash newline. But if, say, the string `a backslash newline b'
4492 ;; is passed to a shell, the shell will expand this into "ab",
4493 ;; completely omitting the newline. This is not what was intended.
4494 ;; It does not appear to be possible to make the function
4495 ;; `shell-quote-argument' work with newlines without making it
4496 ;; dependent on the shell used. But within this package, we know that
4497 ;; we will always use a Bourne-like shell, so we use an approach which
4500 ;; The approach is simple: we call `shell-quote-argument', then
4501 ;; massage the newline part of the result.
4503 ;; This function should produce a string which is grokked by a Unix
4504 ;; shell, even if the Emacs is running on Windows. Since this is the
4505 ;; kludges section, we bind `system-type' in such a way that
4506 ;; `shell-quote-argument' behaves as if on Unix.
4508 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
4509 ;; function to work with Bourne-like shells.
4510 ;;;###tramp-autoload
4511 (defun tramp-shell-quote-argument (s)
4512 "Similar to `shell-quote-argument', but groks newlines.
4513 Only works for Bourne-like shells."
4514 (let ((system-type 'not-windows
))
4516 (let ((result (tramp-unquote-shell-quote-argument s
))
4517 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line
))))
4518 (when (and (>= (length result
) 2)
4519 (string= (substring result
0 2) "\\~"))
4520 (setq result
(substring result
1)))
4521 (while (string-match nl result
)
4522 (setq result
(replace-match (format "'%s'" tramp-rsh-end-of-line
)
4526 ;;; Signal handling. This works for remote processes, which have set
4527 ;;; the process property `remote-pid'.
4529 (defun tramp-interrupt-process (&optional process _current-group
)
4530 "Interrupt remote process PROC."
4531 ;; CURRENT-GROUP is not implemented yet.
4533 ((processp process
) process
)
4534 ((bufferp process
) (get-buffer-process process
))
4535 ((stringp process
) (or (get-process process
)
4536 (get-buffer-process process
)))
4537 ((null process
) (get-buffer-process (current-buffer)))
4540 ;; If it's a Tramp process, send the INT signal remotely.
4541 (when (and (processp proc
) (setq pid
(process-get proc
'remote-pid
)))
4542 (if (not (process-live-p proc
))
4543 (tramp-error proc
'error
"Process %s is not active" proc
)
4544 (tramp-message proc
5 "Interrupt process %s with pid %s" proc pid
)
4545 ;; This is for tramp-sh.el. Other backends do not support this (yet).
4546 (tramp-compat-funcall
4548 (tramp-get-connection-property proc
"vector" nil
)
4549 (format "kill -2 %d" pid
))
4550 ;; Wait, until the process has disappeared.
4552 (1 (tramp-error proc
'error
"Process %s did not interrupt" proc
))
4553 (while (process-live-p proc
)
4554 ;; We cannot run `tramp-accept-process-output', it blocks timers.
4555 (accept-process-output proc
0.1)))
4559 ;; `interrupt-process-functions' exists since Emacs 26.1.
4560 (when (boundp 'interrupt-process-functions
)
4561 (add-hook 'interrupt-process-functions
'tramp-interrupt-process
)
4565 (remove-hook 'interrupt-process-functions
'tramp-interrupt-process
))))
4567 ;;; Integration of eshell.el:
4569 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
4570 ;; when `default-directory' points to another host.
4571 (defun tramp-eshell-directory-change ()
4572 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
4573 (setq eshell-path-env
4574 (if (tramp-tramp-file-p default-directory
)
4575 (with-parsed-tramp-file-name default-directory nil
4579 ;; When `tramp-own-remote-path' is in `tramp-remote-path',
4580 ;; the remote path is only set in the session cache.
4581 (tramp-get-connection-property
4582 (tramp-get-connection-process v
) "remote-path" nil
)
4583 (tramp-get-connection-property v
"remote-path" nil
))
4587 (eval-after-load "esh-util"
4589 (add-hook 'eshell-mode-hook
4590 'tramp-eshell-directory-change
)
4591 (add-hook 'eshell-directory-change-hook
4592 'tramp-eshell-directory-change
)
4593 (add-hook 'tramp-unload-hook
4595 (remove-hook 'eshell-mode-hook
4596 'tramp-eshell-directory-change
)
4597 (remove-hook 'eshell-directory-change-hook
4598 'tramp-eshell-directory-change
)))))
4600 ;; Checklist for `tramp-unload-hook'
4601 ;; - Unload all `tramp-*' packages
4602 ;; - Reset `file-name-handler-alist'
4603 ;; - Cleanup hooks where Tramp functions are in
4604 ;; - Cleanup advised functions
4605 ;; - Cleanup autoloads
4607 (defun tramp-unload-tramp ()
4608 "Discard Tramp from loading remote files."
4610 ;; ange-ftp settings must be enabled.
4611 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp
)
4612 ;; Maybe it's not loaded yet.
4613 (ignore-errors (unload-feature 'tramp
'force
)))
4619 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
4621 ;; * Better error checking. At least whenever we see something
4622 ;; strange when doing zerop, we should kill the process and start
4623 ;; again. (Greg Stark)
4625 ;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
4627 ;; * I was wondering if it would be possible to use tramp even if I'm
4628 ;; actually using sshfs. But when I launch a command I would like
4629 ;; to get it executed on the remote machine where the files really
4630 ;; are. (Andrea Crotti)
4632 ;; * Run emerge on two remote files. Bug is described here:
4633 ;; <https://www.mail-archive.com/tramp-devel@nongnu.org/msg01041.html>.
4636 ;; * Refactor code from different handlers. Start with
4637 ;; *-process-file. One idea is to generalize `tramp-send-command'
4638 ;; and friends, for most of the handlers this is the major
4639 ;; difference between the different backends. Other handlers but
4640 ;; *-process-file would profit from this as well.
4642 ;;; tramp.el ends here