[comp.unix.wizards] Emacs csh alias

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

I've been trying to set up a C-Shell (4.2 BSD) alias for Emacs (GNU
17.64, not that it matters) which, when run the first time will
actually run Emacs, but after suspending Emacs with C-z, will bring
the background Emacs job to the foreground.  The catch is that I'd
also like the alias to re-load emacs if I exit with C-x C-c.  Simply
stated, I want an alias named "emacs" which will load Emacs if it
isn't already loaded, but will foreground a background Emacs if one
exists.

I know I could do this with a script (if I assume the Emacs job is
always job %1), but I'd prefer an alias since they're faster.  It
would be especially nice to determine which background job was the
Emacs job and foreground *it*, instead of just assuming job %1.

Any ideas or alternate approaches?  Should I just put up with the
occasional "fg: No such job." message?

zap@draken.nada.kth.se (Svante Lindahl) (12/06/87)

[Warning: Extensive inclusion, but I have included a new newsgroup in
 the newsgroups-line, and directed followups to it (comp.emacs)]

In article <10672@brl-adm.ARPA> dsill@NSWC-OAS.arpa (Dave Sill) writes:
>I've been trying to set up a C-Shell (4.2 BSD) alias for Emacs (GNU
>17.64, not that it matters) which, when run the first time will
>actually run Emacs, but after suspending Emacs with C-z, will bring
>the background Emacs job to the foreground.  The catch is that I'd
>also like the alias to re-load emacs if I exit with C-x C-c.  Simply
>stated, I want an alias named "emacs" which will load Emacs if it
>isn't already loaded, but will foreground a background Emacs if one
>exists.
>
>I know I could do this with a script (if I assume the Emacs job is
>always job %1), but I'd prefer an alias since they're faster.  It
>would be especially nice to determine which background job was the
>Emacs job and foreground *it*, instead of just assuming job %1.
>
>Any ideas or alternate approaches?  Should I just put up with the
>occasional "fg: No such job." message?

Here is something which should do part of what you want. It doesn't
accomplish to start a new emacs process if you exited the last one
with C-x C-c - unless the first one had never been suspended!
Whenever you get "fg: No such job" just type ``i!!'', reinvoking the
commandline prefixed with an "i", "iemacs" standing for "init emacs".

alias emacs iemacs
alias iemacs 'alias emacs remacs; "emacs" \!* ; alias emacs iemacs'
alias remacs fg %emacs

Here we use a special version of suspend-emacs, that will look for a
file ".emacs_pause" in the user's home directory when emacs is
resumed. In this file suspend-emacs expects to find the current
working directory and an optional "command line" that is parsed like
the initial command line.  Very useful!
This could be done using "suspend-resume-hook", but the hook wasn't
available in 17.?? when this was first implemented here.

These are the aliases I use together with the special version of
suspend-emacs.

alias emacs iemacs
alias remacs 'echo `pwd` \!* >\! ~/.emacs_pause ; %emacs'
alias iemacs 'alias emacs remacs; "emacs" \!* ; alias emacs iemacs'
alias kemacs 'alias emacs iemacs; remacs -kill'


Svante Lindahl		zap@nada.kth.se		uunet!nada.kth.se!zap

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

[ I'd have liked to mail this, but our mailer dislikes mixed !-%-@ syntax. ]

In article <brl-adm.10672> dsill@NSWC-OAS.arpa (Dave Sill) writes:
>I've been trying to set up a C-Shell (4.2 BSD) alias for Emacs (GNU
>17.64, not that it matters) which, when run the first time will
>actually run Emacs, but after suspending Emacs with C-z, will bring
>the background Emacs job to the foreground.  The catch is that I'd
>also like the alias to re-load emacs if I exit with C-x C-c.
>
>I know I could do this with a script (if I assume the Emacs job is
>always job %1), but I'd prefer an alias since they're faster.

First, a general point.  Almost anything you can do in a csh script can be
caused to happen in the current shell by:
1)  Write and debug the csh script.
2)  In .cshrc, include a line like
    alias script_name "set argv=(\!*:p) ; source path_to_script/script_name"
Please remember that any variables set/modified in the script will be those
in your current shell.  This may or may not be desirable.

Now, as to the specific question.  The script looks like

    jobs >! /tmp/j$$
    if ( "`grep emacs /tmp/j$$`" != "" ) then
	fg %emacs
    else
	\emacs $*
    endif

assuming this is saved in ~/lib/emacs, the alias is

    alias emacs "set argv=(\!*:p) ; source ~/lib/emacs"

and you're all set.

The fun stuff with "jobs" and "grep" is necessary because "source" quits if
any command in the sourced file fails, so you have to make sure "fg" will
work before you try it.   The script won't work if you happen to have more
than one suspended emacs ("grep ..." would be ambiguous) or if you have some
other suspended job with "emacs" in the command string.

-- 
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

spuhler@hpisoa2.HP.COM (Tom Spuhler) (12/08/87)

>I've been trying to set up a C-Shell (4.2 BSD) alias for Emacs (GNU
>17.64, not that it matters) which, when run the first time will
>actually run Emacs, but after suspending Emacs with C-z, will bring
>the background Emacs job to the foreground.  The catch is that I'd
>also like the alias to re-load emacs if I exit with C-x C-c.  Simply
>stated, I want an alias named "emacs" which will load Emacs if it
>isn't already loaded, but will foreground a background Emacs if one
>exists.
>
>I know I could do this with a script (if I assume the Emacs job is
>always job %1), but I'd prefer an alias since they're faster.  It
>would be especially nice to determine which background job was the
>Emacs job and foreground *it*, instead of just assuming job %1.

I have the following script (called 'v') which works for 'vi' and would only 
require a tiny twiddle to work for emacs instead:
------------

#  must be sourced
jobs | grep -l '        vi ' >/dev/null
if ( $status == 0 ) then
  fg %vi
else
  vi $*
endif

------------
This script must be sourced (as anything that acts on the current shell) and 
just looks to see if the command exists (note the leading spaces) and 'fg's
it if it does or reinvokes it otherwise.  change 'vi' to 'emacs' and you 
should be there.  The easy way, just do a 'fg %vi' and check to see if it works
if not reinvoke, doesn't quite work correctly as $status = 1 if the fg'd
program doesn't exist OR is resuspened, or some other builtin problems crop up.
--Tom 

by the by, I did this on hpux s800 1.2

rbj@icst-cmr.arpa (Root Boy Jim) (01/14/88)

Several people have commented on the emacs alias issue. The solutions
focused on csh voodoo. As usual, there is more than one way to skin a cat.
Attack the problem from the other direction, with emacs voodoo.

(defun suspend ()
  "Suspend and realias emacs"
  (interactive)
  (suspend-emacs "alias emacs %emacs"))

(defun exit ()
  "Exit and unalias emacs. Too bad suspend-emacs won't take a string to stuff"
  (interactive)
  (suspend-emacs "unalias emacs; %emacs")
  (save-buffers-kill-emacs))

Then simply bind these functions to whatever keys you like.

As noted in the exit documentation, a stuff string 
save-buffers-kill-emacs would be desirable.

	(Root Boy) Jim Cottrell	<rbj@icst-cmr.arpa>
	National Bureau of Standards
	Flamer's Hotline: (301) 975-5688
Now I'm concentrating on a specific tank battle toward the end of World War II!