/************************************************************************
 *
 * LookUpTag.ced					Copyright (c) 1989, Peter Cherna
 *
 * ARexx program for CygnusEd Professional that looks up the word under
 * the cursor in the tags file in the directory of the current file.
 *
 * Version 1.05:  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
	/*  Get current directory: */
	status 20
	currdir = result
	if (right(currdir,1) ~= ':') then
		currdir = currdir || '/'
	utarget = upper(target)
	sourcefile = currdir || 'tags'
	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 function '; ' .
				if utarget = function then
					done = 1
			END
		END
		call close 'searchfile'
		if done = 2 then
		DO
			okay1 'No tag found for' target || '.'
			exit
		END
		else
		DO
			parse var searchline function '; ' targetfile '; ' line
			targetfile = currdir || targetfile

			/*  If the file is already displayed, use it,
				else open it into a new window: */

			/*	Get current file's path & name */
			status 19
			if upper(targetfile) ~= upper(result) then
			DO
				/* Find out how many views are displayed */
				status 66

				/* Try to find the view containing the file */
				DO numwins=result-1 to 1 by -1 until upper(result) = upper(targetfile)
					next view
					/*	Get current file's path & name */
					status 19
				END
			END

			/* If we can't find the file, then ask to open it */
			if upper(result) ~= upper(targetfile) then
			DO
				next view		/* bring back to original view */
				open new
				open targetfile
			END

			jumpto line 1
		END
	END
	else
	DO
		okay1 'Tags not found.'
	END
END
exit
