/* find a matching comment to the one under the cursor */
/* BUG doesn't know about strings and multiple comments ...*/
/* to write in c ! -> strtok 
   area = source and target
   if match of area and token of source then increment count
   if match of area and token of target then decrement count if count==0 then display
   if eof then error

modular: modul suche passende klammer:
if tex: ignore ab %
if ' - ignore to next ', ev. eol oder bis eof sogar, ev. errormeldung

startobject -> tabelle gefundene tokens - aktionen (zB matching suchen fuer dieses item ...)
                                                   zB If ev. reicht  schon ein newline...
                                                   (zB scan to next line - %)
                                                   (zB RETURN - SUCCESS)
                                                   oder MISSING
wie waers ?

ist dies eine lex-yacc anwendung ?

   dieses rexxmacro soll als header dienen und die 2 felder uebergeben
   das programm soll den rc oder aehnliches setzen und so die pos. im file uebergeben
   -> vorher file in ram saven.

*/
/* problem : should ignore extra chars to both sides of the matching pattern ... */
/* but nevertheless it works fine ... */
/* to be expanded */

rexx = 1 /* language flag -> begin / end construct */
address 'rexx_ced'
options results
CALL myword
target = strip(target)
upper target
direction = 0
SELECT
        WHEN target = 'DO'        then match = 'END'
        WHEN target = 'SELECT'    then match = 'OTHERWISE'
	WHEN target = '/*'        then match = '*/'
	WHEN target = '(*'        then match = '*)'
	WHEN target = '{'         then match = '}'
	WHEN target = '('         then match = ')'

        WHEN target = 'BEGIN'     then match = 'END'
	WHEN target = 'OTHERWISE' then do;direction=1;match='SELECT';end 
	WHEN target = '*)'        then do;direction=1;match='(*';end 
	WHEN target = '*/'        then do;direction=1;match='/*';end
	WHEN target = '}'         then do;direction=1;match='{';end
	WHEN target = ')'         then do;direction=1;match='(';end
	WHEN target = 'END'       then do;direction=1;if rexx then match='DO' else match='BEGIN' ;end /* rexx version */
	OTHERWISE DO ;okay1 'INVALID KEY'; EXIT ;END
END
if direction = 0 then 'search for' '"'match'"' 1 0 1 0
else 'search for' '"'match'"' 1 0 0 0
if result ~= 1 then okay1 'not found'

exit 0

myword:						/* get the word under the cursor */
procedure expose target
	tabchar = '09'X
	status 55
	line = result
	status 46
	cur = result + 1
	status 7
	tabline = result
	j = 0
	DO i = 1 to length(line)  /* detab */
		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
	char = substr(line,cur,1)
	right = cur - 1
	left = cur + 1
	char = 'A'
	DO while ( char~='09'x & char~='0A'x & char~=' ') & (left > 0)
	 	left = left - 1
		if left > 0 then
			char = substr(line,left,1)
	END
	char = 'A'
	DO while (char~='09'x & char~='0A'x & 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 target = substr(line,left+1,right-left-1)
RETURN
