Subject: bug#58839: [Patch] Re: bug#58839: 29.0.50; project-kill-buffer fails when Eglot is running

From: Dmitry Gutov <[email-masked]>
Date: Sat, 29 Oct 2022 14:27:31 +0300

On 29.10.2022 04:39, João Távora wrote:

> If you missed it, Eglot has been merged in lisp/progmodes/eglot.el.
> There's not much point in testing that your patch: I can tell you right
> away it doesn't work.

I was thinking that somebody motivated could easily update it to be 
functional.

>> I suppose. But the current criterion depends on the value of
>> default-directory, and that makes it a match.
> 
> This criterion is wrong.  It makes mistakes.  But a criterion that is
> "default-directory and is not hidden", though probably still not ideal,
> but is definitely better.

This whole discussion is about different shades of OCD. One party wants 
to clean up as much as possible, another says don't touch my things.

I don't think there is an objective "right" way to do things, only 
something we're able to agree on in the end. I don't really use this 
feature much myself: if you're able to come to an agreement with Philip 
(who took the initiative on adding that command), I'll be happy.

>>> If you agree that
>>> there are such cases, then it should become clear that the buffer in
>>> question must be at the top of that list.
>>
>> I'm not sure. Intuitively, I'd say that this buffer belongs to the
>> project because it "services" the project. But if it were to work for
>> several projects at the same time, I suppose I could say it doesn't
>> belong to any particular one.
> 
> It indeed indirectly services just that one project: but it's also just
> another object.  Eglot has lots of objects, variables etc., that
> "service the project" and project.el fortunately isn't crazy to to touch
> them.  The buffer in question is an implementation detail of jsonrpc.el.
> It's not a buffer of interest in any way for the user or project.el's
> manipulations.  And it's only a buffer because buffer's are Emacs'
> common way of communicating with external entities, and jsonrpc.el uses
> that technique.  But it could use some other way, say another
> process-filter or function calls into C code of a dynamic library.
> There would also be objects that indirectly "service" the project, but
> not buffers.

Most object types are garbage-collected when no live reference to them 
remains. That's not the case for buffers.

Is the buffer in question killed when the user calls 'M-x 
eglot-shutdown'? If so, consider that, after the user calls 
project-kill-buffers, there won't be any buffers remaining that belong 
to that project, that the user would be able to call 'M-x 
eglot-shutdown' from.

>>> There are more hints that the concept of "buffer belonging to a project"
>>> was not fully thought through, even in cases unrelated to this bug
>>> report.
>>> * Take the *scratch* buffer.  It has a default-directory.  Does this
>>>     also make *scratch* belong to a project?  It doesn't make any sense to
>>>     me that it would.  Yet it is caught by project-buffers.
>>
>> *scratch* is not that special - you can create similar buffers at
>>   will. So there are two ways of looking at that question. One can
>>   create a "scratch" for a project, and it will be part of that
>> project.
>>
>> If "~" (the usual value of default-directory in the original
>> *scratch*) belongs to a project, then *scratch* also does.
> 
> I M-x cd in *scratch* all the time.  It's a global scratch pad,
> now accessible via scratch-buffer everywhere.

And I don't have any projects that "~" belongs to.

>>> * project-buffers also catches the one-time *Completions* buffers, the
>>>     kind produced by hitting TAB after C-x p b.  If you type C-x p b
>>>     again, it quite comically offers the stale *Completions* buffer as a
>>>     candidate to switch to.
>>
>> We could make an exception for that too.
>>
>>> But back to *scratch*.  Somehow *scratch* is not killed by M-x
>>> project-kill-buffers.  I think it's because it doesn't have a
>>> buffer-file-name.  But then neither does the Eglot/Jsonrpc's "background
>>> buffers"!  It seems it is being targeted merely because it uses
>>> fundamental-mode, a most reasonable mode to use for exchanging messages
>>> via standard streams.
>>> I guess this means that the hack below is enough to fix the issue,
>>> but
>>> it is also decidedly silly.
>>
>> It's not much better than adding a function to
>> kill-buffer-query-functions that returns nil. And/or behaves
>> accordingly to eglot-autoshutdown.
> 
> You should think your solution through before comparing with the ones
> provided so far, which have been tested.  Where in the source code would
> you even set kill-buffer-query-functions?  Eglot code in jsonrpc.el??

Same place you changed the major mode in the last patch: jsonrpc.el. If 
jsonrpc.el doesn't want its buffers to be killed, it could set that up 
as described above, through kill-buffer-query-functions.

> Not to mention duplicating the eglot-autoshutdown check in unrelated
> places is pretty ugly.
> 
>>> So please consider fixing this in project.el.  As Manuel pointed out,
>>> the venerable ibuffer.el's ibuffer-kill-filter-group also kills project
>>> buffers and handles this whole thing very well.  We should just take a
>>> hint from it.
>>
>> I'm unable to find that message.
> 
> In the original conversation:
> 
> https://github.com/joaotavora/eglot/discussions/822#discussioncomment-2053395

It's a reasonable approach too. Just not the one we took here. It would 
make sense to try to make it work first.

And if we're comparing different commands similar to this one, how does 
`projectile-kill-buffers' behave?