Morgan Smith <[email protected]> writes:

> `next-error' calls `next-error-find-buffer' to determine where to find the 
> next
> error.  In that function it actually prioritizes the last buffer it was used 
> in
> over the current buffer!  This explains a lot of confusing behaviour I've
> experienced when switching back and forth between grep and compilation 
> buffers!
>
> Looking through the source code it seems the "solution" is to manually set
> `next-error-last-buffer' as grep, compile, occur, and xref do.  While I'm not 
> a
> fan of this, I suppose we should do what everyone else does.
>
> I made some patches to fix the problem.  See attached

Thanks for the patch!

> While I was looking at it I also removed an incorrect sentence from the
> docstring of `org-highlight-sparse-tree-matches'.  The highlights 
> automatically
> disapper on edit for org-occur matches but not for sparse trees

Maybe we should instead add this feature to sparse tree matches?

> +      (buffer-substring-no-properties (point) (pos-eol))))

Do note that `pos-eol' is not available in Emacs 28, which we still
support (see https://orgmode.org/worg/org-maintenance.html#emacs-compatibility)
You need to use the functions available in all the supported Emacs
versions. If you absolutely need to use a function from newer Emacs, you
need to implement backwards compatibility version in org-compat.el. If
you do, the convention is to name compatibility version of function
`foo' as `org-foo'. There are many examples in org-compat.

> Subject: [PATCH 2/2] Fix `next-error' behavior with sparse trees
>
> * lisp/org.el (org-highlight-sparse-tree-matches): Adjust docstring.

Ideally, we should limit commits to a single change. Changing the
docstring appears to be irrelevant to fixing next-error behavior. It
would better be placed in a separate commit.

> (org-scan-tags): Set `next-error-last-buffer' when adding
> highlighting.
> * testing/lisp/test-org.el (test-org/sparse-tree-next-error): Show
> fixed behavior.  Remove todo.

Please also add Reported-by: and link back to this email thread. See
https://orgmode.org/worg/org-contribute.html#org252f961

> @@ -11570,7 +11569,8 @@ org-scan-tags
>              (and org-highlight-sparse-tree-matches
>                   (org-get-heading) (match-end 0)
>                   (org-highlight-new-match
> -                  (match-beginning 1) (match-end 1)))
> +                  (match-beginning 1) (match-end 1))
> +                    (setq next-error-last-buffer (current-buffer)))

This way, your change will only take effect when
`org-highlight-sparse-tree-matches' is non-nil. When it is nil, the bug
will still remain.

Also, what about `org-occur' itself? Won't it suffer from the same bug?

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to