The functionality is built into Emacs. Run emacsclient and pass it the -a (long form --alternate-editor) option with an empty argument, and it'll start Emacs (in daemon mode, i.e. initially without any window) if it isn't already running.
emacsclient -a '' # in sh syntax
emacsclient --alternate-editor= # anywhere whitespace-separated ...
Closing The Client
Keep in mind that when you are running an emacs daemon, the frame in which you are actually editing is simply a client of the server. The file is not buffered in the client, it is buffered in the server. When you close the client you are not killing the server process, nor is the file closed. The file you were editing remains open in ...
The default behavior when invoking emacsclient is a little conservative.
Check out this comment from
/* Unless we are certain we don't want to occupy the tty, send our
tty information to Emacs. For example, in daemon mode Emacs may
need to occupy this tty if no other frame is available. */
From your description and ...
You cannot useemacsclient this way. The Emacs Server does not create frames on the client side; it always creates them on the server-side X11 display or TTY, but the remote server doesn't have access to your local client side TTYs or X11 displays.
The TCP connection of Emacs server is not intended for remote access, it's just a work around for the lack of ...
There is Tramp. You can edit a file on another machine using the syntax /ssh:user@machine:/path/to/file. If you take sudo instead of ssh, and empty user and machine, you can edit a local file as root. Like /sudo::/path/to/file.
This answer has the general method, though the question was different than yours.
You can use emacs --daemon=workspaceN combined with emacsclient -s workspaceN to have an emacs daemon on workspace N. If you need multiple, simply do workspace1, workspace2, etc. Note that the daemon name is entirely arbitrary and you can use whatever naming scheme you like.
It appears daemonp will return the name of the server. I ended up with code that looks something like
((string= "org" (daemonp))
You might check:
(and (boundp 'server-process)
That should work provided that you've avoided starting a server in the other instances.
Assuming a socket-based server, if you were to start a second Emacs instance, and forcibly delete and then restart the server process from that second instance, then ...
That FRAME argument to after-make-frame-functions that you're explicitly ignoring? Don't ignore it.
(defun my-start-emacs (frame)
"Switch client frames of an emacs daemon to the 'server' workgroup."
(if (not (boundp 'server-wg))
Since you said you are already using use-package, you can turn on its own verbose option that reports how long each package takes to load.
Use customize-option use-package-verbose or put this in your init file:
(setq use-package-verbose t)
Then restart Emacs and check your *Messages* buffer. The output will look something like:
Loading package dash......
I am unsure of how to do that strictly within Emacs, but luckily there are other ways to get what you describe.
If you don't have something in your .emacs to start a server, you could always make a small script that starts up Emacs with the file you want to edit and start the server forked.
emacs --eval '(server-start)' $* &...
After a lot of experimenting with writing wrappers around keyboard-quit (the default binding of C-g), I discovered that C-g has special behavior. If you bind C-S-g to keyboard-quit, it will not interrupt emacsclient requests. Only C-g will. In fact, C-g will do so even if you unbind it with global-unset-key! I don't know why, but in the end this gives me the ...
You can wrap the relevant portions of you init file in a conditional
on (daemonp), whose docstring reads:
Return non-nil if the current emacs process is a daemon.
If the daemon was given a name argument, return that name.
Some programs run your editor and ask you to edit some text. (For instance: writing a commit message in git when you didn't specify one in your command.) They expect you to turn off your editor when you're done with editing, but turning off your entire Emacs server would be pointless. server-edit exists so you can use Emacs with programs that work like that ...
The directory where the socket is created is controlled by this variable:
(and (featurep 'make-network-process '(:family local))
(format "%s/emacs%d" (or (getenv "TMPDIR") "/tmp") (user-uid)))
"The directory in which to place the server socket.
If local sockets are not supported, this is nil.")
If you use tcp sockets (...
Since this is running on X, it might be possible to use a program like xdotool , xdo, or wmctrl
All these programs allow you to list windows, select one, and simulate typing on them.
Another approach would be to run x11vnc on the remote machine and connect to it with a local vnc viewer. The referenced manual page tells you how to forward needed ports using ...
Add the following code to your init.el.
((eq system-type 'windows-nt)
(setq server-auth-dir "~\\.emacs.d\\server\\"))
((eq system-type 'gnu/linux)
(setq server-auth-dir "~/.emacs.d/server/")))
(setq server-name "emacs-server-file")
Then you can get access to server with ...
You can either wrap your two forms in a progn:
emacsclient --no-wait --eval '(progn (message "hi") (setq abc 123))'
Or you can send them separately:
emacsclient --no-wait --eval '(message "hi")' '(setq def 456)'
For me, using emacsclient with the '-n' switch prevents me from getting the "no client connection" warning.
Invoking emacsclient file from the command line will open file in the current Emacs session. The command line from which you ran emacsclient will be waiting for the file to be closed by the server, and then terminate. You won't be able to execute ...
I used the TCP approach suggested by @wvxvw in the comments. I'm starting a TCP server inside emacs, which, when receiving a package, will eval it as elisp code. I found a piece of code for the TCP server somewhere in the internet (I can't seem to find it again, if anybody knows please leave a comment and I'll add to the answer), and made a few changes for ...
This should work with a fairly recent emacsclient, as long as you pass in a prefix that lets the server emacs find remote files over tramp:
I can’t remember if the feature made it in time for 26.1 or not, but it was committed in May 2017. See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=26591 for details. No changes should be ...
Nowadays several megabytes are no problem for a client program.
So maybe the problem of the size of Emacs is no longer that relevant.
With Emacs 27 you can run stuff in ~/.emacs.d/early-init.el before the initial frame opens.
There you can try:
(if (server-running-p "server")
(let ((file-name (expand-file-name (car (cdr command-line-...
If you had started the erroneous example from a terminal, you would have noticed output similiar to the following:
$ emacsclient -c --eval '(helm-locate)'
*ERROR*: Wrong number of arguments: (1 . 1), 0
Comparing the arguments both mu4e and helm-locate require (which can be easily found out with M-x find-function) reveals that while both commands are ...
You'll want to look at the environment frame parameter, which the server.el code sets up whenever a frame is created for a particular client:
(frame-parameter nil 'environment)
This should then hold a list of strings, where some of those strings will be like "SSH_CLIENT=IP PORT1 PORT2".
I think that your 2nd attempt is correct. You should, however, use C-x C-c to quit Emacs and not close the frame created by emacsclient directly through the GUI. I use C-x C-c to quit Emacs and it always asks me whether I want to save my modified buffers even if they aren't the ones I'm using.
On the other hand, it's to be expected that Emacs won't prompt ...
I submitted a bug and got response that this is known bug of GTK since 2002. See etc/PROBLEMS:
** When Emacs is compiled with Gtk+, closing a display kills Emacs.
As temporary solution - it is recommended to use Lucid Emacs build:
$ sudo apt-get install emacs-lucid
You can check out ob-async. It worked well for me.
Someone said that you need to implement the feature for Emacs to do that. But ob-async does exactly what you want. It does run the process asynchronously so it doesn't freeze emacs. It's a package for babel and you don't need to write any LISP code. Just install ob-async using el-get or any other preferred ...