tramp-file-name-with-method can be set connection-local

* doc/misc/tramp.texi (Ad-hoc multi-hops):
tramp-file-name-with-method can be set connection-local.

* etc/NEWS: tramp-file-name-with-method can be set connection-local.
Presentational fixes and improvements.

* lisp/net/tramp-cmds.el (tramp-get-file-name-with-method): New defun.
(with-tramp-file-name-with-method, tramp-file-name-with-sudo): Use it.
(tramp-dired-find-file-with-sudo): Fix docstring.
This commit is contained in:
Michael Albinus 2025-07-13 15:48:54 +02:00
parent 9d750c7e80
commit f1aa9482d1
3 changed files with 46 additions and 38 deletions

View file

@ -3949,7 +3949,7 @@ This is performed with @option{sudo} permissions.
The method used in @code{tramp-revert-buffer-with-sudo} and
@code{tramp-dired-find-file-with-sudo}. It defaults to @option{sudo},
other valid methods are @option{su}, @option{doas}, @option{run0}, and
@option{ksu}.
@option{ksu}. The value can be set connection-local.
If a command is called with a prefix argument @kbd{C-u}, the option's
value is read interactively.

View file

@ -355,9 +355,9 @@ retains the existing behavior of inserting parentheses.
+++
*** New minor mode 'mode-line-invisible-mode'.
This minor mode makes the mode-line of the current buffer invisible.
This minor mode makes the mode line of the current buffer invisible.
The command 'mode-line-invisible-mode' toggles the visibility of the
current-buffer's mode-line. The default is to show the mode line of
current-buffer's mode line. The default is to show the mode line of
every buffer.
** Tab Bars and Tab Lines
@ -525,7 +525,7 @@ CSS attribute continues to be set as before.
This change should result in more consistent display of SVG images.
To use the :foreground or current text color ensure the 'fill' attribute
To use the ':foreground' or current text color ensure the 'fill' attribute
in the SVG is set to 'currentcolor', or set the image spec's ':css'
value to 'svg {fill: currentcolor;}'.
@ -1522,7 +1522,10 @@ recent Dired line with superuser, or root, permissions.
+++
*** 'C-x x @' is now bound to 'tramp-revert-buffer-with-sudo'.
You can use 'C-u C-x x @' to select a Tramp method other than the
default, 'sudo'.
default, "sudo".
+++
*** 'tramp-file-name-with-method' can now be set as connection-local variable.
+++
*** Connection method "kubernetes" supports now optional namespace.
@ -1847,7 +1850,7 @@ the directory into which the repository was cloned.
Reverting a directory means reverting changes to all files inside it.
+++
*** New minor mode 'vc-auto-revert-mode'.
*** New global minor mode 'vc-auto-revert-mode'.
This is like 'global-auto-revert-mode' but limited to VCS-tracked files.
As compared with VC's existing, default support for reverting files
after VCS operations, the new mode is a more reliable way to ensure that
@ -2269,7 +2272,7 @@ useful when you want the face attributes to be absolute and not
*** New user option 'ffap-prefer-remote-file'.
If non-nil, FFAP always finds remote files in buffers with remote
'default-directory'. If nil, FFAP finds local files first for absolute
filenames in above buffers. The default is nil.
file names in above buffers. The default is nil.
---
** Flymake

View file

@ -639,6 +639,10 @@ For details, see `tramp-rename-files'."
(const "ksu"))
:link '(tramp-info-link :tag "Tramp manual" tramp-file-name-with-method))
(defun tramp-get-file-name-with-method ()
"Return connection-local value of tramp-file-name-with-method."
(tramp-compat-connection-local-value tramp-file-name-with-method))
(defmacro with-tramp-file-name-with-method (&rest body)
"Ask user for `tramp-file-name-with-method' if needed.
Run BODY."
@ -647,42 +651,43 @@ Run BODY."
(if current-prefix-arg
(completing-read
"Tramp method: "
(mapcar #'cadr (cdr (get 'tramp-file-name-with-method 'custom-type)))
nil t tramp-file-name-with-method)
tramp-file-name-with-method)))
(mapcar
#'cadr (cdr (get 'tramp-file-name-with-method 'custom-type)))
nil t (tramp-get-file-name-with-method))
(tramp-get-file-name-with-method))))
,@body))
(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
(cond
;; Remote file with proper method.
((string-equal method tramp-file-name-with-method)
filename)
;; Remote file on the local host.
((and
(stringp tramp-local-host-regexp) (stringp host)
(string-match-p tramp-local-host-regexp host))
(tramp-make-tramp-file-name
(make-tramp-file-name
:method tramp-file-name-with-method :localname localname)))
;; Remote file with multi-hop capable method.
((tramp-multi-hop-p v)
(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))))
;; Other remote file.
(t (tramp-user-error v "Multi-hop with `%s' not applicable" method))))
;; Local file.
(tramp-make-tramp-file-name
(make-tramp-file-name
:method tramp-file-name-with-method :localname filename))))
(let ((default-method (tramp-get-file-name-with-method)))
(if (tramp-tramp-file-p filename)
(with-parsed-tramp-file-name filename nil
(cond
;; Remote file with proper method.
((string-equal method default-method)
filename)
;; Remote file on the local host.
((and
(stringp tramp-local-host-regexp) (stringp host)
(string-match-p tramp-local-host-regexp host))
(tramp-make-tramp-file-name
(make-tramp-file-name
:method default-method :localname localname)))
;; Remote file with multi-hop capable method.
((tramp-multi-hop-p v)
(tramp-make-tramp-file-name
(make-tramp-file-name
:method (tramp-find-method default-method nil host)
:user (tramp-find-user default-method nil host)
:host (tramp-find-host default-method nil host)
:localname localname :hop (tramp-make-tramp-hop-name v))))
;; Other remote file.
(t (tramp-user-error v "Multi-hop with `%s' not applicable" method))))
;; Local file.
(tramp-make-tramp-file-name
(make-tramp-file-name :method default-method :localname filename)))))
;; FIXME: We would like to rename this for Emacs 31.1 to a name that
;; does not encode the default method. It is intended as a generic
@ -736,7 +741,7 @@ They are completed by `M-x TAB' only in Dired buffers."
"Visit the file or directory named on this line as the superuser.
By default this is done using the \"sudo\" Tramp method.
YOu can customize `tramp-file-name-with-method' to change this.
You can customize `tramp-file-name-with-method' to change this.
Interactively, with a prefix argument, prompt for a different method."
;; (declare (completion tramp-dired-buffer-command-completion-p))