Renke Grunwald
Renke Grunwald

Reputation: 885

Create or reuse existing Emacs GUI frame

Assuming an Emacs server is running, I want emacsclient <file> to either create a new frame (like -c) when there is no existing frame or reuse an existing frame when there is one. In other words, I want the -c only when there is no existing frame. Is that possible?

Upvotes: 7

Views: 2488

Answers (5)

David Strickland
David Strickland

Reputation: 1

So there are three states to account for:

  1. Daemon not running: Start emacs and create a new frame.
  2. Daemon running, but no client attached: Create a new frame attached to the daemon.
  3. Client already attached to daemon: Open file in the existing frame.

This section of my ~/.zshrc is working for me:

emacs_start_or_attach () {
    if ! pgrep emacs &> /dev/null 2>&1
    then
        emacsclient -a '' -c $@ &
        return 0
    fi

    if pgrep emacsclient &> /dev/null 2>&1
    then emacsclient $@ &
    else emacsclient -c $@ &
    fi
}

alias emacs=emacs_start_or_attatch
export EDITOR=emacs_start_or_attatch

If you like, you can also start the emacs daemon at system startup and/or have emacs start the daemon on initialization (if it's not running).

Upvotes: 0

Gino
Gino

Reputation: 1790

Here's my solution:

In your emacs startup file (e.g.: ~/.emacs/init.el), ensure that you have the following lines:

;; start emacs server, if it's not already running:
(require 'server)
(unless (server-running-p) (server-start))

Then, add the following to your ~/.bashrc file:

function e()
{
   emacsclient "$@" -a "emacs"
}

To establish a baseline, make sure you exit all emacs instances and kill the emacs daemon if you currently have it running.

Then, from the command line, 'source' the ~/.bashrc file:

source ~/.bashrc

Finally, use 'e' to edit a file:

e Foobar.java &
e notes.txt &

Upvotes: 0

Renke Grunwald
Renke Grunwald

Reputation: 885

I solved my problem with a set of shell scripts.

my_emacs

#!/bin/sh
emacs24-x $@ 1> /dev/null 2> /dev/null &

You may need to change emacs24-x to something that points to your X11 emacs.

my_emacsclient

#!/bin/sh
emacsclient $@ 1> /dev/null 2> /dev/null || my_emacs

Add both files to you PATH via ~/bin or the like.

In my .emacs I also added the followings lines

(load "server")
(unless (server-running-p) (server-start))

Also change some environment variables and optionally add an alias

export ALTERNATE_EDITOR="my_emacs"
export EDITOR="my_emacsclient -n"
export SUDO_EDITOR="my_emacsclient"
...
alias e="$EDITOR"

When you run e in your shell it should create or resuse an existing GUI frame. Also, running e <filename> opens that file in a frame; you can also pass other flags like -n to e.

For the same behaviour in other applications (say your file manager), you should also change the Emacs .desktop file (for me that's /usr/share/applications/emacs24.desktop) to run my_emacs.

It might also be a good idea to change emacs to my_emacsclient via the alternatives system in Debian-based (?) distributions.

Upvotes: 1

Stefan
Stefan

Reputation: 28531

You might want to try `emacsclient --display "$DISPLAY" " to force creation of a GUI frame if there isn't one yet.

Upvotes: 0

Nicolas Dudebout
Nicolas Dudebout

Reputation: 9262

emacsclient <file> does what you want.

If you just want to open a frame without specifying a file, then you need to use -c.

Upvotes: 0

Related Questions