/*-- AutoRev header do NOT edit!
*
*   Program         :   Search.c
*   Copyright       :   Copyright © 1991-92 Jaba Development
*   Author          :   Jan van den Baard
*   Creation Date   :   05-Apr-92
*   Current version :   2.0
*   Translator      :   Dice v2.06.40
*
*   REVISION HISTORY
*
*   Date          Version         Comment
*   ---------     -------         ------------------------------------------
*   05-Apr-92     2.0             Search routines. (rewrite)
*
*-- REV_END --*/

#include "View.h"

Prototype void Find( void );
Prototype void FindNP( long );
Prototype void DoFound( void );

extern struct Screen            *vwScreen;
extern struct Window            *vwWindow;
extern struct AsciiText         *vwText;
extern struct Line              *vwFirst, *vwLast;
extern struct StringScan         vwSearch;
extern UBYTE                     vwFBuf[], ClearStr[], vwFound;
extern UWORD                     vwMode, vwMaxLin, vwYMax;
extern ULONG                     vwShown;

void Find( void )
{
    if ( rtGetString( vwFBuf, 256, "Type string to find...", NULL, RT_Window, vwWindow,
                                                                    RT_ReqPos, REQPOS_CENTERSCR,
                                                                    RT_WaitPointer, TRUE,
                                                                    TAG_DONE )) {
        Busy();
        ErrorLine( "Searching..." );
        if ( FindFrom( vwText, vwFBuf, &vwSearch, vwFirst, vwMode )) {
            vwFound = TRUE;
            DoFound();
        } else
            DisplayBeep( vwScreen );
        InfoLine();
        Ready();
    }
}

void FindNP( long dir )
{
    long            ret;

    if ( ! vwText )     return;

    if ( vwFound ) {
        Busy();
        ErrorLine( "Searching..." );
        if ( ! dir ) ret = NextOccurrence( &vwSearch, vwMode );
        else         ret = PreviousOccurrence( &vwSearch, vwMode );

        if ( ret ) {
            DoFound();
            goto doneIt;
        }
    }
    DisplayBeep( vwScreen );
    doneIt:
    Ready();
    InfoLine();
}

void DoFound( void )
{
    struct Line     *line;
    UWORD            i, y = 0, yy;

    vwLast = vwSearch.Line;

    for ( i = 0; i < vwMaxLin; i++ )
        if (( vwLast = vwLast->Next ) == vwText->Last->Next ) break;

    vwFirst = vwLast;

    for ( yy = vwYMax, i = vwMaxLin; i >= 1; i--, yy--  ) {
        if (( vwFirst = vwFirst->Prev ) == vwText->First ) break;
        if ( vwFirst == vwSearch.Line ) y = yy;
    }

    vwShown = NULL;

    for ( line = vwText->First; line != vwLast; line = line->Next )
        vwShown += line->Size;

    Inform( ClearStr );
    DisplayText();
}
