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