; Merge from origin/emacs-25
[emacs.git] / lisp / net / tramp-cmds.el
blob856011fc0ee9844af639b2548d144bc87410e853
1 ;;; tramp-cmds.el --- Interactive commands for Tramp
3 ;; Copyright (C) 2007-2016 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This package provides all interactive commands which are related
27 ;; to Tramp.
29 ;;; Code:
31 (require 'tramp)
33 ;; Pacify byte-compiler.
34 (declare-function mml-mode "mml")
35 (declare-function mml-insert-empty-tag "mml")
36 (declare-function reporter-dump-variable "reporter")
37 (defvar reporter-eval-buffer)
38 (defvar reporter-prompt-for-summary-p)
40 (defun tramp-list-tramp-buffers ()
41 "Return a list of all Tramp connection buffers."
42 (append
43 (all-completions
44 "*tramp" (mapcar 'list (mapcar 'buffer-name (buffer-list))))
45 (all-completions
46 "*debug tramp" (mapcar 'list (mapcar 'buffer-name (buffer-list))))))
48 (defun tramp-list-remote-buffers ()
49 "Return a list of all buffers with remote default-directory."
50 (delq
51 nil
52 (mapcar
53 (lambda (x)
54 (with-current-buffer x (when (tramp-tramp-file-p default-directory) x)))
55 (buffer-list))))
57 ;;;###tramp-autoload
58 (defun tramp-cleanup-connection (vec &optional keep-debug keep-password)
59 "Flush all connection related objects.
60 This includes password cache, file cache, connection cache,
61 buffers. KEEP-DEBUG non-nil preserves the debug buffer.
62 KEEP-PASSWORD non-nil preserves the password cache.
63 When called interactively, a Tramp connection has to be selected."
64 (interactive
65 ;; When interactive, select the Tramp remote identification.
66 ;; Return nil when there is no Tramp connection.
67 (list
68 (let ((connections
69 (mapcar
70 (lambda (x)
71 (tramp-make-tramp-file-name
72 (tramp-file-name-method x)
73 (tramp-file-name-user x)
74 (tramp-file-name-host x)
75 (tramp-file-name-localname x)))
76 (tramp-list-connections)))
77 name)
79 (when connections
80 (setq name
81 (completing-read
82 "Enter Tramp connection: " connections nil t
83 (try-completion "" connections)))
84 (and (tramp-tramp-file-p name) (tramp-dissect-file-name name))))
85 nil nil))
87 (if (not vec)
88 ;; Nothing to do.
89 (message "No Tramp connection found.")
91 ;; Flush password cache.
92 (unless keep-password (tramp-clear-passwd vec))
94 ;; Cleanup `tramp-current-connection'. Otherwise, we would be
95 ;; suppressed in the test suite. We use `keep-password' as
96 ;; indicator; it is not worth to add a new argument.
97 (when keep-password (setq tramp-current-connection nil))
99 ;; Flush file cache.
100 (tramp-flush-directory-property vec "")
102 ;; Flush connection cache.
103 (when (processp (tramp-get-connection-process vec))
104 (delete-process (tramp-get-connection-process vec))
105 (tramp-flush-connection-property (tramp-get-connection-process vec)))
106 (tramp-flush-connection-property vec)
108 ;; Remove buffers.
109 (dolist
110 (buf (list (get-buffer (tramp-buffer-name vec))
111 (unless keep-debug
112 (get-buffer (tramp-debug-buffer-name vec)))
113 (tramp-get-connection-property vec "process-buffer" nil)))
114 (when (bufferp buf) (kill-buffer buf)))))
116 ;;;###tramp-autoload
117 (defun tramp-cleanup-this-connection ()
118 "Flush all connection related objects of the current buffer's connection."
119 (interactive)
120 (and (tramp-tramp-file-p default-directory)
121 (tramp-cleanup-connection
122 (tramp-dissect-file-name default-directory 'noexpand))))
124 ;;;###tramp-autoload
125 (defun tramp-cleanup-all-connections ()
126 "Flush all Tramp internal objects.
127 This includes password cache, file cache, connection cache, buffers."
128 (interactive)
130 ;; Unlock Tramp.
131 (setq tramp-locked nil)
133 ;; Flush password cache.
134 (password-reset)
136 ;; Flush file and connection cache.
137 (clrhash tramp-cache-data)
139 ;; Remove buffers.
140 (dolist (name (tramp-list-tramp-buffers))
141 (when (bufferp (get-buffer name)) (kill-buffer name))))
143 ;;;###tramp-autoload
144 (defun tramp-cleanup-all-buffers ()
145 "Kill all remote buffers."
146 (interactive)
148 ;; Remove all Tramp related connections.
149 (tramp-cleanup-all-connections)
151 ;; Remove all buffers with a remote default-directory.
152 (dolist (name (tramp-list-remote-buffers))
153 (when (bufferp (get-buffer name)) (kill-buffer name))))
155 ;; Tramp version is useful in a number of situations.
157 ;;;###tramp-autoload
158 (defun tramp-version (arg)
159 "Print version number of tramp.el in minibuffer or current buffer."
160 (interactive "P")
161 (if arg (insert tramp-version) (message tramp-version)))
163 ;; Make the "reporter" functionality available for making bug reports about
164 ;; the package. A most useful piece of code.
166 (autoload 'reporter-submit-bug-report "reporter")
168 ;;;###tramp-autoload
169 (defun tramp-bug ()
170 "Submit a bug report to the Tramp developers."
171 (interactive)
172 (catch 'dont-send
173 (let ((reporter-prompt-for-summary-p t))
174 (reporter-submit-bug-report
175 tramp-bug-report-address ; to-address
176 (format "tramp (%s)" tramp-version) ; package name and version
177 (sort
178 (delq nil (mapcar
179 (lambda (x)
180 (and x (boundp x) (cons x 'tramp-reporter-dump-variable)))
181 (append
182 (mapcar 'intern (all-completions "tramp-" obarray 'boundp))
183 ;; Non-tramp variables of interest.
184 '(shell-prompt-pattern
185 backup-by-copying
186 backup-by-copying-when-linked
187 backup-by-copying-when-mismatch
188 backup-by-copying-when-privileged-mismatch
189 backup-directory-alist
190 password-cache
191 password-cache-expiry
192 remote-file-name-inhibit-cache
193 file-name-handler-alist))))
194 (lambda (x y) (string< (symbol-name (car x)) (symbol-name (car y)))))
196 'tramp-load-report-modules ; pre-hook
197 'tramp-append-tramp-buffers ; post-hook
198 (propertize
199 "\n" 'display "\
200 Enter your bug report in this message, including as much detail
201 as you possibly can about the problem, what you did to cause it
202 and what the local and remote machines are.
204 If you can give a simple set of instructions to make this bug
205 happen reliably, please include those. Thank you for helping
206 kill bugs in Tramp.
208 Before reproducing the bug, you might apply
210 M-x tramp-cleanup-all-connections
212 This allows us to investigate from a clean environment. Another
213 useful thing to do is to put
215 (setq tramp-verbose 9)
217 in your init file and to repeat the bug. Then, include the
218 contents of the *tramp/foo* buffer and the *debug tramp/foo*
219 buffer in your bug report.
221 --bug report follows this line--
222 ")))))
224 (defun tramp-reporter-dump-variable (varsym mailbuf)
225 "Pretty-print the value of the variable in symbol VARSYM."
226 (let* ((reporter-eval-buffer (symbol-value 'reporter-eval-buffer))
227 (val (with-current-buffer reporter-eval-buffer
228 (symbol-value varsym))))
230 (if (hash-table-p val)
231 ;; Pretty print the cache.
232 (set varsym (read (format "(%s)" (tramp-cache-print val))))
233 ;; There are non-7bit characters to be masked.
234 (when (and (boundp 'mm-7bit-chars)
235 (stringp val)
236 (string-match
237 (concat "[^" (symbol-value 'mm-7bit-chars) "]") val))
238 (with-current-buffer reporter-eval-buffer
239 (set
240 varsym
241 (format
242 "(decode-coding-string (base64-decode-string \"%s\") 'raw-text)"
243 (base64-encode-string (encode-coding-string val 'raw-text)))))))
245 ;; Dump variable.
246 (reporter-dump-variable varsym mailbuf)
248 (unless (hash-table-p val)
249 ;; Remove string quotation.
250 (forward-line -1)
251 (when (looking-at
252 (concat "\\(^.*\\)" "\"" ;; \1 "
253 "\\((base64-decode-string \\)" "\\\\" ;; \2 \
254 "\\(\".*\\)" "\\\\" ;; \3 \
255 "\\(\")\\)" "\"$")) ;; \4 "
256 (replace-match "\\1\\2\\3\\4")
257 (beginning-of-line)
258 (insert " ;; Variable encoded due to non-printable characters.\n"))
259 (forward-line 1))
261 ;; Reset VARSYM to old value.
262 (with-current-buffer reporter-eval-buffer
263 (set varsym val))))
265 (defun tramp-load-report-modules ()
266 "Load needed modules for reporting."
267 (message-mode)
268 (mml-mode t))
270 (defun tramp-append-tramp-buffers ()
271 "Append Tramp buffers and buffer local variables into the bug report."
272 (goto-char (point-max))
274 ;; Dump buffer local variables.
275 (insert "\nlocal variables:\n================")
276 (dolist (buffer
277 (delq nil
278 (mapcar
279 (lambda (b)
280 (when (string-match "\\*tramp/" (buffer-name b)) b))
281 (buffer-list))))
282 (let ((reporter-eval-buffer buffer)
283 (elbuf (get-buffer-create " *tmp-reporter-buffer*")))
284 (with-current-buffer elbuf
285 (emacs-lisp-mode)
286 (erase-buffer)
287 (insert (format "\n;; %s\n(setq-local\n" (buffer-name buffer)))
288 (lisp-indent-line)
289 (dolist
290 (varsym
291 (sort
292 (append
293 (mapcar
294 'intern
295 (all-completions "tramp-" (buffer-local-variables buffer)))
296 ;; Non-tramp variables of interest.
297 '(default-directory))
298 'string<))
299 (reporter-dump-variable varsym elbuf))
300 (lisp-indent-line)
301 (insert ")\n"))
302 (insert-buffer-substring elbuf)))
304 ;; Dump load-path shadows.
305 (insert "\nload-path shadows:\n==================\n")
306 (ignore-errors
307 (mapc
308 (lambda (x) (when (string-match "tramp" x) (insert x "\n")))
309 (split-string (list-load-path-shadows t) "\n")))
311 ;; Append buffers only when we are in message mode.
312 (when (and
313 (eq major-mode 'message-mode)
314 (boundp 'mml-mode)
315 (symbol-value 'mml-mode))
317 (let ((tramp-buf-regexp "\\*\\(debug \\)?tramp/")
318 (buffer-list (tramp-list-tramp-buffers))
319 (curbuf (current-buffer)))
321 ;; There is at least one Tramp buffer.
322 (when buffer-list
323 (switch-to-buffer (list-buffers-noselect nil))
324 (delete-other-windows)
325 (setq buffer-read-only nil)
326 (goto-char (point-min))
327 (while (not (eobp))
328 (if (re-search-forward tramp-buf-regexp (point-at-eol) t)
329 (forward-line 1)
330 (forward-line 0)
331 (let ((start (point)))
332 (forward-line 1)
333 (kill-region start (point)))))
334 (insert "
335 The buffer(s) above will be appended to this message. If you
336 don't want to append a buffer because it contains sensitive data,
337 or because the buffer is too large, you should delete the
338 respective buffer. The buffer(s) will contain user and host
339 names. Passwords will never be included there.")
341 (when (>= tramp-verbose 6)
342 (insert "\n\n")
343 (let ((start (point)))
344 (insert "\
345 Please note that you have set `tramp-verbose' to a value of at
346 least 6. Therefore, the contents of files might be included in
347 the debug buffer(s).")
348 (add-text-properties start (point) (list 'face 'italic))))
350 (set-buffer-modified-p nil)
351 (setq buffer-read-only t)
352 (goto-char (point-min))
354 (if (y-or-n-p "Do you want to append the buffer(s)? ")
355 ;; OK, let's send. First we delete the buffer list.
356 (progn
357 (kill-buffer nil)
358 (switch-to-buffer curbuf)
359 (goto-char (point-max))
360 (insert (propertize "\n" 'display "\n\
361 This is a special notion of the `gnus/message' package. If you
362 use another mail agent (by copying the contents of this buffer)
363 please ensure that the buffers are attached to your email.\n\n"))
364 (dolist (buffer buffer-list)
365 (mml-insert-empty-tag
366 'part 'type "text/plain"
367 'encoding "base64" 'disposition "attachment" 'buffer buffer
368 'description buffer))
369 (set-buffer-modified-p nil))
371 ;; Don't send. Delete the message buffer.
372 (set-buffer curbuf)
373 (set-buffer-modified-p nil)
374 (kill-buffer nil)
375 (throw 'dont-send nil))))))
377 (defalias 'tramp-submit-bug 'tramp-bug)
379 (add-hook 'tramp-unload-hook
380 (lambda () (unload-feature 'tramp-cmds 'force)))
382 (provide 'tramp-cmds)
384 ;;; TODO:
386 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
387 ;; * WIBNI there was an interactive command prompting for Tramp
388 ;; method, hostname, username and filename and translates the user
389 ;; input into the correct filename syntax (depending on the Emacs
390 ;; flavor) (Reiner Steib)
391 ;; * Let the user edit the connection properties interactively.
392 ;; Something like `gnus-server-edit-server' in Gnus' *Server* buffer.
394 ;;; tramp-cmds.el ends here