From: Michael Albinus Date: Sun, 13 Jul 2025 13:48:54 +0000 (+0200) Subject: tramp-file-name-with-method can be set connection-local X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=20a03e6623f0bfdca4089173bd56dc1ab2202808;p=emacs.git 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. (cherry picked from commit f1aa9482d1920a7fb9f01006b16bc25449a1c622) --- diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi index 9c31cce0e56..0d3d2d4e580 100644 --- a/doc/misc/tramp.texi +++ b/doc/misc/tramp.texi @@ -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. diff --git a/lisp/net/tramp-cmds.el b/lisp/net/tramp-cmds.el index 88345fa7e16..00901d37972 100644 --- a/lisp/net/tramp-cmds.el +++ b/lisp/net/tramp-cmds.el @@ -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