#!/bin/csh

# etc/precmd-vsh - tcsh and vshnu/vsh interaction setup script
# Steve Kinzler, steve@kinzler.com, Mar 98/Mar 99/Mar 02/Jul 03/Aug 22
# see website https://kinzler.com/me/vshnu/
# https://kinzler.com/me/home.html#unixcfg
# see also https://kinzler.com/me/home.html#binp/penv

# For use with the vsh*<->tcsh integration.  This file allows directory
# changes to be communicated between vsh* and tcsh, environment changes
# to be propagated from tcsh to vsh*, and chosen file lists to be progated
# from vsh* to tcsh.  It provides quick aliases for switching back to vsh*.
# It assumes the `penv` command script is available.

# To use this, you may install this file as ~/etc/precmd-vsh and add the
# following to your ~/.tcshrc or ~/.cshrc file:
#
#	if ( $?prompt && $?tcsh ) then
#		setenv VSHNUTMP /tmp
#		alias precmd 'source ~/etc/precmd-vsh'
#		alias V 'set precmd_vsh1; $VSH'
#		if ( ! $?alias_v ) then
#			alias v '\ls >& /dev/null || cd; V'
#			alias vv V
#			set alias_v
#		endif
#	endif
#	setenv VSH vshnu	# vsh* only
#
# and then start a new vsh* supplemental shell with a "v" command (the
# first time) or "V" (any time), and return to vsh* mode with "v".  "vv"
# returns to vsh* mode in the same directory you were last in there.

if ( $?precmd_vsh1 ) then
	jobs -l >& $VSHNUTMP/precmd-vsh$$
	set vshpid=`sed -n '/[ (]vsh/s/.* \([0-9]*\) S[tu].*/\1/p' \
		   $VSHNUTMP/precmd-vsh$$`
	\rm -f	   $VSHNUTMP/precmd-vsh$$
	set vshtmp=$VSHNUTMP/vsh$vshpid
	set vshenv=$VSHNUTMP/env$vshpid
	unset vshpid

	alias getvsh 'set vshdir="`sed 1q < $vshtmp`";' \
		     'set choose=(`sed 1d < $vshtmp`); \rm -f $vshtmp $vshenv'
	alias setvsh 'set umask=0`umask`; umask 077; echo "$cwd" > $vshtmp;' \
		     'penv > $vshenv; umask $umask; unset umask'
	alias vcd 'pushd "$vshdir" > /dev/null'
	alias vv  "set precmd_vshN; %vsh"
	alias v   'setvsh; vv'

	unset precmd_vsh1; getvsh; vcd
endif
if ( $?precmd_vshN ) then
	unset precmd_vshN; getvsh; vcd
endif
