flathub / org.gnu.emacs

https://flathub.org/apps/details/org.gnu.emacs
16 stars 17 forks source link

Not obvious how to use `emacsclient` from host #48

Open posita opened 2 years ago

posita commented 2 years ago

In other environments, I would normally fire up Emacs and then interact with it calling emacsclient directly from the command line (e.g., emacsclient /some/path/some/file). However it is not obvious how to do this when Emacs is running via flatpak. It would be nice if this was documented.

After reading https://www.reddit.com/r/flatpak/comments/w4aoih/launch_application_emacsclient_from_within_a/, I came up with something like flatpak enter org.gnu.emacs flatpak-spawn --host emacsclient …, but that seems somewhat absurd and results in the following error:

% flatpak enter org.gnu.emacs flatpak-spawn --host emacsclient /some/path/some/file
emacsclient: can't find socket; have you started the server?
emacsclient: To start the server in Emacs, type "M-x server-start".
emacsclient: No socket or alternate editor.  Please use:

        --socket-name
        --server-file      (or environment variable EMACS_SERVER_FILE)
        --alternate-editor (or environment variable ALTERNATE_EDITOR)
posita commented 2 years ago

Somewhat answering my own question: flatpak enter org.gnu.emacs flatpak-spawn emacsclient … kind of works (at least intermittently), but this (or whatever is the preferred method) should probably be documented.

One oddity is that relative paths will be interpreted relative to the container's default directory which can be really confusing. This (from the host) will not work as expected:

% cd ~/some/directory
% flatpak enter org.gnu.emacs flatpak-spawn emacsclient ./some/file  # will try to open ~/some/file, not ~/some/directory/some/file
posita commented 2 years ago

Okay, I think I found a solution to the directory issue, but this is not intuitive:

% flatpak enter org.gnu.emacs flatpak-spawn --directory="${PWD}" emacsclient -- …

This assumes that the host directory is mounted at the same location in the container. Note the -- is necessary to ensure that emacsclient receives all arguments thereafter. This is also not documented (at least not in the man pages) and was a lucky guess on my part.

% flatpak enter org.gnu.emacs flatpak-spawn --directory="${PWD}" emacsclient --help
error: No such pid org.gnu.emacs
% flatpak enter org.gnu.emacs flatpak-spawn --directory="${PWD}" emacsclient -- .
Waiting for Emacs...
posita commented 2 years ago

Nope. The -- is a red herring. This is really weird:

% emacsclient --help
Usage: emacsclient [OPTIONS] FILE...
…
% emacsclient --help
error: No such pid org.gnu.emacs
% emacsclient --help
error: No such pid org.gnu.emacs
% emacsclient --help
error: No such pid org.gnu.emacs
% emacsclient .     
Waiting for Emacs...

These look like intermittent failures? :confused: