Subject: Re: bug#65509: [PATCH] Use correct env var when opening frames with freedesktop menu entries

From: Davide Masserut <[email-masked]>
Date: Wed, 06 Sep 2023 16:01:58 +0200

Po Lu <[email-masked]> writes:

> Davide Masserut <[email-masked]> writes:
>
>> Correct.  However, that TTY is occupied by the X window manager,
>> which hides the Emacs interface.
>>
>> When the argument of --display doesn't exits or is empty, should we
>> check if $DISPLAY is present in the environment and use its value?
>>
>> When PGTK is enabled, we could check $WAYLAND_DISPLAY and then
>> $DISPLAY before resorting to displaying the frame in a terminal or in
>> a TTY.
>>
>> I tested this with Gnome and i3, both started from a TTY using
>> startx. I'm not sure how this behaves when using a display manager.
>
> Display selection is GTK's territory, and we basically give it carte
> blanche to judge which displays are suitable, since we can't account
> for every present and future GDK backend.
>
> Only subsequent to the establishment of the display connection are we
> capable of taking display-specific action, such as displaying a
> warning message.

It turns out that Emacs 29 has new CLI option that solves exactly these problems.

The attached patch replaces the usage of --display=$DISPLAY with --reuse-frame.

>From bb7ba852c9add6ed5b3c1edd783aaf07eca08a46 Mon Sep 17 00:00:00 2001
From: Davide Masserut <[email-masked]>
Date: Wed, 6 Sep 2023 15:35:47 +0200
Subject: [PATCH] Avoid using --display to reuse frames

Using hard-coded display values can cause graphical frames to open
using the wrong backend or not open at all.

* etc/emacsclient-mail.desktop, etc/emacsclient.desktop: Use
--reuse-frame instead of --display.
---
 etc/emacsclient-mail.desktop | 4 ++--
 etc/emacsclient.desktop      | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/etc/emacsclient-mail.desktop b/etc/emacsclient-mail.desktop
index 0a2420ddead..819e3855567 100644
--- a/etc/emacsclient-mail.desktop
+++ b/etc/emacsclient-mail.desktop
@@ -2,9 +2,9 @@
 Categories=Network;Email;
 Comment=GNU Emacs is an extensible, customizable text editor - and more
 # We want to pass the following commands to the shell wrapper:
-# u=$(echo "$1" | sed 's/[\"]/\\&/g'); exec emacsclient --alternate-editor= --display="$DISPLAY" --eval "(message-mailto \"$u\")"
+# u=$(echo "$1" | sed 's/[\"]/\\&/g'); exec emacsclient --alternate-editor= --reuse-frame --eval "(message-mailto \"$u\")"
 # Special chars '"', '$', and '\' must be escaped as '\\"', '\\$', and '\\\\'.
-Exec=sh -c "u=\\$(echo \\"\\$1\\" | sed 's/[\\\\\\"]/\\\\\\\\&/g'); exec emacsclient --alternate-editor= --display=\\"\\$DISPLAY\\" --eval \\"(message-mailto \\\\\\"\\$u\\\\\\")\\"" sh %u
+Exec=sh -c "u=\\$(echo \\"\\$1\\" | sed 's/[\\\\\\"]/\\\\\\\\&/g'); exec emacsclient --alternate-editor= --reuse-frame --eval \\"(message-mailto \\\\\\"\\$u\\\\\\")\\"" sh %u
 Icon=emacs
 Name=Emacs (Mail, Client)
 MimeType=x-scheme-handler/mailto;
diff --git a/etc/emacsclient.desktop b/etc/emacsclient.desktop
index 4395d3b02bc..cfbd4d97979 100644
--- a/etc/emacsclient.desktop
+++ b/etc/emacsclient.desktop
@@ -3,7 +3,7 @@ Name=Emacs (Client)
 GenericName=Text Editor
 Comment=Edit text
 MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;x-scheme-handler/org-protocol;
-Exec=sh -c "if [ -n \\"\\$*\\" ]; then exec emacsclient --alternate-editor= --display=\\"\\$DISPLAY\\" \\"\\$@\\"; else exec emacsclient --alternate-editor= --create-frame; fi" sh %F
+Exec=sh -c "if [ -n \\"\\$*\\" ]; then exec emacsclient --alternate-editor= --reuse-frame \\"\\$@\\"; else exec emacsclient --alternate-editor= --create-frame; fi" sh %F
 Icon=emacs
 Type=Application
 Terminal=false
-- 
2.42.0