Subject: bug#76417: [PATCH] Add tramp-cleanup-bufferless-connections

From: Ship Mints <[email-masked]>
Date: Thu, 20 Feb 2025 07:50:25 -0500

On Thu, Feb 20, 2025 at 7:07 AM Michael Albinus <[email-masked]>
wrote:

> Ship Mints <[email-masked]> writes:
>
> > 'tramp-cleanup-bufferless-connections' cleans up connection-related
> > objects for which no associated buffers exist.  This is helpful to
> > prune connections after you close remote-file buffers without having
> > to either cherry pick via 'tramp-cleanup-connection' or clear them all
> > via 'tramp-cleanup-all-connections'.
>
> Thanks for this. However, I feel we have already too many
> Tramp-cleanup-* commands; I fear another one would bring more
> confusion. Nobody but the author would remember :-)
>

No worries.  I'll advertise this on support forums when it comes up, no
problem.  Someone had a problem just yesterday for which this could help.
I added this function to my buffer-list-update-hook to keep clean
automatically.

> I looked for an equivalent but didn't see one so I've been using this
> > command, and I think it's ready for contribution.
>
> For this, we have tramp-cleanup-some-buffers. Would it be possible to
> implement your feature by adding a proper hook function in
> tramp-cleanup-some-buffers-hook?
>

I did look. What concerned me was that tramp-cleanup-some-buffers-hook
could be altered by users, where tramp-cleanup-bufferless-connections does
one and only one thing without interference from potentially modified hooks.

As a first change, I propose to extend tramp-list-remote-buffers like
>
> --8<---------------cut here---------------start------------->8---
> (defsubst tramp-get-buffer-file-name (buffer)
>   "Return `buffer-file-name' of BUFFER."
>   (buffer-local-value 'buffer-file-name buffer))
>
> (defun tramp-list-remote-buffers ()
>   "Return a list of all buffers with remote `default-directory'."
>   (tramp-compat-seq-keep
>    (lambda (x)
>      (when (or (tramp-tramp-file-p (tramp-get-default-directory x))
>                (tramp-tramp-file-p (tramp-get-buffer-file-name x)))
>        x))
>    (buffer-list)))
> --8<---------------cut here---------------end--------------->8---
>
> Could you work on such a patch?
>

It looks to me, though, that tramp-cleanup-some-buffers does things
differently.  It does (tramp-cleanup-all-connections) which is not what I
want.  I want only to kill unused tramp connections, not all tramp
connections.  It also kills user buffers which is not what my function
does--it kills only tramp-related objects not user buffers.

I could take a second look today, but it doesn't seem like they solve the
same problem?