/* AlphaSpell.xdme */
/* Copyright © 1994 Fergus Duniho */

if ~show("L","rexxarplib.library") then
    if ~addlib("rexxarplib.library",0,-30,0) then exit

options results
address 'XDME.1'
'rxresult $Dict'
D = result

top
first
'blocktype character'
'title `Spell Checking ...'
C = "execute (C:AlphaSpell S <T:TEXT >T:B1" D || ")"
C

Call Open 'List', 'T:B1', 'R'
W.=''; L.=0; X=0
Do Until EOF('List')
    X = X + 1
    W.X = Readln('List')
    L = Readln('List')
    L.X = Value('L')
End
X = X - 1
Call Close 'List'
If X = 0 Then Do
    'title (No words are misspelled.)'
    Exit
End

/* Requester */

'ignorecase on'
TRUE = (A = A); FALSE = ~(A = A); YES.TRUE = 'Yes'; YES.FALSE = 'No '
A. = FALSE; X=X-1; Y=1; Z=0; O=0; G=''; NW=''; NL=d2c(10)
Call Open 'Dict', D

Address AREXX '"Call CreateHost CPORT, NOTPORT"'
Do Until ShowList(P,CPORT)
    Call Delay(10)
End

IDCMP = 'WINDOWDRAG+GADGETUP+CLOSEWINDOW'
FLAGS = 'ACTIVATE+WINDOWDRAG+WINDOWCLOSE'

Call OpenWindow CPORT, 45, 0, 505, 80, IDCMP, FLAGS, 'XDMD AlphaSpell Interface V1.00 © 1992-4 Fergus Duniho'
Call SetReqColor CPORT, OKAYPEN, 1
Call Move CPORT, 10, 20
Call Text CPORT, "Word    :" W.Y
Call Move CPORT, 400, 20
Call Text CPORT, "Add : No"
Call Move CPORT, 10, 34
Call Text CPORT, "Replace :"
Call AddGadget CPORT, 90, 26, 'newword', NW, 'NW = "(%g)"', 310
Call AddGadget CPORT, 12, 44, 'skip', ' Skip ', 'Call Skip(1)'
Call AddGadget CPORT, 79, 44, 'back', ' Back ', 'Call Skip(-1)'
Call AddGadget CPORT, 146, 44, 'find',' Find ', 'Call FindW'
Call AddGadget CPORT, 212, 44, 'add', ' Add ', 'If ~A.Y Then Z=Z+1; A.Y=TRUE; Call Skip(1)'
Call AddGadget CPORT, 270, 44, 'unadd', ' Unadd ', 'If A.Y Then Z=Z-1; A.Y=FALSE; Call Skip(1)'
Call AddGadget CPORT, 344, 44, 'addall', ' Add All ', 'A. = TRUE; Z=1; Call Done'
Call AddGadget CPORT, 436, 44, 'done', ' Done ', 'Call Done'
Call AddGadget CPORT, 10, 62, 'view', ' View Dictionary ', 'Call View'
Call AddGadget CPORT, 163, 62, 'repfind', ' Replace & Find ', 'Call Replace; Call FindW'
Call AddGadget CPORT, 305, 62, 'replace', ' Replace ', 'Call Replace'
Call AddGadget CPORT, 395, 62, 'repall', ' Replace All ', 'Call RepAll'
Call ModifyHost CPORT, CLOSEWINDOW, 'Z=0; Call Done'

Call OpenPort MAINPORT
Do Until ShowList(P, MAINPORT)
    Call Delay(10)
End

Call SetNotify(CPORT, GADGETUP, MAINPORT)
Call SetNotify(CPORT, CLOSEWINDOW, MAINPORT)

Do Forever
    Call WaitPkt(MAINPORT)
    PACKET = GetPkt(MAINPORT)
    If PACKET ~== Null() Then Interpret GetArg(packet, 0)
End

Skip:
    Arg I
    Call Move CPORT, 90, 20
    Call Text CPORT, Copies(' ',Length(W.Y))
    Y=Y+I
    If Y=0 Then Y=X-1
    If Y=X Then Y=1
    top
    first
    Call AddGadget CPORT, 90, 26, 'newword', NW, 'NW = "(%g)"', 310
    Call ActivateGadget CPORT, 'newword'
    Call Move CPORT, 90, 20
    Call Text CPORT, W.Y
    Call Move CPORT, 448, 20
    Q = A.Y
    Call Text CPORT, YES.Q
Return

FindW:
    unblock
    find W.Y
    block
    C = "repeat" (length(W.Y) - 1) "right"
    C
    block
    wleft
Return

Replace:
    findstr W.Y
    repstr NW
    replace
Return

Repall:
    findstr W.Y
    repstr NW
    "repeat -1 nextr"
    Call Skip 1
Return

Backward:
    Call Seek 'Dict', -(Length(G)+2), 'C'
    Do Until Readch('Dict')=NL
        Call Seek 'Dict', -2, 'C'
    End
    Call Guess
Return

Done:
    unblock
    Call Close 'Dict'
    If Z>0 Then Do
        'title `Adding New Words ...'
        N = D'.new'
        If Exists(N) Then Call Open 'New', N, 'A'
                     Else Call Open 'New', N, 'W'
        Do Y = 1 to X-1
            If A.Y Then Call Writeln 'New', W.Y
        End
        Call Close 'New'
    End
    Call Quit CPORT
Exit

View:
    C = "execute (SYS:Utilities/more" D || ")"
    C
Return
