findcmd.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 682 行 · 第 1/2 页

C
682
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#include <stdio.h>
#include <string.h>
#include "vi.h"
#include "rxsupp.h"
#include "win.h"
#ifdef __WIN__
#include "winvi.h"
#endif

static char     *lastFind=NULL,*sStr=NULL;
#ifdef __WIN__
static bool     lastFindWasRegExp;
static bool     lastFindWasCaseIgnore;
static bool     lastFindWasForward;
static bool     lastFindWasWrap;
#endif

static linenum  lastLine=0,cLineNumber=0;
static int      lastCol=0,cColumn=0;

static int setLineCol( char *, linenum *, int *, int );
static int processFind( range *, char *, int (*)( char *, long *, int *, int * ) );

void FindCmdFini( void ){
    MemFree( lastFind );
    MemFree( sStr );
}


/*
 * HilightSearchString - bring a search string into view and hilight it
 */
void HilightSearchString( linenum lineno, int col, int slen )
{
    if( slen > 0 ) {
        GoToColumnOK( col+slen );
    }
    GoToColumnOK( col+1 );
    if( slen > 0 ) {
#ifdef __WIN__
        SetSelRegionCols( lineno, col+1, col + slen );
        DCUpdate();
#else
        DCUpdate();
        HiliteAColumnRange( lineno, col, col+slen-1 );
#endif
    }
    EditFlags.ResetDisplayLine = TRUE;

} /* HilightSearchString */

/*
 * ResetLastFind - set so it is as if no last find was entered
 */
void ResetLastFind( void )
{
    lastLine = 0L;
    lastCol = 0;

} /* ResetLastFind */


/*
 * GetFindForward - get position of forward find string
 */
int GetFindForward( char *st, linenum *ln1, int *col1, int *len1 )
{
    return( GetFind( st, ln1, col1, len1, FINDFL_FORWARD ) );

} /* GetFindForward */

/*
 * GetFindBackwards - get backwards find position
 */
int GetFindBackwards( char *st, linenum *ln1, int *col1, int *len1 )
{
    return( GetFind( st, ln1, col1, len1, FINDFL_BACKWARDS ) );

} /* GetFindBackwards */

/*
 * getFindString - get string and search for it
 */
static int getFindString( range *r, bool is_forward, bool is_fancy, bool search_again )
{
    int         rc;
    char        st[MAX_INPUT_LINE+1];
    char        *res;
    char        *prompt;
    #ifdef __WIN__
        bool            old_ci;
        bool            old_sw;
        bool            old_no;
        fancy_find      ff;
    #endif

    is_fancy = is_fancy;
    search_again = search_again;

    #ifdef __WIN__
        old_ci = EditFlags.CaseIgnore;
        old_sw = EditFlags.SearchWrap;
        old_no = EditFlags.NoReplaceSearchString;
        if( is_fancy ) {
            if( lastFind != NULL ) {
                strcpy( st, lastFind );
                ff.use_regexp = lastFindWasRegExp;
                ff.case_ignore = lastFindWasCaseIgnore;
                ff.search_forward = is_forward;
                ff.search_wrap  = lastFindWasWrap;
            } else {
                st[0] = 0;
            }
            ff.find = st;
            ff.findlen = sizeof( st );
            if( !search_again ) {
                if( !GetFindStringDialog( &ff ) ) {
                    return( ERR_NO_ERR );
                }
            } else {
                EditFlags.NoReplaceSearchString = TRUE;
            }

            is_forward = ff.search_forward;
            EditFlags.CaseIgnore = ff.case_ignore;
            EditFlags.SearchWrap = ff.search_wrap;
            if( !ff.use_regexp ) {
                /* we need to add the string without any changes */
                if( !EditFlags.NoReplaceSearchString ) {
                    AddString2( &lastFind, st );
                    lastFindWasRegExp = FALSE;
                }
                MakeExpressionNonRegular( st );
                EditFlags.NoReplaceSearchString = TRUE;
            }
            res = st;
        } else {
    #endif
            if( is_forward ) {
                prompt = "/";
            } else {
                prompt = "?";
            }
            st[0] = prompt[0];
            rc = PromptForString( prompt, st+1, sizeof( st )-1, &FindHist );
            if( rc != ERR_NO_ERR ) {
                if( rc == NO_VALUE_ENTERED ) {
                    return( ERR_NO_ERR );
                }
                return( rc );
            }
            res = &st[1];       // skip prompt
    #ifdef __WIN__
        }
    #endif

    if( is_forward ) {
        rc = processFind( r, res, GetFindForward );
    } else {
        rc = processFind( r, res, GetFindBackwards );
    }
    #ifdef __WIN__
        EditFlags.NoReplaceSearchString = old_no;
        EditFlags.CaseIgnore = old_ci;
        EditFlags.SearchWrap = old_sw;
        lastFindWasRegExp = ff.use_regexp;
        lastFindWasCaseIgnore = ff.case_ignore;
        lastFindWasForward = ff.search_forward;
        lastFindWasWrap = ff.search_wrap;
    #endif
    EditFlags.LastSearchWasForward = is_forward;
    return( rc );
} /* getFindString */

