/************************************************************************
 *
 * insert_adr.ced                by Dirk Federlein
 *
 * Inserts the address of the "person" under the cursor. You must have
 * DFA running to use this script successfully.
 *
 * Lookup part taken from:
 *
 * LookUp.ced						Copyright (c) 1989, Peter Cherna
 *
 * ARexx program for CygnusEd Professional that looks up the word under
 * the cursor.
 *
 * Version 1.30:  August 20, 1989	Release 1.2:  August 29, 1989
 *
 ************************************************************************/

options results

address 'rexx_ced'

tabchar = '09'X
cr	= '0A'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)
	newtarget = '#?'target'#?'
END

DO
	say 'Searching for address' newtarget '...'

	if ~show(ports, DFA) then
	do
		'okay1' 'You should have DFA running, if you' cr 'want to get an address from it!'
        exit 0
	end

	address 'DFA' "SEARCH" newtarget "IGNORECASE FIELDS=ALL STEM ADR."

	if rc=0 then
	do
		"Prev WORD"
		"Mark BLOCK"
		"NEXT WORD"
		"CUT BLOCK"

		text ADR.ADDRESS.0
		text cr
		text ADR.ADDRESS.2
		text " "
		text ADR.ADDRESS.1
		text cr
		text ADR.ADDRESS.3
		text cr
		text ADR.ADDRESS.4
		text " "
		text ADR.ADDRESS.5
		text cr
		text ADR.ADDRESS.6
		text cr
	end
	else
		'okay1' 'Could not find address of' newtarget '! '
END

exit
