* automatic selection of emacsclient/emacs?
@ 2005-06-08 2:32 Hattuari
[not found] ` <2cydneqEPbdzxTvfRVn-uw-zY4eFNvK5D9If6P1QZMOBw@public.gmane.org>
` (4 more replies)
0 siblings, 5 replies; 9+ messages in thread
From: Hattuari @ 2005-06-08 2:32 UTC (permalink / raw)
Is there a clean way to get Emacs to start as a client when there is an
available server? I find it moderately annoying to have to look around to
see if there is already an instance of Emacs started in order to determine
if I should enter `emacs' or `emacsclient'. I much prefer running
emacsclient when I already have one instance of Emacs running. One option
is to try and sniff for emacs with ps and filter out the current processes
such as grep emacs, and (assuming the script I'm running is also called
emacs) the launch script. It would seem more correct to directly check to
see if the service is available. Is there such a capability?
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: automatic selection of emacsclient/emacs?
[not found] ` <2cydneqEPbdzxTvfRVn-uw-zY4eFNvK5D9If6P1QZMOBw@public.gmane.org>
@ 2005-06-08 7:56 ` Jochen Küpper
0 siblings, 0 replies; 9+ messages in thread
From: Jochen Küpper @ 2005-06-08 7:56 UTC (permalink / raw)
Hattuari <susudata-BGxEDysKjuJn9mNO0jIupQsAya0C8aCo@public.gmane.org> writes:
> Is there a clean way to get Emacs to start as a client when there is an
> available server?
RTFM
,----[ (info "(emacs)Invoking emacsclient") ]
| The option `--alternate-editor=COMMAND' is useful when running
| `emacsclient' in a script. It specifies a command to run if
| `emacsclient' fails to contact Emacs. For example, the following
| setting for the EDITOR environment variable will always give you an
| editor, even if no Emacs server is running:
|
| EDITOR="emacsclient --alternate-editor emacs +%d %s"
|
| The environment variable ALTERNATE_EDITOR has the same effect, but the
| value of the `--alternate-editor' takes precedence.
|
| Alternatively, the file `etc/emacs.bash' defines a bash function
| which will communicate with a running Emacs server, or start one if
| none exists.
`----
Greetings,
Jochen
--
Einigkeit und Recht und Freiheit http://www.Jochen-Kuepper.de
Liberté, Égalité, Fraternité GnuPG key: CC1B0B4D
(Part 3 you find in my messages before fall 2003.)
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: automatic selection of emacsclient/emacs?
2005-06-08 2:32 automatic selection of emacsclient/emacs? Hattuari
[not found] ` <2cydneqEPbdzxTvfRVn-uw-zY4eFNvK5D9If6P1QZMOBw@public.gmane.org>
@ 2005-06-08 8:00 ` Thorsten Bonow
2005-06-08 9:17 ` David Kastrup
` (2 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Thorsten Bonow @ 2005-06-08 8:00 UTC (permalink / raw)
>>>>> "Hattuari" == Hattuari <susudata@setidava.kushan.aa> writes:
Hattuari> Is there a clean way to get Emacs to start as a client when there
Hattuari> is an available server? I find it moderately annoying to have to
Hattuari> look around to see if there is already an instance of Emacs
Hattuari> started in order to determine if I should enter `emacs' or
Hattuari> `emacsclient'. I much prefer running emacsclient when I already
Hattuari> have one instance of Emacs running. One option is to try and
Hattuari> sniff for emacs with ps and filter out the current processes such
Hattuari> as grep emacs, and (assuming the script I'm running is also called
Hattuari> emacs) the launch script.
Hi,
this has been asked before and several solutions/approaches have been
posted. Try googling around for them.
One is already coming with GNU Emacs: There are emacs.bash/emacs.csh scripts in
the etc sub-directory of the source tree which should do in principal what you're
trying to achieve.
Hattuari> It would seem more correct to directly check to see if the service
Hattuari> is available. Is there such a capability?
For this you could try something like
emacsclient --eval t > /dev/null 2>&1
echo $?
The exit status $? is only 0 if the client was able to connect to an running
emacs server.
Hope this helps.
For ages now I'm working on a little script which tries to solve this problem in
a more general way (supporting both GNU Emacs/emacsclient and XEmacs/gnuclient),
making it possible to start a mail/news reader from it, open up a new frame/tty
etc. Never got around finishing it since leaving university [7 years ago :-)]
It's basically working, but not nearly finished. If you're interested I can mail
it to you.
Toto
--
Contact information and PGP key at
http://www-users.rwth-aachen.de/thorsten.bonow
[Featured Creature]
NAME: Oscar the Grouch
BIO/STATS: Built in 1969, Oscar was originally orange but was changed
to green very early in his career. He was the first grouch
in what is now a very large family of grouches.
APPEARANCES: Sesame Street, The Adventures of Elmo in Grouchland, and
many specials and home videos.
FAMOUS FOR: Living in a trashcan and having a pet elephant and worm.
SPECIAL TALENTS: Being Grouchy, says "Go Away!" losing his patience.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: automatic selection of emacsclient/emacs?
2005-06-08 2:32 automatic selection of emacsclient/emacs? Hattuari
[not found] ` <2cydneqEPbdzxTvfRVn-uw-zY4eFNvK5D9If6P1QZMOBw@public.gmane.org>
2005-06-08 8:00 ` Thorsten Bonow
@ 2005-06-08 9:17 ` David Kastrup
2005-06-12 14:29 ` Dan Elliott
2005-06-12 20:28 ` Benjamin Rutt
4 siblings, 0 replies; 9+ messages in thread
From: David Kastrup @ 2005-06-08 9:17 UTC (permalink / raw)
Hattuari <susudata@setidava.kushan.aa> writes:
> Is there a clean way to get Emacs to start as a client when there is
> an available server? I find it moderately annoying to have to look
> around to see if there is already an instance of Emacs started in
> order to determine if I should enter `emacs' or `emacsclient'. I
> much prefer running emacsclient when I already have one instance of
> Emacs running. One option is to try and sniff for emacs with ps and
> filter out the current processes such as grep emacs, and (assuming
> the script I'm running is also called emacs) the launch script. It
> would seem more correct to directly check to see if the service is
> available. Is there such a capability?
(info "(emacs) Invoking emacsclient")
[...]
The option `--alternate-editor=COMMAND' is useful when running
`emacsclient' in a script. It specifies a command to run if
`emacsclient' fails to contact Emacs. For example, the following
setting for the EDITOR environment variable will always give you an
editor, even if no Emacs server is running:
EDITOR="emacsclient --alternate-editor emacs +%d %s"
The environment variable ALTERNATE_EDITOR has the same effect, but the
value of the `--alternate-editor' takes precedence.
Alternatively, the file `etc/emacs.bash' defines a bash function
which will communicate with a running Emacs server, or start one if
none exists.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: automatic selection of emacsclient/emacs?
2005-06-08 2:32 automatic selection of emacsclient/emacs? Hattuari
` (2 preceding siblings ...)
2005-06-08 9:17 ` David Kastrup
@ 2005-06-12 14:29 ` Dan Elliott
2005-06-12 18:19 ` Floyd L. Davidson
` (2 more replies)
2005-06-12 20:28 ` Benjamin Rutt
4 siblings, 3 replies; 9+ messages in thread
From: Dan Elliott @ 2005-06-12 14:29 UTC (permalink / raw)
Hattuari wrote:
> Is there a clean way to get Emacs to start as a client when there is an
> available server? I find it moderately annoying to have to look around to
> see if there is already an instance of Emacs started in order to determine
> if I should enter `emacs' or `emacsclient'. I much prefer running
> emacsclient when I already have one instance of Emacs running. One option
> is to try and sniff for emacs with ps and filter out the current processes
> such as grep emacs, and (assuming the script I'm running is also called
> emacs) the launch script. It would seem more correct to directly check to
> see if the service is available. Is there such a capability?
Will I get flammed if I ask what the benefit of running emacs as a
client (opposed to the "usual" way) would be? Could this be an
alternative to using TRAMP?
Thanks.
- dan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: automatic selection of emacsclient/emacs?
2005-06-12 14:29 ` Dan Elliott
@ 2005-06-12 18:19 ` Floyd L. Davidson
2005-06-13 15:29 ` Drew Adams
2005-06-13 18:27 ` kgold
2 siblings, 0 replies; 9+ messages in thread
From: Floyd L. Davidson @ 2005-06-12 18:19 UTC (permalink / raw)
Dan Elliott <dan-underscore-elliott-at-cox-dot-net@spamispathetic.org> wrote:
>Hattuari wrote:
>> Is there a clean way to get Emacs to start as a client when there is an
>> available server? I find it moderately annoying to have to look around to
>> see if there is already an instance of Emacs started in order to determine
>> if I should enter `emacs' or `emacsclient'. I much prefer running
>> emacsclient when I already have one instance of Emacs running.
I've seen code to do that somewhere... but I didn't find it
particularly useful (though you might) for reasons I'll describe
below.
>> One option
>> is to try and sniff for emacs with ps and filter out the current processes
>> such as grep emacs, and (assuming the script I'm running is also called
>> emacs) the launch script. It would seem more correct to directly check to
>> see if the service is available. Is there such a capability?
>
>Will I get flammed if I ask what the benefit of running emacs as
>a client (opposed to the "usual" way) would be? Could this be
>an alternative to using TRAMP?
The number one benefit historically was speed of invocation.
Imagine what it was like back when 10 MHz 68010 cpu's were
commonly what an "Engineering Workstation" was using! Nobody
wants to wait 1-2 minutes for the editor to load...
Today of course we all have supercomputers, and Emacs loads fast
enough that some people even think it is "fast". The client is
*faster*!!
Beyond that, if the server is always running, there are a few
more (dis)advantages. The client's frame can be dumped while it
still has active files buffered, and they will be persistent.
Odd things like search strings and the delete buffer are kept by
the server instead of the client, and are therefore also
persistent. Which makes it possible to fire up another instance
of the client, and use exactly the same (presumably complex and
difficult to type in) search pattern that was last being used by
a client process.
Or... yank back the text just deleted and then discovered to
still be need! Or yank it back into a totally different file.
And of course the whole delete buffer ring is still there...
Alas, some of that can get in the way too. For example, I run a
separate instance of XEmacs/gnus for news and mail, because
there are often literally dozens of buffers active on that
instance and they absolutely get in the way of normal editing.
Custom keymaps can also be troublesum.
So, for me the solution is to run a single (X)Emacs server
instance, and _leave_ _it_ _running_ _all_ _the_ _time_. I start my
server automatically when the window manager is invoked, with a
command that looks like this:
/usr/local/bin/xemacs -l ~/.semacs -iconic -g 100x30 -T "XEmacs Server" &
(The ~/.semacs file is magic, and I'll explain that down a bit.)
There are also several aliases defined in ~/.bashrc, to make life
easier:
To start up a server process:
alias startxemacs='/usr/local/bin/xemacs -l ~/.semacs -iconic \
-g 100x30 -T "XEmacs Server" &'
To start up xemacs for news and email:
alias gnus='/usr/local/bin/xemacs -l ~/.gemacs -f gnus \
-g 105x35+30+-36 -T "GNUS" 2>/dev/null &'
My normal "editor" command is this one:
alias em='/usr/local/bin/gnuclient'
And for use from a virtual console:
alias pw='/usr/local/bin/xemacs -nw'
Note the -l options, which load extra init files. The ~./gemacs
file sets up paths and so on for running gnus, and the "gnus"
alias invokes gnus with the -f option. But the "startxemacs"
alias reads the server init file, ~/.semacs (and uses the
-iconic option to keep it hidden from view).
There is a trick to making this copacetic, because it is all too
easy to accidentally kill the server. All it takes is C-x C-c
when only one client is running, and the server process is gone.
That key sequence is probably a reflex that can't be undone for
many Emacs users...
Here is the magic in ~/.semacs (which works with both Emacs and
XEmacs, even though I normally only use XEmacs):
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Load these modules to provide symbol definitions for compilation
;;;
(eval-when-compile
(when (featurep 'xemacs)
(defun current-frame () "dummy to quiet XEmacs byte-compiler" ()))
(require 'gnuserv))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Setup for running gnuserv, then run it
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; for GNU Emacs only
(unless (featurep 'xemacs)
(setq gnuserv-frame (current-frame)))
;;;
;;; Start up the gnu edit server process
;;;
(gnuserv-start)
;;;
;;; disallow exiting from a command (menu option only)
;;;
(defun do-exit ()
"Exits Emacs or an Emacs client"
(interactive)
(if (gnuserv-running-p)
(if (eq gnuserv-clients nil) (delete-frame) (gnuserv-edit))))
(define-key ctl-x-map "\C-c" 'do-exit)
;;; end of file
Note the remapping of C-x C-c in a way that will kill off the frame
that a client process is displayed in, but will never kill the server
process itself.
However, that isn't a perfect solution. It has a quirk that has
just never bothered me enough to cause an effort at fixing it.
When the client is invoked with a file argument on the command
line, and then while the C-x C-f command is used to view another
file, the results are strange. The command C-x C-c will delete
the first buffer as you exit, but the second will remain buffered
by the server (the client disclaims ownership, so if it was modified,
on exit the client will complain and it has to be either saved or
killed with a kill-buffer command, C-x K).
If the second file, while still buffered by the server, is then
modified some other way (archive it with rcs, for example), the
next time the emacs client is invoked it will ask what to do
about this buffer associated with a file that has been modified
on disk. Answering that it is okay to kill it will cause the
client to *exit*! It does delete that buffer, so the client can
be immediately invoked again with the same command line and all
will be fine. Annoying, but not a show stopper.
Anyway, that's an overall configuration that works for me, in part
because I don't turn computers off...
--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: automatic selection of emacsclient/emacs?
2005-06-08 2:32 automatic selection of emacsclient/emacs? Hattuari
` (3 preceding siblings ...)
2005-06-12 14:29 ` Dan Elliott
@ 2005-06-12 20:28 ` Benjamin Rutt
4 siblings, 0 replies; 9+ messages in thread
From: Benjamin Rutt @ 2005-06-12 20:28 UTC (permalink / raw)
Hattuari <susudata@setidava.kushan.aa> writes:
> Is there a clean way to get Emacs to start as a client when there is an
> available server? I find it moderately annoying to have to look around to
> see if there is already an instance of Emacs started in order to determine
> if I should enter `emacs' or `emacsclient'. I much prefer running
> emacsclient when I already have one instance of Emacs running. One option
> is to try and sniff for emacs with ps and filter out the current processes
> such as grep emacs, and (assuming the script I'm running is also called
> emacs) the launch script. It would seem more correct to directly check to
> see if the service is available. Is there such a capability?
I use the following, which I call '$HOME/bin/e'. I use it as follows:
$ e # opens a window
$ e foo.sh # opens an old or new file
$ e foo.sh:23 # opens foo.sh at line 23
I never have to think if emacs is already loaded or not. Hope it
helps.
#!/bin/sh
EMACS=emacs
if [ -z "$DISPLAY" ]
then
$EMACS ${1+"$@"}
else
if emacsclient --eval "(setenv \"DISPLAY\" \"$DISPLAY\")" >/dev/null 2>&1; then
if [ $# -eq 0 ]; then
emacsclient --eval "(make-frame-on-display \"$DISPLAY\")"
elif [ $# -lt 10 ]; then
for f in $@; do
# handle file.h:234 paths
echo $f | egrep ".*:[0-9]+$" >/dev/null
if [ $? -eq 0 ]; then
emacsclient -n `echo $f | sed 's/\(.*\):\([0-9]\+\)$/+\2/'` `echo $f | sed 's/\(.*\):\([0-9]\+\)$/\1/'`
else
emacsclient -n $f
fi
done
else
echo "`basename $0`: ERROR, opening $# files would take too long"
fi
else
emacs ${1+"$@"} &
fi
fi
--
Benjamin Rutt
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: automatic selection of emacsclient/emacs?
2005-06-12 14:29 ` Dan Elliott
2005-06-12 18:19 ` Floyd L. Davidson
@ 2005-06-13 15:29 ` Drew Adams
2005-06-13 18:27 ` kgold
2 siblings, 0 replies; 9+ messages in thread
From: Drew Adams @ 2005-06-13 15:29 UTC (permalink / raw)
I ask what the benefit of running emacs as a
client (opposed to the "usual" way) would be?
Two possible benefits:
1. Consider using Emacs from some other application, as the editor for a
text field, for example. A single Emacs process (perhaps on a different
machine) can serve multiple users, and the time to start Emacs up and load
appropriate libraries for the external application is avoided for each user.
The users need not be familiar with setting up Emacs etc.; things are set up
for them.
2. You have a running Emacs session. You double-click a file in Windows
Explorer (or do the equivalent in another OS/GUI). Instead of starting a new
Emacs session and opening the file in that new session, your existing
session is used. Depending on your session initialization, this can 1) save
startup time and 2) let you use existing session-state info when
manipulating the open file.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: automatic selection of emacsclient/emacs?
2005-06-12 14:29 ` Dan Elliott
2005-06-12 18:19 ` Floyd L. Davidson
2005-06-13 15:29 ` Drew Adams
@ 2005-06-13 18:27 ` kgold
2 siblings, 0 replies; 9+ messages in thread
From: kgold @ 2005-06-13 18:27 UTC (permalink / raw)
Dan Elliott <dan-underscore-elliott-at-cox-dot-net@spamispathetic.org> writes:
> Hattuari wrote:
> > Is there a clean way to get Emacs to start as a client when there is an
> > available server? I find it moderately annoying to have to look around to
> > see if there is already an instance of Emacs started in order to determine
> > if I should enter `emacs' or `emacsclient'. I much prefer running
> > emacsclient when I already have one instance of Emacs running. One option
> > is to try and sniff for emacs with ps and filter out the current processes
> > such as grep emacs, and (assuming the script I'm running is also called
> > emacs) the launch script. It would seem more correct to directly check to
> > see if the service is available. Is there such a capability?
>
> Will I get flammed if I ask what the benefit of running emacs as a
> client (opposed to the "usual" way) would be? Could this be an
> alternative to using TRAMP?
To Dan:
As your .emacs gets bigger, the emacs startup time can become several
seconds. With client/server, you just start emacs as a server once
per login, and the new emacs clients attach very quickly.
To Hattuari:
I use gnuserv/gnuclient, but this might help.
I use the command 'emacs' once to start the server at login. From
then on, I alias 'e' to gnuclient to start clients. Since I always
start the emacs server at login to read mail, my 'e' command never has
to check if the server is running.
--
Ken Goldman kgold@watson.ibm.com 914-784-7646
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2005-06-13 18:27 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-06-08 2:32 automatic selection of emacsclient/emacs? Hattuari
[not found] ` <2cydneqEPbdzxTvfRVn-uw-zY4eFNvK5D9If6P1QZMOBw@public.gmane.org>
2005-06-08 7:56 ` Jochen Küpper
2005-06-08 8:00 ` Thorsten Bonow
2005-06-08 9:17 ` David Kastrup
2005-06-12 14:29 ` Dan Elliott
2005-06-12 18:19 ` Floyd L. Davidson
2005-06-13 15:29 ` Drew Adams
2005-06-13 18:27 ` kgold
2005-06-12 20:28 ` Benjamin Rutt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).