Björn Bidar <bjorn.bi...@thaodan.de> writes:

> Ihor Radchenko <yanta...@posteo.net> writes:
>
>> Björn Bidar <bjorn.bi...@thaodan.de> writes:
>>
>>>> May it be that `org-link-frame-setup' is configured by the user to use
>>>> current frame, but the user manually used gnus-other-frame and closed
>>>> the frame? In my mind, your patch then introduced unexpected behavior.
>>>
>>> The `org-link-frame-setup' introduced the first unexpected behavior I
>>> think since `org-gnus-follow-link' unconditionally uses the previously
>>> existing frame, going with that opening Gnus outside of the
>>> gnus-other-frame-object in the frame setup function would go against
>>> that.
>>> ...
>>
>> Ok. Looks like you know what you are doing :)
>> I am a bit lost in this global state managed by gnus.
>
> I get it, I had my fare share of issues with it..
> Gnus suffered to my neglect over the years so much todo :)
>
>> In any case, given your arguments, I think that your patch is ok.
>> However, please add ORG-NEWS entry announcing new custom option.
>
> OK will do.


Here the updated patch as requested.

Please check if I put the announcement into the right sections.

>From 4114f6f02c0d2c25d95ab0ec5cbe2d23bfc9a77a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Bidar?= <bjorn.bi...@thaodan.de>
Date: Tue, 21 Jan 2025 00:55:15 +0200
Subject: [PATCH] Ensure that gnus-other-frame-object is alive before selecting

* lisp/ol-gnus.el (org-gnus-follow-link): Ensure that
`gnus-other-frame-object' is active before selecting it.
Error out in case frame-object is dead, i.e. wasn't activated
by `org-link-frame-setup-function'.
(org-gnus-no-new-news): Take `gnus-other-frame-object' into account.
Call gnus-other-frame with chosen action instead without.
Ensures that previous behavior of `org-gnus-follow-link' is kept.
(org-gnus-no-new-news-other-frame): New function for users who choose
to call `gnus-other-frame' regardless if previous used or not.
* lisp/ol.el (org-link-frame-setup): Include
new `org-gnus-no-news-other-frame` function.
---
 etc/ORG-NEWS    | 19 +++++++++++++++++++
 lisp/ol-gnus.el | 30 ++++++++++++++++++++++++------
 lisp/ol.el      |  4 +++-
 3 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index cb2c16da8..f435dd888 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -211,6 +211,25 @@ retail 4-4-5 calendars, etc).
 # adding new customizations, or changing the interpretation of the
 # existing customizations.
 
+*** =org-link-gnus= Ensure that other frame is alive before selecting it
+Ensure that the Gnus frame is alive before selecting it if it was
+activated before.  Call ~gnus-other-frame~ in case it
+was used to call Gnus before, i.e. if ~gnus-other-frame-object~ is
+not ~nil~.  The side effect of that is that the original Gnus frame
+is reused when the frame is setup instead of being replaced
+when Gnus was activated using ~gnus~ instead ~gnus-other-frame~
+as it should when there is a Gnus frame.
+
+Existing ~org-link-frame-setup~ functions for Gnus links have
+to be adjusted to ensure that ~gnus-other-frame-object~ contains
+a frame which is alive.  Check the default ~org-gnus-no-news~
+function for more.
+
+*** ~org-link-frame-setup~ include new option to open Gnus in other frame
+Include ~org-gnus-no-new-news-other-frame~ as an option to always
+call Gnus using ~gnus-other-frame~ regardless if it had been previously
+used or not.  The function can be useful in a frame orientated setup.
+
 *** ox-odt: New export option ~org-odt-with-forbidden-chars~
 
 The new export option controls how to deal with characters that are forbidden
diff --git a/lisp/ol-gnus.el b/lisp/ol-gnus.el
index 5cddc3790..65d77ea88 100644
--- a/lisp/ol-gnus.el
+++ b/lisp/ol-gnus.el
@@ -228,7 +228,12 @@ (defun org-gnus-follow-link (&optional group article)
   "Follow a Gnus link to GROUP and ARTICLE."
   (require 'gnus)
   (funcall (org-link-frame-setup-function 'gnus))
-  (when gnus-other-frame-object (select-frame gnus-other-frame-object))
+  (when gnus-other-frame-object
+    (if (not (frame-live-p gnus-other-frame-object))
+        ;; Error out in case org-link-frame-setup did not take care of setting up
+        ;; the gnus frame if was activate previously.
+        (error "Couldn't select \'gnus-other-frame-object\', make sure it is active"))
+    (select-frame gnus-other-frame-object))
   (let ((group (org-no-properties group))
 	(article (org-no-properties article)))
     (cond
@@ -260,11 +265,24 @@ (defun org-gnus-follow-link (&optional group article)
 	 (message "Couldn't follow Gnus link.  The linked group is empty."))))
      (group (gnus-group-jump-to-group group)))))
 
-(defun org-gnus-no-new-news ()
-  "Like `\\[gnus]' but doesn't check for new news."
-  (cond ((gnus-alive-p) nil)
-	(org-gnus-no-server (gnus-no-server))
-	(t (gnus))))
+(defun org-gnus-no-new-news (&optional other-frame)
+  "Like `\\[gnus]' but doesn't check for new news.
+In case of OTHER-FRAME or `gnus-other-frame-object' call `gnus-other-frame'.
+
+Ensures that `gnus-other-frame' is activated correctly if dead."
+  (let ((action (cond  (org-gnus-no-server #'gnus-no-server)
+	               (t #'gnus))))
+    (cond ((or other-frame gnus-other-frame-object)
+           (let ((gnus-other-frame-function action)
+                 (gnus-other-frame-resume-function action))
+             (gnus-other-frame)))
+          (t (if (not (gnus-alive-p))
+                 (funcall action))))))
+
+(defun org-gnus-no-new-news-other-frame ()
+  "Like `org-gnus-no-new-news' but always in another frame."
+    (org-gnus-no-new-news t))
+
 
 (provide 'ol-gnus)
 
diff --git a/lisp/ol.el b/lisp/ol.el
index 0429e257c..856a409d7 100644
--- a/lisp/ol.el
+++ b/lisp/ol.el
@@ -348,6 +348,7 @@ (defcustom org-link-frame-setup
     `gnus'
     `gnus-other-frame'
     `org-gnus-no-new-news'
+    `org-gnus-no-new-news-other-frame'
 For FILE, use any of
     `find-file'
     `find-file-other-window'
@@ -374,7 +375,8 @@ (defcustom org-link-frame-setup
 		(choice
 		 (const gnus)
 		 (const gnus-other-frame)
-		 (const org-gnus-no-new-news)))
+		 (const org-gnus-no-new-news)
+                 (const org-gnus-no-new-news-other-frame)))
 	  (cons (const file)
 		(choice
 		 (const find-file)
-- 
2.45.2

Reply via email to