[comp.emacs] Emacs csh alias

dsill@NSWC-OAS.ARPA.UUCP (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?

hansen@mips.UUCP (Craig Hansen) (12/06/87)

In article <8712041951.AA21105@ucbvax.Berkeley.EDU>, 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.

> Any ideas or alternate approaches?

The %emacs construction will permit you to invoke the job without having to
assume that it's job %1. It would seem that the easiest method to do this is
to pipe the output of the 'jobs' command through sed or awk to generate an
optional '%' in front of the command 'emacs'. A further refinement would be
to put the command line arguments somewhere that the foregrounded emacs will
pick them up an interpret them (e.g. do a visit-file on each argument).

By all rights, this ought to be a shell function, say that %emacs would
foreground the job, but if it's not there, should invoke the command.
Unfortunately, there's no standard way to reset the command line arguments.

-- 
Craig Hansen
Manager, Architecture Development
MIPS Computer Systems, Inc.
...{ames,decwrl,prls}!mips!hansen or hansen@mips.com

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

grady@stinson.uucp (Steven Grady) (12/07/87)

In article <8712041951.AA21105@ucbvax.Berkeley.EDU> 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.

A friend (I helped a little) wrote this:

alias e 'eval "if (-e ~/.emacs.$$) then\\
%?emacs.${$}\\
else\\
(emacs.${$}:>~/.emacs.$$ ; emacs \!*; /bin/rm -f ~/.emacs.$$)\\
endif" >& /dev/null'

     The only process it  forks (other than emacs)  is the rm  to
remove the file.  We also  created a similar alias if you  wanted
to have one of these jobs per directory (ie if you were using vi,
it might be useful).  I haven't really used the alias above -  we
just got something  that seems  to work -  so I  don't know  what
problems there might be.   One marginally annoying thing: if  you
suspend, then restart the emacs, when you finally quit emacs,  it
prints 2  question  marks.    These  are  from  the  if-then-else
statement.  They mean nothing.

	Steven
	grady@postgres.berkeley.edu
	...!ucbvax!grady

derrell@retix.retix.COM (Derrell Lipman) (12/10/87)

Posting-Front-End: GNU Emacs 18.41.14 of Sun Jun 14 1987 on retix (berkeley-unix)



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

Try this:

alias emacs 'jobs > temp_file;
	     grep -s emacs temp_file;
	     set estatus = $status;
	     rm temp_file;
	     if ($status) /usr/local/emacs;
	     if (! $status) fg ?emacs'


A couple of comments about this:

	1. you may not have been familiar with the option of
'fg' to search for a pattern in the job names, e.g. ?emacs

	2. the file 'temp_file' will always have only a few
lines in it, so it would be a bit faster to substitute 'temp_file'
with something like '~/.emacs_jobs'.  if you do this, you
wouldn't need to remove the file each time, which would make
it a bit quicker.  Also, the storage of the status variable
could be eliminated.  (an even better way to do this would be
to pipe the output from 'jobs' to 'grep -s emacs'.  the
problem is that $status gets set wrongly, as 'jobs' is a
builtin command.  i didn't spend the time to see if this
could be gotten around.)

	3. i put each of the commands within the alias on
separate lines, above, to make it easier to see what was
going on, but in actual use, the whole alias should be
entered on one line.

	4. substitute '/usr/local/emacs' with the actual
full path name of your emacs code file.  this is necessary
because 'emacs' has been aliased.  alternatively, you could
use ''emacs, where the two initial single quotes eliminate
the alias expansion.

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!

rbj@ICST-CMR.ARPA (Root Boy Jim) (01/21/88)

Roberto Shironoshita <shirono@eniac.seas.upenn.edu> has improved on my
emacs suspend function. His version:

(defun suspend ()
  "Suspend emacs and alias emacs command to restart"
  (interactive)
  (suspend-emacs "alias emacs 'unalias emacs ; %emacs'"))

This does not require an exit function.

	(Root Boy) Jim Cottrell	<rbj@icst-cmr.arpa>
	National Bureau of Standards
	Flamer's Hotline: (301) 975-5688
An INK-LING?  Sure -- TAKE one!!  Did you BUY any COMMUNIST UNIFORMS??

thomas%spline.utah.edu.uucp@utah-gr.UUCP (Spencer W. Thomas) (01/21/88)

Here is a pair of aliases that I've been using for years.  The real
work is done by the 's' alias and the start command:

alias e 's emacs \!*'
alias s 'jobs >~/.pcs; eval `start < ~/.pcs \!*`'

Here is start.c:

/*
 * start - figure out if a process is already running
 * from the output of the csh's "jobs" command.
 */

#include <stdio.h>

char *index(), *rindex();

main(argc, argv)
char **argv;
{
	register char *cp;
	int parens = 0;
	char line[512];

	if (argc <= 1)
		exit(1);

	while (gets(line) > 0)
	{
		if ((cp = index(line, 'S')) == NULL)
			continue;
		if (strncmp(cp, "Stopped", 7) != 0)
			continue;
		for (cp += 7; *cp; cp++)
		{
		    if (*cp == '(')
		    	parens++;
		    if (parens == 0 && *cp != ' ')
		        break;
		    if (*cp == ')')
		    	parens--;
		}
		if (*cp == 0)
		    continue;

		if (strncmp(cp, argv[1], strlen(argv[1])) != 0)
			continue;
		if (argc > 2)
			fprintf(stderr, "Arguments ignored for continue of %s\n",
				argv[1]);
		cp = index(line, ']');
		*cp = 0;
		cp = index(line, '[') + 1;
		printf("%%%s\n", cp);
		exit(0);
	}

	/* Not already running: start it up */
	for (argv++, argc--; argc > 0; argv++, argc--)
		printf("%s ", *argv);
	printf("\n");
	exit(0);
}

=Spencer   ({ihnp4,decvax}!utah-cs!thomas, thomas@cs.utah.edu)