Subject: Re: bug#20489: 25.0.50; next-error-find-buffer chooses non-current buffer without good reason

From: Dmitry Gutov <[email-masked]>
Date: Wed, 28 Feb 2018 04:13:54 +0200

On 2/27/18 11:16 PM, Juri Linkov wrote:
>> On that subject: why did we make next-error-last-buffer always buffer-local?
> 
> next-error-last-buffer is buffer-local to keep the reference to the
> parent buffer used to navigate to the current buffer, so the next call of
> next-buffer will use the same parent buffer to continue navigation from it.

Thank you for the reminder. Looking at the previous discussion, I wasn't 
fond of this design back then, and still think it's largely unnecessary 
complexity (it introduces implicit state, and in the situations that it 
can be useful in, the user will have to remember which windows came from 
which navigation).

Anyway, I've fixed the current problem (see below), so this is a matter 
of opinion. If you still consider this feature to be important, I think 
ideally we'd abstract it away behind a new -function variable as well. 
This way, someone would also be able to implement window-local 
navigation relationship instead of buffer-local (you've mentioned this 
option before).

> This works well in most cases except the case of xref buffers.
> Below is the explanation for code from next-error with added remarks:
> 
> (defun next-error (&optional arg reset)
>    (let ((buffer (next-error-find-buffer)))
>      (when buffer
>        (with-current-buffer buffer
> 
> Here the current buffer is *xref*
> 
>          (funcall next-error-function (prefix-numeric-value arg) reset)
> 
> next-error-function should navigate from *xref* to another buffer
> and change the current-buffer to the navigated buffer, e.g. ChangeLog.1.

If that's something next-error-function must do, let's document it 
better. Right now it only says "Function to use to find the next error 
in the current buffer" (how does one "find" an error?) and describes 
input arguments, but not the return value (which is luckily unused) or 
which buffer must be current, or which window selected at the end.

> This works fine in most cases, for example when next-error-function is
> compilation-next-error-function, but fails when next-error-function is
> xref--next-error-function that switches to ChangeLog.1, but doesn't set
> the value current-buffer to ChangeLog.1.
> 
>          (setq next-error-last-buffer buffer)
> 
> In normal cases this sets buffer-local next-error-last-buffer in the
> navigated buffer, e.g. ChangeLog.1 that should be the current buffer.
> But since xref--next-error-function doesn't set the right current buffer
> to ChangeLog.1, this sets buffer-local next-error-last-buffer in the wrong
> buffer, i.e. in *xref*.  IOW, the value returned from (current-buffer)
> is wrong here after xref--next-error-function call.

I see.

> So the question is: in xref--next-error-function can we use code similar
> to what is used in compilation-next-error-function that works without problems?

You probably mean compilation-goto-locus (it contains the navigation 
part), but even so, it's not easy to tell which part you mean (the 
function does more than one would expect).

In short, adapting that code is kind of difficult, but hopefully I found 
and fixed the problem in xref directly in 
11c58c4fc495ea4f7bff52ca077fd3e4382aa900.