[unix-pc.general] Using Gnu Emacs server on a UNIXpc

brant@manta.pha.pa.us (Brant Cheikes) (09/29/88)

I just got around to bringing up Gnu Emacs 18.52, and discovered
that #defining HAVE_SYSVIPC in src/m-7300.h causes the files
etc/server and etc/emacsclient to compile with the SYSV IPC code.
This now makes it possible to use emacs as a server for other
processes (I suspect that the IPC code was added before 18.52, but I
just noticed it).  I thought some of you Gnu'ers out there might find
the following information useful:

To start an emacs server, you run emacs and eval the following elisp
expressions:

	(load "server")
	(server-edit)

You can put these lines in your .emacs if you like.

This will create a couple of "server" subprocesses.  Now switch to
another shell window.  Say you want to edit a file called "frobozz."
Run the "emacsclient" program, found in the etc area of your Emacs
library, giving it "frobozz" as an argument, e.g.,

% /pkg/emacs-18.52/etc/emacsclient frobozz

You should see a message like "Waiting".  Emacsclient sends a message
to the emacs server, passing the filename.  If you switch back to the
emacs window, you should see the file "frobozz" appear.  You can now
go ahead and edit frobozz as you normally would.  You can also switch
buffers, go on to other things as you like, whatever.  But when you
finish editing frobozz, you end the edit with C-x # (C-x followed by
the '#' character).  That will cause the file to be written, and an IPC
message to be sent back to the waiting process.  If you now switch
back to your shell window, you'll see something like "Got back: Close:
0 Done", and you'll be back at the shell prompt.

What's going on is that you have ONE Gnu Emacs process running to
which other processes can pass file editing requests.

Now, you might say, "but isn't it a hassle to have to explicitly think
about whether to run emacs or emacsclient?"  Of course it's a hassle,
but there's a simple solution.

On my system, the Gnu Emacs executable is called /usr/local/bin/gnu-emacs.
I have created a simple shell script, called /usr/local/bin/emacs,
which contains:

#!/bin/sh
BINDIR=/usr/local/bin
EMACSDIR=/pkg/emacs-18.52

if [ "$*" ]
then # filenames were given
	${EMACSDIR}/etc/emacsclient $*
	if [ $? != 0 ]
	then # there is no server
		${BINDIR}/gnu-emacs $*
	fi
else # no filenames given
	${BINDIR}/gnu-emacs
fi

I have a ksh alias "e=emacs" (with /usr/local/bin in my PATH).  When I
type "e" without an argument, a fresh gnu emacs is always brought up.
If, however, I type "e" followed by one or more filenames, then
emacsclient tries to pass the request on to the emacs server.  If
there is no server active, a new gnu is invoked.

Typically, right after I log in I bring up an emacs.  My .emacs starts
the server.  The server comes in handy whenever I use the editor
escape in various programs like Pnews.  When Pnews tries to put me
into the editor to edit an article, it actually runs the emacs script
above, which calls the server.  I no longer have to wait while another
emacs is loaded up; I just switch windows and edit the article.

Anyhoo, though I wouldn't call this a major productivity improver,
it's certainly handy, so I thought I'd pass the word on.

[a word on the Gnu distribution: I'm still distributing it by mail on
UNIXpc floppies, but the update to 18.52 has made past announcements
obsolete.  Write to me for the new info if interested.  You can also
obtain the latest Gnu Emacs distribution by anonymous Internet FTP
from prep.ai.mit.edu in /u2/emacs/edist.tar-18.52.Z, or by anonymous
uucp from osu-cis.  Note that the distribution file is 4+ Mb in size.]
-- 
Brant Cheikes
University of Pennsylvania
Department of Computer and Information Science
Internet: brant@manta.pha.pa.us, UUCP: bpa!manta!brant