/************************************************************************
 *
 * LookUpHeader.ced					Copyright (c) 1989, Peter Cherna
 *
 * ARexx program for CygnusEd Professional that looks up the word under
 * the cursor in the compiler headers.
 *
 * Version 1.45:  August 28, 1989	Release 1.2:  August 29, 1989
 *
 ************************************************************************/

options results

address 'rexx_ced'

tabchar = '09'X

/*	Get contents of current line: */
status 55
line = result

/*	Get tab size: */
status 8
tabadjust = result - 1

/*	Get cursor x position (relative to beginning of line = 1): */
status 46
cur = result + 1

i = index(line,tabchar)
DO while i > 0 & i <= cur - tabadjust
	cur = cur - tabadjust
	i = index(line,tabchar,i+1)
END

/*	If the current character is non-alphabetic, then start one character
	over to the left.  This allows the cursor to be immediately after
	the key word (say on a space or bracket.) */

char = substr(line,cur,1)
if (~(datatype(char,'A') | char = '_') & cur > 1) then
	cur = cur - 1

/*	Find leftmost and rightmost alphabetic character adjacent to current: */
right = cur - 1
left = cur + 1
char = 'A'
DO while (datatype(char,'A') | char = '_') & (left > 0)
 	left = left - 1
	if left > 0 then
		char = substr(line,left,1)
END
char = 'A'
DO while (datatype(char,'A') | (char = '_'))
	right = right + 1
	char = substr(line,right,1)
END

if right-left <= 1 then
DO
	getstring
	target = result
	if (target = 'RESULT') then
		exit
END
else
DO
	target = substr(line,left+1,right-left-1)
END

DO
	address command 'WBTF'
	say 'Searching for structure/#define' target '...'
	utarget = upper(target)
	sourcefile = 'ch:Index/Index' || left(target,1)
	if exists(sourcefile) then
	DO
		call open 'searchfile', sourcefile
		done = 0
		DO until (done ~= 0)
			searchline = readln('searchfile')
			if eof('searchfile') then
				done = 2
			else
			DO
				parse upper var searchline match '; ' .
				if utarget = match then
					done = 1
			END
		END
		call close 'searchfile'
		if done = 2 then
		DO
			cedtofront
			okay1 'Could not find' target 'in header files.'
			exit
		END
		else
		DO
			parse var searchline match '; ' headerfile '; ' line
			/*  If we have a help file displayed, use its window,
				else open a new one: */

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

				/* Try to find the view containing the help file */
				DO numwins=result-1 to 1 by -1 until result = helpfile
					next view
					/*	Get current file's name */
					status 21
				END
			END

			/* If we can't find the file, then ask to open it */
			if result ~= helpfile then
			DO
				next view		/* bring back to original view */
				open new
			END
			else
			DO
				/*	Only replace this file if there have been no
					changes.  Get number of changes: */
				status 18
				if result ~= 0 then
				DO
					next view
					open new
				END
			END

			cedtofront
			/*	Set tab size to eight: */
			menu 2 1 7
			open headerfile
			jumpto line 1
			/*	Save file name of the Help Window */
			status 21
			call setclip 'HelpWindow',result
		END
	END
	else
	DO
		cedtofront
		okay1 'Could not find' target 'in header files.'
	END
END
exit
