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

options results

address 'rexx_ced'

tabchar = '09'X

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

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
target = target||';'
DO
        utarget = upper(target)
        sourcefile = 'Index:Index' || left(target,1) 
        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 ? */
                done = 0
                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
                        cedtofront
                        okay1 'Can`t find >'substr(target,1,length(target)-1)'< in header files.'
                        exit
                END
                parse var searchline match '; ' headerfile '; ' line
                        /*  If we have a help file displayed, use its window,
                                else open a new one: */

                helpfile = getclip('CEDHelpWindow')
                jump to file helpfile
                if result = 0 then 
                        open new
                                /*      Only replace this file if there have been no
                                        changes.  Get number of changes: */
                status 18
                if result ~= 0 then
                        open new
                cedtofront
                        /*      Set tab size to eight: */
                status 19
                if result ~= headerfile then  
                DO
                        menu 2 1 7
                        open headerfile
                END
                jumpto line 1
                        /*      Save file name of the Help Window */
                status 21
                call setclip 'CEDHelpWindow',result
        END
        else
        DO
                cedtofront
                okay1 'Indexfile not found.'
        END
END
exit
