Subject: 28.1.91; emacsclient-mail.desktop doesn't work for me

From: Damien Cassou <[email-masked]>
Date: Mon, 12 Sep 2022 20:31:27 +0200

Hi,

The file emacsclient-mail.desktop that is provided by Emacs (see below
for an excerpt) doesn't seem to work for me. I would like mailto: links
in the web browser to open with emacsclient but nothing happens. How to
reproduce:

1. start the Emacs daemon (if not already done)

2. go to
https://lists.gnu.org/archive/html/emacs-devel/2022-09/msg00320.html in
a non-Emacs web browser

3. click the "Eli Zaretskii" button after "reply via email to"

Expected: An Emacs client frame appears with a buffer in message mode
Actual: Nothing happens

If you try to reproduce and you get a different application opening to
compose your email, you might want to add the following to
~/.config/mimeapps.list:

  [Default Applications]
  x-scheme-handler/mailto=emacsclient-mail.desktop

It feels like launching my web browser from the terminal sometimes makes
it work, but that's not really reliable.

The freedesktop Desktop Entry Specification [1] contains:

  Field codes must not be used inside a quoted argument, the result of
  field code expansion inside a quoted argument is undefined.

It seems to me that the .desktop file Emacs provides does just that: use
a field code (%u) inside a quoted argument. I might be wrong in the
interpretation of the spec though as the next sentence in the spec seems
to contradict this interpretation.

Anyway, I found a way to always have it working:

1. create a file emacs-compose-email.sh that starts emacsclient
2. add the executable bit to the file
3. reference the shell script from emacsclient-mail.desktop

See below for the script and .desktop file. Another advantage of this
approach is that the desktop file becomes much simpler with much less
backslashes.

My question is: do you want a patch with this change?

emacs-compose-email.sh:
  #!/usr/bin/env bash
  emacsclient --alternate-editor= --eval "(message-mailto \"$1\")"

Working emacsclient-mail.desktop:
  [Desktop Entry]
  Exec=emacs-compose-email.sh %u
  MimeType=x-scheme-handler/mailto
  Name=Emacs (Mail, Client)
  NoDisplay=true
  Terminal=false
  Type=Application
  Version=1.4

Excerpt of the existing (non-working) emacsclient-mail.desktop:
  [Desktop Entry]
  Exec=sh -c "exec emacsclient --alternate-editor= --display=\\"\\$DISPLAY\\" --eval \\\\(message-mailto\\\\ \\\\\\"%u\\\\\\"\\\\)"
  Name=Emacs (Mail, Client)
  MimeType=x-scheme-handler/mailto;
  Actions=new-window;new-instance;
  
  [Desktop Action new-window]
  Name=New Window
  Exec=emacsclient --alternate-editor= --create-frame --eval "(message-mailto \\"%u\\")"
  
  [Desktop Action new-instance]
  Name=New Instance
  Exec=emacs -f message-mailto %u

[1]: https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables

-- 
Damien Cassou

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill