]> git.eshelyaron.com Git - emacs.git/commitdiff
tramp-file-name-with-method can be set connection-local
authorMichael Albinus <michael.albinus@gmx.de>
Sun, 13 Jul 2025 13:48:54 +0000 (15:48 +0200)
committerEshel Yaron <me@eshelyaron.com>
Thu, 24 Jul 2025 08:24:57 +0000 (10:24 +0200)
* 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.

(cherry picked from commit f1aa9482d1920a7fb9f01006b16bc25449a1c622)

doc/misc/tramp.texi
lisp/net/tramp-cmds.el

index 9c31cce0e56f16cff04d6f9da20a94b04853a45c..0d3d2d4e580f8a1a5c8413d964dca58a8e35a0a6 100644 (file)
@@ -3939,7 +3939,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.
index 88345fa7e16f9df63a3693b78394c44e241ff548..00901d37972eef935d0ce0a51dfcfa8c670bf709 100644 (file)
@@ -631,6 +631,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."
@@ -639,42 +643,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
@@ -723,8 +728,12 @@ They are completed by `M-x TAB' only in Dired buffers."
 
 ;;;###autoload
 (defun tramp-dired-find-file-with-sudo ()
-  "In Dired, visit the file or directory named on this line.
-This is performed with \"sudo\" permissions."
+  "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.
+
+Interactively, with a prefix argument, prompt for a different method."
   ;; (declare (completion tramp-dired-buffer-command-completion-p))
   (interactive)
   (with-tramp-file-name-with-method