[comp.sources.misc] v03i067: runfilter for Sun textedit

liz@grian.UUCP (Liz Allen-Mitchell) (06/30/88)

Posting-number: Volume 3, Issue 67
Submitted-by: "Liz Allen-Mitchell" <liz@grian.UUCP>
Archive-name: runfilter

The following is a program you can use with Sun's textedit to filter
text through arbitray commands -- something I wanted to do for a long
time!  What you do is type the command on a line by itself above the
region you want to filter through the command or where you want the
output of the command to go.  Then select the region and hit the key
you bound runfilter to.  For example, the shar below was created by
putting the line:
	(cd bin; shar runfilter)
in this buffer, selecting it and hitting the R15 key.

It would be faster in C, but it isn't too slow...  The code I found I
needed to use to get the command to run is arcane, but nothing else
wanted to work.  Comments would be appreciated.

Enjoy!

				- Liz

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  runfilter
# Wrapped by liz@grian on Wed Jun 29 10:24:03 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'runfilter' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'runfilter'\"
else
echo shar: Extracting \"'runfilter'\" \(1232 characters\)
sed "s/^X//" >'runfilter' <<'END_OF_FILE'
X#!/bin/csh -f
X#
X# This takes standard input, assumes the first line is a command and the
X# rest is stuff to be filtered through the command.  It is for binding to
X# a key in textedit.  For example, put the following in your .textswrc:
X#	/*
X#	 * Take the first line of the pending delete region as a
X#	 * command and run the command with the rest of the region as
X#	 * input.  The output (including error output) replaces the
X#	 * region.  If you want to run a command with a ; in it, use
X#	 * ()'s around it.
X#	 */
X#	KEY_RIGHT(15)   FILTER
X#	runfilter
X
set prefix=/tmp/rf$$
onintr cleanup
X
X# Split up the input into $prefix.00 and .01.  The 00 file will contain
X#	the command and the 01 file will contain the text to be filtered.
csplit -f $prefix. -s - 2
X
X# Execute the command redirecting both standard out and error out to
X#	regular output so any errors show up in the editor.  Not using
X#	eval since the quotes in something like:
X#		sed 's/ /	/'
X#	disappear too soon and the shell turns the tab turns into a space.
X#	Using "cat $prefix.01 | ..." in case there are pipes embedded in
X#	the command.
X/bin/csh -f <<eof |& cat
cat $prefix.01 | `cat $prefix.00`
eof
X
X# Clean up temp files.
X#
cleanup:
X/bin/rm -f $prefix.00 $prefix.01
END_OF_FILE
if test 1232 -ne `wc -c <'runfilter'`; then
    echo shar: \"'runfilter'\" unpacked with wrong size!
fi
chmod +x 'runfilter'
# end of 'runfilter'
fi
echo shar: End of shell archive.
exit 0
-- 
		- Liz Allen-Mitchell	elroy!grian!liz@csvax.caltech.edu
					ames!elroy!grian!liz
"God is light; in him there is no darkness at all." -- 1 John 1:5b