1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Tramp: Don't offer non-existing containers in completion

* doc/misc/tramp.texi (File name completion):
Explain "completion-use-cache" property.

* lisp/net/tramp-cache.el (tramp-get-method-parameter): Declare.
(tramp-get-hash-table): Mark connection properties.
(tramp-dump-connection-properties): Remove empty lists.
(tramp-parse-connection-properties): Take connection property
"completion-use-cache" into account.  (Bug#76950)

* lisp/net/tramp-container.el (tramp-methods) <docker, dockercp, podman>
<podmancp, kubernetes, toolbox, distrobox, flatpak, apptainer, nspawn>:
Add `tramp-completion-use-cache' argument.
This commit is contained in:
Michael Albinus 2025-03-12 20:56:24 +01:00
parent 85c00405b9
commit 13a043fec9
3 changed files with 70 additions and 34 deletions

View file

@ -3798,7 +3798,18 @@ completion lists. If you want to suppress this completion because
there are invalid entries in the persistency file, for example if the
host configuration changes often, or if you plug your laptop to
different networks frequently, you can set the user option
@code{tramp-completion-use-cache} to @code{nil}.
@code{tramp-completion-use-cache} to @code{nil}. This is set per
default for all container-based methods, like @option{docker} or
@option{podman}. If you want to suppress this for other connections,
you can use the connection property @t{"completion-use-cache"},
@xref{Predefined connection information}. Example:
@lisp
@group
(add-to-list 'tramp-connection-properties
(list "^/sshfs:" "completion-use-cache" nil))
@end group
@end lisp
After remote host name completion comes completion of file names on
the remote host. It works the same as with local host file completion

View file

@ -73,6 +73,9 @@
;; `tramp-persistency-file-name', although being connection
;; properties related to a `tramp-file-name' structure.
;;
;; - Properties retrieved from `tramp-connection-properties' are not
;; saved in the file `tramp-persistency-file-name'.
;;
;; - Reusable properties, which should not be saved, are kept in the
;; process key retrieved by `tramp-get-process' (the main connection
;; process). Other processes could reuse these properties, avoiding
@ -86,6 +89,8 @@
(require 'tramp-compat)
(require 'time-stamp)
(declare-function tramp-get-method-parameter "tramp")
;;; -- Cache --
;;;###tramp-autoload
@ -136,17 +141,20 @@ If it doesn't exist yet, it is created and initialized with
matching entries of `tramp-connection-properties'.
If KEY is `tramp-cache-undefined', don't create anything, and return nil."
;; (declare (tramp-suppress-trace t))
(unless (eq key tramp-cache-undefined)
(or (gethash key tramp-cache-data)
(let ((hash
(puthash key (make-hash-table :test #'equal) tramp-cache-data)))
(when (tramp-file-name-p key)
(dolist (elt tramp-connection-properties)
(when (string-match-p
(or (nth 0 elt) "")
(tramp-make-tramp-file-name key 'noloc))
(tramp-set-connection-property key (nth 1 elt) (nth 2 elt)))))
hash))))
(let ((tramp-verbose 0))
(unless (eq key tramp-cache-undefined)
(or (gethash key tramp-cache-data)
(let ((hash
(puthash key (make-hash-table :test #'equal) tramp-cache-data)))
(when (tramp-file-name-p key)
(dolist (elt tramp-connection-properties)
(when (string-match-p
(or (nth 0 elt) "")
(tramp-make-tramp-file-name key 'noloc))
;; Mark it as taken from `tramp-connection-properties'.
(tramp-set-connection-property
key (propertize (nth 1 elt) 'tramp-default t) (nth 2 elt)))))
hash)))))
;; We cannot use the `declare' form for `tramp-suppress-trace' in
;; autoloaded functions, because the tramp-loaddefs.el generation
@ -596,9 +604,14 @@ PROPERTIES is a list of file properties (strings)."
(not (tramp-file-name-localname key))
(not (gethash "login-as" value))
(not (gethash "started" value)))
(dolist (k (hash-table-keys value))
(when (string-prefix-p " " k)
(remhash k value)))
(progn
(dolist (k (hash-table-keys value))
;; Suppress ephemeral properties.
(when (or (string-prefix-p " " k)
(get-text-property 0 'tramp-default k))
(remhash k value)))
(unless (hash-table-keys value)
(remhash key cache)))
(remhash key cache)))
cache)
;; Dump it.
@ -635,15 +648,17 @@ your laptop to different networks frequently."
"Return a list of (user host) tuples allowed to access for METHOD.
This function is added always in `tramp-get-completion-function'
for all methods. Resulting data are derived from connection history."
(and tramp-completion-use-cache
(mapcar
(lambda (key)
(and (tramp-file-name-p key)
(string-equal method (tramp-file-name-method key))
(not (tramp-file-name-localname key))
(list (tramp-file-name-user key)
(tramp-file-name-host key))))
(hash-table-keys tramp-cache-data))))
(mapcar
(lambda (key)
(let ((tramp-verbose 0))
(and (tramp-file-name-p key)
(string-equal method (tramp-file-name-method key))
(not (tramp-file-name-localname key))
(tramp-get-method-parameter
key 'tramp-completion-use-cache tramp-completion-use-cache)
(list (tramp-file-name-user key)
(tramp-file-name-host key)))))
(hash-table-keys tramp-cache-data)))
;; When "emacs -Q" has been called, both variables are nil. We do not
;; load the persistency file then, in order to have a clean test environment.

View file

@ -556,7 +556,8 @@ see its function help for a description of the format."
(tramp-direct-async (,tramp-default-remote-shell "-c"))
(tramp-remote-shell ,tramp-default-remote-shell)
(tramp-remote-shell-login ("-l"))
(tramp-remote-shell-args ("-i" "-c"))))
(tramp-remote-shell-args ("-i" "-c"))
(tramp-completion-use-cache nil)))
(add-to-list 'tramp-methods
`(,tramp-dockercp-method
@ -573,7 +574,8 @@ see its function help for a description of the format."
(tramp-copy-program ,tramp-docker-program)
(tramp-copy-args (("cp")))
(tramp-copy-file-name (("%h" ":") ("%f")))
(tramp-copy-recursive t)))
(tramp-copy-recursive t)
(tramp-completion-use-cache nil)))
(add-to-list 'tramp-methods
`(,tramp-podman-method
@ -586,7 +588,8 @@ see its function help for a description of the format."
(tramp-direct-async (,tramp-default-remote-shell "-c"))
(tramp-remote-shell ,tramp-default-remote-shell)
(tramp-remote-shell-login ("-l"))
(tramp-remote-shell-args ("-i" "-c"))))
(tramp-remote-shell-args ("-i" "-c"))
(tramp-completion-use-cache nil)))
(add-to-list 'tramp-methods
`(,tramp-podmancp-method
@ -603,7 +606,8 @@ see its function help for a description of the format."
(tramp-copy-program ,tramp-podman-program)
(tramp-copy-args (("cp")))
(tramp-copy-file-name (("%h" ":") ("%f")))
(tramp-copy-recursive t)))
(tramp-copy-recursive t)
(tramp-completion-use-cache nil)))
(add-to-list 'tramp-methods
`(,tramp-kubernetes-method
@ -618,7 +622,8 @@ see its function help for a description of the format."
(tramp-direct-async (,tramp-default-remote-shell "-c"))
(tramp-remote-shell ,tramp-default-remote-shell)
(tramp-remote-shell-login ("-l"))
(tramp-remote-shell-args ("-i" "-c"))))
(tramp-remote-shell-args ("-i" "-c"))
(tramp-completion-use-cache nil)))
(add-to-list 'tramp-completion-multi-hop-methods tramp-docker-method)
(add-to-list 'tramp-completion-multi-hop-methods tramp-podman-method)
@ -674,7 +679,8 @@ see its function help for a description of the format."
(tramp-direct-async (,tramp-default-remote-shell "-c"))
(tramp-remote-shell ,tramp-default-remote-shell)
(tramp-remote-shell-login ("-l"))
(tramp-remote-shell-args ("-c"))))
(tramp-remote-shell-args ("-c"))
(tramp-completion-use-cache nil)))
(add-to-list 'tramp-default-host-alist `(,tramp-toolbox-method nil ""))
(add-to-list 'tramp-completion-multi-hop-methods tramp-toolbox-method)
@ -695,7 +701,8 @@ see its function help for a description of the format."
;(tramp-direct-async (,tramp-default-remote-shell "-c"))
(tramp-remote-shell ,tramp-default-remote-shell)
(tramp-remote-shell-login ("-l"))
(tramp-remote-shell-args ("-c"))))
(tramp-remote-shell-args ("-c"))
(tramp-completion-use-cache nil)))
(add-to-list 'tramp-completion-multi-hop-methods tramp-distrobox-method)
@ -719,7 +726,8 @@ see its function help for a description of the format."
(tramp-direct-async (,tramp-default-remote-shell "-c"))
(tramp-remote-shell ,tramp-default-remote-shell)
(tramp-remote-shell-login ("-l"))
(tramp-remote-shell-args ("-c"))))
(tramp-remote-shell-args ("-c"))
(tramp-completion-use-cache nil)))
(add-to-list 'tramp-completion-multi-hop-methods tramp-flatpak-method)
@ -750,7 +758,8 @@ see its function help for a description of the format."
("%h"))) ; Needed for multi-hop check.
(tramp-remote-shell ,tramp-default-remote-shell)
(tramp-remote-shell-login ("-l"))
(tramp-remote-shell-args ("-c"))))
(tramp-remote-shell-args ("-c"))
(tramp-completion-use-cache nil)))
(add-to-list 'tramp-completion-multi-hop-methods tramp-apptainer-method)
@ -771,7 +780,8 @@ see its function help for a description of the format."
("%h")))
(tramp-remote-shell ,tramp-default-remote-shell)
(tramp-remote-shell-login ("-l"))
(tramp-remote-shell-args ("-i" "-c"))))
(tramp-remote-shell-args ("-i" "-c"))
(tramp-completion-use-cache nil)))
(add-to-list 'tramp-default-host-alist `(,tramp-nspawn-method nil ".host"))
(add-to-list 'tramp-completion-multi-hop-methods tramp-nspawn-method)