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