[comp.unix.wizards] Summary of Emacs csh alias replies

dsill@NSWC-OAS.arpa (Dave Sill) (12/10/87)

My question was: how can one set up a csh alias for emacs that will
run emacs if there isn't one in the background or foreground it if it
is in the background.

I received many replies of the form "%emacs will foreground a
background emacs so you don't have to assume emacs is job %1."  I
vaguely remember reading that in the csh man page but it didn't stick
with me.

Most "solutions" involved grepping the output of jobs to determine if
there was a background emacs job, followed by an if-then-else to
either run or foreground emacs.  I've never been able to get an
if-then-else to work in an alias.  Some suggested putting the if-then-
else in a file to be sourced by the alias.  This works, but requires
reading the file each time the alias is invoked, so I might as well
use a script.

Another problem with some of these solutions was that they used the
jobs command in a pipe.  This doesn't work because the piped jobs
command is run in a subshell that has no jobs.  The alternative is to
redirect the output of jobs to a temporary file, which involves a bit
of file I/O overhead.

Another person suggested modifying the kill-emacs and suspend-emacs
commands to redefine the alias as required.  This works fine in emacs,
which is what I wanted it for, but is not applicable to other editors
or programs.

One person suggested that this should be handled by the shell.  I.e.,
%emacs should foreground emacs if it's there, otherwise invoke emacs.

One person suggested I should put up with the occasional "No such
job." message.

I'd like to thank everyone who took the time to reply to my query.

==============
"Don't let existing sub-optimal solutions cloud your vision."
					-- Fred Fish

schaefer@ogcvax.UUCP (Barton E. Schaefer) (12/11/87)

In article <brl-adm.10750> dsill@NSWC-OAS.arpa (Dave Sill) writes:
}My question was: how can one set up a csh alias for emacs that will
}run emacs if there isn't one in the background or foreground it if it
}is in the background.
}
}Most "solutions" involved grepping the output of jobs to determine if
}there was a background emacs job, followed by an if-then-else to
}either run or foreground emacs.  I've never been able to get an
}if-then-else to work in an alias.  Some suggested putting the if-then-
}else in a file to be sourced by the alias.  This works, but requires
}reading the file each time the alias is invoked, so I might as well
}use a script.

"Might as well use a script" ?!?!?

For something as short as the 6 or 7 lines in the file in question, the
major part of the overhead is NOT in reading the file, but in fork-execing a
new shell which then has to read the file anyway.  Sourcing the file from the
current shell will be MUCH faster than running a script.

Furthermore, what do you think is going to happen when you issue a "%emacs"
in the script?  There isn't any background job running under the script shell!
This is one case where you MUST use "source", or type the commands in yourself
(which you don't want to do or you wouldn't have asked the question in the
first place).

-- 
Bart Schaefer			CSNET:	schaefer@cse.ogc.edu
				UUCP:	...{tektronix,verdix}!ogcvax!schaefer
"A band of BIG, DUMB, LOUDMOUTHED, BUNGLING OGRES is a GREAT ASSET to the
neighbohood.  It keeps out the RIFF-RAFF."		-- Wormy

dsill@NSWC-OAS.arpa (Dave Sill) (12/15/87)

>} is Dave Sill
> is Barton Schaefer

>}Some suggested putting the if-then-
>}else in a file to be sourced by the alias.  This works, but requires
>}reading the file each time the alias is invoked, so I might as well
>}use a script.
>
>"Might as well use a script" ?!?!?
>
>For something as short as the 6 or 7 lines in the file in question, the
>major part of the overhead is NOT in reading the file, but in fork-execing a
>new shell which then has to read the file anyway.  Sourcing the file from the
>current shell will be MUCH faster than running a script.

You're right.  The fork/exec overhead *is* higher than the file I/O
overhead, but I still want to avoid both, if possible.  Your solution
requires reading the sourced file, a fork/exec of grep, and the use of
a temporary file.  There should be a more efficient way.  Probably the
best way to do this is to modify csh.   

>Furthermore, what do you think is going to happen when you issue a "%emacs"
>in the script?  There isn't any background job running under the script shell!

You're right again.  I guess I fell into the same trap that got those
who suggested piping "jobs" to grep.

======
The opinions expressed above are mine.

Willy Wonka: But don't ever forget what happened to the man who
	     suddenly got everything he ever wanted.
    Charlie: What's that?
      Willy: He lived happily ever after.

allbery@ncoast.UUCP (Brandon Allbery) (12/18/87)

As quoted from <1512@ogcvax.UUCP> by schaefer@ogcvax.UUCP (Barton E. Schaefer):
+---------------
| In article <brl-adm.10750> dsill@NSWC-OAS.arpa (Dave Sill) writes:
| }My question was: how can one set up a csh alias for emacs that will
| }run emacs if there isn't one in the background or foreground it if it
| }is in the background.
| 
| Furthermore, what do you think is going to happen when you issue a "%emacs"
| in the script?  There isn't any background job running under the script shell!
| This is one case where you MUST use "source", or type the commands in yourself
| (which you don't want to do or you wouldn't have asked the question in the
| first place).
+---------------

This may be a dumb question, but... assuming the Emacs in question is of the
Gnu persuasion, why don't you use server-mode?  (The only reason I know of
offhand doesn't apply to you:  *I* can't use server-mode because I run System
V and therefore can't use the Berzerk mechanisms of the server.  But with a
job-control csh I daresay your system has the guts to do it.)  Server-mode
starts up an emacs the first time, which goes off into the background; a
small (hence fast) program is then used from then on to communicate with
the backgrounded emacs via a socketpair.  Check out server.el for the nitty
details.
-- 
Brandon S. Allbery		      necntc!ncoast!allbery@harvard.harvard.edu
 {hoptoad,harvard!necntc,cbosgd,sun!mandrill!hal,uunet!hnsurg3}!ncoast!allbery
			Moderator of comp.sources.misc