Fix nested Emacs frames

Contents

Earlier this year I added Ghostel to my Emacs configuration. Ghostel is a terminal emulator for Emacs that uses Ghostty's engine underneath. It is a delight to use especially with the TUI applications.

Nested Emacs frame problem

I use Emacs in daemon mode, and then use emacsclient to open new frames from the daemon. To use the same Emacs daemon when opening files through other programs such as git, sops, I have a small shell script ee configured as my default EDITOR. It opens a new Emacs frame in the terminal.

#!/bin/sh

exec emacsclient --create-frame --no-window-system "$@"

When I run this script from Ghostel, it opens a nested Emacs frame in Ghostel's terminal session.

Nested Emacs Frame

Solution

Emacs sets an environment variable INSIDE_EMACS for shells and subprocesses. The script use this variable to check if it is launched from Ghostel and use the --reuse-frame flag to open the new buffer in the current frame.

#!/bin/sh

if [ "$INSIDE_EMACS" = "ghostel" ]
then
  exec emacsclient --reuse-frame "$@"
fi

exec emacsclient --create-frame --no-window-system "$@"