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?
问题:
回答1:
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.
回答2:
emacsclient <file>
does what you want.
If you just want to open a frame without specifying a file, then you need to use -c
.
回答3:
You might want to try `emacsclient --display "$DISPLAY" " to force creation of a GUI frame if there isn't one yet.
回答4:
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 &