
#
# load <var> <file>
#
#	Run a syntax check on <file>. Collect the stderr ans print
#	it if neccessary. Return the line number of the first error
#	or an empty string if the file looks ok.
#
fn load {
	if (~ $#* 1) {
		* = ('' $1)
		}
		
	rc -c '. ' ^ $2 >[2] $rdkerror
	if (~ $status 0) {
		echo -n $2 'looks ok - reload? [Y/n]: '
		ans = `{ awk '{ print substr($0, 1, 1); exit }' }
		if (~ $ans y Y  ||  ~ $#ans 0) {
			. $2
			}

		if (! ~ $1 '') {
			$1 = ''
			}
			
		return 0
		}

	cat $rdkerror >[1=2]
	if (! ~ $1 () '') {
		$1 = `{ awk '{ gsub(/[^0-9]/, "", $2); print $2; exit 0 }' <$rdkerror }
		}

	return 1
	}
	
#
# edit [<file>]
#
#	Load <file> into the $editor and run a syntax check when
#	editing is complete.  If there is an error store it's
#	position load the file otherwise.
#

fn edit {
	~ $1 ()  &&  * = $rdklast
	if (~ $1 () '') {
		echo 'no current file' >[1=2]
		return 1
		}
	
	rdklast = $1
	$editor $rdklast

	while () {
		load rdkerrpos $rdklast
		
		if (~ $rdkerrpos '') {
			break;
		} else {
			echo -n 'edit? [Y/n]: '
			ans = `{ awk '{ print substr($0, 1, 1); exit }' }
			if (! ~ $ans y Y  &&  ! ~ $#ans 0) {
				break;
				}

			$editor +$rdkerrpos $rdklast
			}
		}
	}


# Preset some variables
#
rdklast = ''
~ $editor ()  &&  editor = vi

# Create a file for error messages.
#
mktemp rdkerror

