[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Reopening ssh file with sudo
From: |
Michael Albinus |
Subject: |
Re: Reopening ssh file with sudo |
Date: |
Sun, 10 Sep 2023 20:02:01 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Joseph Garvin <k04jg02@gmail.com> writes:
Hi Joseph,
> Yeah, I haven't tried automating tramp much before so I'm not super
> familiar with what best practice is here. I'm on 29.1.
OK, so I've used the opportunity to write a more stable command
`tramp-revert-buffer-with-sudo'. Works for buffers visitng a file, and
for dired buffers. Could you pls check?
--8<---------------cut here---------------start------------->8---
(defcustom tramp-file-name-with-method "sudo"
"Which method to be used in `tramp-file-name-with-sudo'."
:group 'tramp
:type '(choice (const "su")
(const "sudo")
(const "doas")
(const "ksu")))
(defun tramp-file-name-with-sudo (filename)
"Convert FILENAME into a multi-hop file name with \"sudo\".
An alternative method could be chosen with `tramp-file-name-with-method'."
(setq filename (expand-file-name filename))
(if (tramp-tramp-file-p filename)
(with-parsed-tramp-file-name filename nil
(if (and (tramp-multi-hop-p v)
(not (string-equal method tramp-file-name-with-method)))
(tramp-make-tramp-file-name
(make-tramp-file-name
:method (tramp-find-method tramp-file-name-with-method nil host)
:user (tramp-find-user tramp-file-name-with-method nil host)
:host (tramp-find-host tramp-file-name-with-method nil host)
:localname localname :hop (tramp-make-tramp-hop-name v)))
(tramp-user-error v "Multi-hop with `%s' not applicable" method)))
(tramp-make-tramp-file-name
(make-tramp-file-name
:method tramp-file-name-with-method :localname filename))))
(defun tramp-revert-buffer-with-sudo ()
"Revert current buffer to visit with \"sudo\" permissions.
An alternative method could be chosen with `tramp-file-name-with-method'.
If the buffer visits a file, the file is replaced.
If the buffer runs `dired', the buffer is reverted."
(interactive)
(cond
((buffer-file-name)
(find-alternate-file (tramp-file-name-with-sudo (buffer-name))))
((derived-mode-p 'dired-mode)
(setq default-directory (tramp-file-name-with-sudo default-directory)
list-buffers-directory
(tramp-file-name-with-sudo list-buffers-directory))
(if (consp dired-directory)
(setcar
dired-directory (tramp-file-name-with-sudo (car dired-directory)))
(setq dired-directory (tramp-file-name-with-sudo dired-directory)))
(revert-buffer))))
--8<---------------cut here---------------end--------------->8---
Best regards, Michael.