tramp-cmds.el 12 KB

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