/*
 * DoFindForward - get string and search for it
 */
int DoFindForward( range *r, long count )
{
    int rc;

    count = count;
    rc = getFindString( r, TRUE, FALSE, FALSE );
    return( rc );

} /* DoFindForward */

/*
 * DoFindBackwards - get string and search for it
 */
int DoFindBackwards( range *r, long count )
{
    int rc;

    count = count;
    rc = getFindString( r, FALSE, FALSE, FALSE );
    return( rc );

} /* DoFindBackwards */

/*
 * FancyDoFindMisc - an EVENT_MISC version of below
 */
int FancyDoFindMisc( void )
{
    range   r;
    int     rc;

    if( CurrentFile == NULL ) {
        // you cant search if theres no file!
        return( ERR_NO_FILE );
    }

    rc = FancyDoFind( &r, 1L );

    if( CurrentLineNumber != r.start.line ) {
        GoToLineNoRelCurs( r.start.line );
    }
    if( CurrentColumn != r.start.column ) {
        GoToColumnOK( r.start.column );
    }
    return( rc );
}

/*
 * FancyDoFind - get string and search for it
 */
int FancyDoFind( range *r, long count )
{
    int rc;

    count = count;
    if( CurrentFile == NULL ) {
        // you cant search if theres no file!
        return( ERR_NO_FILE );
    }
    rc = getFindString( r, TRUE, TRUE, FALSE );
    return( rc );

} /* FancyDoFind */

/*
 * DoNextFindForward - search again, based on last string
 */
int DoNextFindForward( range *r, long count )
{
    char        st=0;

    count = count;
    if( EditFlags.LastSearchWasForward ) {
        return( processFind( r, &st, GetFindForward ) );
    } else {
        return( processFind( r, &st, GetFindBackwards ) );
    }

} /* DoNextFindForward */

/*
 * DoNextFindBackwards - search again, based on last string
 */
int DoNextFindBackwards( range *r, long count )
{
    char        st=0;

    count = count;
    if( !EditFlags.LastSearchWasForward ) {
        return( processFind( r, &st, GetFindForward ) );
    } else {
        return( processFind( r, &st, GetFindBackwards ) );
    }

} /* DoNextFindBackwards */


void jumpTo( range *r )
{
    if( CurrentLineNumber != r->start.line ) {
        GoToLineNoRelCurs( r->start.line );
    }
    if( CurrentColumn != r->start.column ) {
        GoToColumnOK( r->start.column );
    }
}

/*
 * DoNextFindForwardMisc - search again, based on last string (EVENT_MISC)
 */
int DoNextFindForwardMisc( void )
{
    range       r;
    int         rc;

    if( CurrentFile == NULL ) {
        // you cant search if theres no file!
        return( ERR_NO_FILE );
    }
    rc = getFindString( &r, TRUE, TRUE, TRUE );
    jumpTo( &r );

    return( rc );

} /* DoNextFindForwardMisc */

/*
 * DoNextFindBackwardsMisc - search again, based on last string (EVENT_MISC)
 */
int DoNextFindBackwardsMisc( void )
{
    range       r;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?