/************************************************************************
 * AREXX Script
 *
 * MGuideErr.ced					Copyright (c) 1993, Gerhard Leibrock
 *
 *                          Derived from:
 * NextError.ced					Copyright (c) 1989, Peter Cherna
 *
 * ARexx program that scans the MakeGuide error file put out by the MakeGuide
 * Version 1.55 document maker (In conjunction with my WB-Interface).
 * ------------
 * Version $VER: MakeGuide.ced 1.00 by Gerhard Leibrock (Oct 7, 1993)
 *
 * WARNING:
 *         ONLY WORKS FOR PRODUCING .guide FILES !!!!
 *         USELESS FOR PRODUCING .doc OR .dvi FILES !!
 * HINT:
 *         USE MY MakeGuide Workbench Package (WBMGv_vv.lha)
 *         (v.vv -> Version Indicator)
 *
 * ONLY WORKS WITH CYGNUS ED PROFESSIONAL AND AREXX !!
 *
 * Gerhard Leibrock, Neuh"auselerstr. 12, 66459 Kirkel, Germany
 * leibrock@fsinfo.cs.uni-sb.de
 ************************************************************************/



/* Allow CygnusEd to pass status variables */
options results

/* Tell ARexx to talk to CygnusEd */
address 'rexx_ced'

cr = '0A'X

status 20 /* get only the file's path */
path = RESULT
IF RIGHT(path,1)~='/' & RIGHT(path,1)~=':' THEN path=path||'/'
path = path || "MakeGuide.err"
if open('errfile', path) then
DO
  offset = 0
	offset = seek('errfile',offset,'B')
	errorstr = readln('errfile')
	offset = seek('errfile',0,'C')

	done = 0
	DO until done = 1
    parse var errorstr txt '®'
		if (length(errorstr) > 1) & ( txt ~= 'Making AmigaGuide') then
		DO	
      /*  For MakeGuide the error file contains lines of the following
          form:
          ``filename:line:errmsg.explanation''
      		where filename is the name of the source module, line
          is the line number of the error, errmsg is the error message
          and explanation explains the error causing item.
      */

			parse var errorstr root ':' file ':' line ':' errmsg

			/*	Check if file's name had no colon.  True if errmsg if empty. */

			if (length(errmsg) = 0) then
			DO
				root = ''
				parse var errorstr file ':' line ':' errmsg
			END
			else
				root = root':'

			sourcefile = file
			/*	Strip off any leading directories: */
			slash = lastpos('/',sourcefile)
			if (slash > 0) then
				sourcefile = substr(sourcefile,slash+1)

			/*	Get current file's name: */
			status 21
			if upper(sourcefile) ~= upper(result) then
			DO
				/* Find out how many views are displayed */
				status 66

				/* Try to find the view containing the error file */
				DO numwins=result-1 to 1 by -1 until upper(result) = upper(sourcefile)
					next view
					/*	Get current file's name: */
					status 21
				END

				/* If we can't find the file, then ask to open it */
				if upper(result) ~= upper(sourcefile) then
				DO
					next view		/* bring back to original view */
					okay2 "Error in line" line "of file" sourcefile || cr || errmsg || cr || "Bring it in?"
					if result = 1 then
					DO
						open new
						open root || file
					END
					else
					DO
						errorstr = readln('errfile')
						offset = seek('errfile',0,'C')
					END
				END
			END

			/*	Get current file's name: */
			status 21
			if upper(sourcefile) = upper(result) then
			DO
				/*	We want to put the cursor on the line containing the
					error.  Check if we're already there.  Get the current
					line (relative to first line = 0) */
				status 47

				jumpto line 1

				okay2 root || file':  Line' line || cr || errmsg || cr || 'Go to next error?'
				if result = 0 then
					done = 1
				else
				DO
					errorstr = readln('errfile')
					offset = seek('errfile',0,'C')
				END
			END
		END
		else
		DO
			okay1 'No more errors.'
			done = 1
		END
	END
	call close 'errfile'
END
ELSE
	OKAY1 'Error Messages not found.'
exit
