To reproduce:

1. Put some %%(org-diary) lines in your diary file, e.g.,

%%(org-diary) ~/my-org-file.org

2. Open an Org agenda, then kill it with C-x k.

3. Run M-x diary.

This gives:

Bad diary sexp at [...]
(org-diary)
Error: (error "Selecting deleted buffer")

The issue is that killing the agenda buffer leaves org-agenda-buffer
with a non-nil reference to a dead buffer.  The patch checks whether the
agenda buffer is still live, and otherwise uses the current buffer.

>From fe4d81bab7fee06c933419d09d7beb938460d337 Mon Sep 17 00:00:00 2001
From: Paul Nelson <[email protected]>
Date: Mon, 5 Oct 2026 15:36:36 +0200
Subject: [PATCH] Fix diary entries after killing the agenda buffer

* lisp/org-agenda.el (org-compile-prefix-format): Use the
current buffer when org-agenda-buffer is no longer live.
---
 lisp/org-agenda.el | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index 02c963d12..2196aacef 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -7270,7 +7270,9 @@ and stored in the variable `org-prefix-format-compiled'."
         (setq s (replace-match "%s" t nil s)))
       (push varform vars))
     (setq vars (nreverse vars))
-    (with-current-buffer (or org-agenda-buffer (current-buffer))
+    (with-current-buffer (if (buffer-live-p org-agenda-buffer)
+                             org-agenda-buffer
+                           (current-buffer))
       (setq org-prefix-format-compiled
 	    (list
 	     `((org-prefix-has-time ,org-prefix-has-time)
-- 
2.54.0

Reply via email to