/************************************************************************
 *
 * 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
 * New Version    March  23, 1990 Eniac
 ************************************************************************/

options results

address 'rexx_ced'

tabchar = '09'X

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

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

status 7
tabline = result

j = 0
DO i = 1 to length(line)
	j = j + 1
	if i >= cur then leave
	if substr(line,i,1)~=tabchar then iterate
	cur = cur - index(tabline,'T',j) + j
	j = index(tabline,'T',j)
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
                line = '0A'X||readch('searchfile',32000)||'0A'X /* be a nice guy - and smaller than 32k - ok ? */
                if index(getenv("cc-casesign"),'y')=1 then
                        index1 = index(line,'0A'X||target)
                else
                        index1 = index(upper(line),'0A'X||utarget)
                index2 = index(line,'0A'X,index1+1)
                call close 'searchfile'
                if index1 ~=0 then
                        searchline = substr(line,index1,index2-index1)
                else
		DO
			okay1 'No tag found for' target || '.'
			exit
		END
		/* peter chernas maketags -> */ 
		parse var searchline function '; ' targetfile '; ' line
		ctagsline = strip(word(function,2))
		searchword = ''
		if ctagsline ~= '' then 
		DO      /* public domain ctags -> aufruf : for * t1:cc-util/ctags/ctags >>ram:tagsfile -xt %% */
			line = ctagsline
			targetfile = strip(word(function,3))
			function = strip(word(function,1))
		        searchindex = wordindex(searchline,4) 
			searchword  = strip(substr(searchline,searchindex,length(searchline)-searchindex+1))
		END	

		/*  If the file is already displayed, use it,
			else open it into a new window: */
		status 19
		call setclip 'CEDLastWindowFile',result
		status 21
		call setclip 'CEDLastWindowName',result
		status 46
		call setclip 'CEDLastWindowXPos',result + 1
		status 47
		call setclip 'CEDLastWindowYPos',result + 1
		jump to file targetfile
		if result = 0 then
		DO
			jump to file getclip('CEDHelpWindow')
			if result = 0 then 
			DO
				open new
				call setclip 'CEDHelpWindow',targetfile
			END
			else 
			DO
				status 18
				if result ~= 0 then 
				DO
					open new
					call setclip 'CEDHelpWindow',targetfile 	
				END
			END
			open currdir || targetfile
		END
		if (getenv("cc-finder")='search') & (searchword ~= '') then
		DO
			jumpto 1 1
			search for '"'searchword'"'
		END
		else
			jumpto line 1
	END
	else
	DO
		okay1 'Tags not found.'
	END
END
exit
