Subject: bug#37856: 27.0.50; 'next-error' advised with 'recenter' signals error when run from "*grep*" buffer

From: Stefan Kangas <[email-masked]>
Date: Mon, 21 Oct 2019 22:11:50 +0200

Eli Zaretskii <[email-masked]> writes:

> > 1. Under "emacs -Q", evaluate:
> >     (defun my-advice (&rest args)
> >       (recenter))
> >     (advice-add 'next-error :after #'my-advice)
> > 2. M-x rgrep RET [... grep for something]
> > 3. Run 'next-error' (M-g M-n) from the "*grep*" buffer.
> >
> > Result: I get the error:
> >     recenter-top-bottom: ‘recenter’ing a window that does not display
> > current-buffer.
>
> Why isn't this a cockpit error: you force a function that happens to
> switch buffers to recenter.  It's like you've added a call to
> 'recenter' in some arbitrary place in a random function: you are
> responsible for that code, and if it does stuff that it shouldn't,
> your code is wrong.  No?

In this case my expectation was that this code would be run *after*
the command, i.e. when it had already done all the switching.  I'm not
sure that's comparable to placing recenter in an arbitrary place
inside the function.

It was surprising to me that this didn't work, and it's not clear what
I should do instead.  For example, the following code exhibits the
same problem:

(defun my-next-error ()
  (interactive)
  (next-error)
  (recenter))

What am I missing?

Best regards,
Stefan Kangas