#!/bin/csh -f
#
# This takes standard input, assumes the first line is a command and the
# rest is stuff to be filtered through the command.  It is for binding to
# a key in textedit.  For example, put the following in your .textswrc:
#	/*
#	 * Take the first line of the pending delete region as a
#	 * command and run the command with the rest of the region as
#	 * input.  The output (including error output) replaces the
#	 * region.  If you want to run a command with a ; in it, use
#	 * ()'s around it.
#	 */
#	KEY_RIGHT(15)   FILTER
#	runfilter

set prefix=/tmp/rf$$
onintr cleanup

# Split up the input into $prefix.00 and .01.  The 00 file will contain
#	the command and the 01 file will contain the text to be filtered.
csplit -f $prefix. -s - 2

# Execute the command redirecting both standard out and error out to
#	regular output so any errors show up in the editor.  Not using
#	eval since the quotes in something like:
#		sed 's/ /	/'
#	disappear too soon and the shell turns the tab turns into a space.
#	Using "cat $prefix.01 | ..." in case there are pipes embedded in
#	the command.
/bin/csh -f <<eof |& cat
cat $prefix.01 | `cat $prefix.00`
eof

# Clean up temp files.
#
cleanup:
/bin/rm -f $prefix.00 $prefix.01
