From c92917f09eb1b1ef043dd92c914bd88fa72e79d9 Mon Sep 17 00:00:00 2001
From: Rohit Patnaik <quanticle@quanticle.net>
Date: Fri, 9 Sep 2022 14:55:04 -0500
Subject: [PATCH] org-agenda.el: Make `org-agenda-switch-to' recenter on the
 match

* org-agenda.el (org-agenda-switch-to): Port the recentering behavior from
`org-agenda-goto' to `org-agenda-switch-to'.

This change centers the screen on the match that `org-agenda-switch-to' jumps to
when the user hits RET in the agenda buffer.  This prevents the match from
appearing at the very top or very bottom of the screen, allowing the user to see
more context around the match.

TINYCHANGE
---
 etc/ORG-NEWS       | 9 +++++++++
 lisp/org-agenda.el | 1 +
 2 files changed, 10 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index a7f32671e..82eca2d62 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -489,6 +489,15 @@ support in LaTeX.  The new =babel= syntax for loading languages via
 =ini= files and the new command =\babelprovide= (see:
 https://mirrors.ctan.org/macros/latex/required/babel/base/babel.pdf)
 are also supported.
+
+*** ~org-agenda-switch-to~ recenters the window on the agenda match
+
+~org-agenda-switch-to~ now recenters the window on the agenda match,
+similar to ~org-agenda-goto~. This ensures that the user can see more
+of the context for the agenda match, whereas previously the match may
+have appeared at the top of the window, requiring the user to scroll
+up to see the headline or other context around the match.
+
 * Version 9.5
 
 ** Important announcements and breaking changes
diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index 053232078..e9ab54527 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -9741,6 +9741,7 @@ displayed Org file fills the frame."
       (goto-char pos)
       (when (derived-mode-p 'org-mode)
 	(org-fold-show-context 'agenda)
+        (recenter (/ (window-height) 2))
 	(run-hooks 'org-agenda-after-show-hook)))))
 
 (defun org-agenda-goto-mouse (ev)
-- 
2.37.3.windows.1

