summaryrefslogtreecommitdiffstats
path: root/kutils/kfind.cpp
blob: d47b1c4a25ea854283599e30920a5295a9327b5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
    Copyright (C) 2001, S.R.Haque <[email protected]>.
    Copyright (C) 2002, David Faure <[email protected]>
    Copyright (C) 2004, Arend van Beelen jr. <[email protected]>
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2, as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kfind.h"
#include "kfinddialog.h"
#include <kapplication.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <tqlabel.h>
#include <tqregexp.h>
#include <tqstylesheet.h>
#include <tqguardedptr.h>
#include <tqptrvector.h>
#include <kdebug.h>

//#define DEBUG_FIND

#define INDEX_NOMATCH -1

class KFindNextDialog : public KDialogBase
{
public:
    KFindNextDialog(const TQString &pattern, TQWidget *parent);
};

// Create the dialog.
KFindNextDialog::KFindNextDialog(const TQString &pattern, TQWidget *parent) :
    KDialogBase(parent, 0, false,  // non-modal!
        i18n("Find Next"),
        User1 | Close,
        User1,
        false,
        KStdGuiItem::find())
{
    setMainWidget( new TQLabel( i18n("<qt>Find next occurrence of '<b>%1</b>'?</qt>").arg(pattern), this ) );
}

////

struct KFind::Private
{
    Private() :
      findDialog(0),
      patternChanged(false),
      matchedPattern(""),
      incrementalPath(29, true),
      emptyMatch(0),
      currentId(0),
      customIds(false)
    {
        incrementalPath.setAutoDelete(true);
        data.setAutoDelete(true);
    }

    ~Private()
    {
        delete emptyMatch;
        emptyMatch = 0;
    }

    struct Match
    {
        Match(int dataId, int index, int matchedLength) :
          dataId(dataId),
          index(index),
          matchedLength(matchedLength)
        { }

        int dataId;
        int index;
        int matchedLength;
    };

    struct Data
    {
        Data() : id(-1), dirty(false) { }
        Data(int id, const TQString &text, bool dirty = false) :
          id(id),
          text(text),
          dirty(dirty)
        { }

        int     id;
        TQString text;
        bool    dirty;
    };

    TQGuardedPtr<TQWidget>  findDialog;
    bool                  patternChanged;
    TQString               matchedPattern;
    TQDict<Match>          incrementalPath;
    Match *               emptyMatch;
    TQPtrVector<Data>      data;
    int                   currentId;
    bool                  customIds;
};

////

KFind::KFind( const TQString &pattern, long options, TQWidget *parent )
    : TQObject( parent )
{
    d = new KFind::Private;
    m_options = options;
    init( pattern );
}

KFind::KFind( const TQString &pattern, long options, TQWidget *parent, TQWidget *findDialog )
    : TQObject( parent )
{
    d = new KFind::Private;
    d->findDialog = findDialog;
    m_options = options;
    init( pattern );
}

void KFind::init( const TQString& pattern )
{
    m_matches = 0;
    m_pattern = pattern;
    m_dialog = 0;
    m_dialogClosed = false;
    m_index = INDEX_NOMATCH;
    m_lastResult = NoMatch;
    if (m_options & KFindDialog::RegularExpression)
        m_regExp = new TQRegExp(pattern, m_options & KFindDialog::CaseSensitive);
    else {
        m_regExp = 0;
    }
}

KFind::~KFind()
{
    delete m_dialog;
    delete d;
}

bool KFind::needData() const
{
    // always true when m_text is empty.
    if (m_options & KFindDialog::FindBackwards)
        // m_index==-1 and m_lastResult==Match means we haven't answered nomatch yet
        // This is important in the "replace with a prompt" case.
        return ( m_index < 0 && m_lastResult != Match );
    else
        // "index over length" test removed: we want to get a nomatch before we set data again
        // This is important in the "replace with a prompt" case.
        return m_index == INDEX_NOMATCH;
}

void KFind::setData( const TQString& data, int startPos )
{
    setData( -1, data, startPos );
}

void KFind::setData( int id, const TQString& data, int startPos )
{
    // cache the data for incremental find
    if ( m_options & KFindDialog::FindIncremental )
    {
        if ( id != -1 )
            d->customIds = true;
        else
            id = d->currentId + 1;

        if ( id >= (int) d->data.size() )
            d->data.resize( id + 100 );

        d->data.insert( id, new Private::Data(id, data, true) );
    }

    if ( !(m_options & KFindDialog::FindIncremental) || needData() )
    {
        m_text = data;

        if ( startPos != -1 )
            m_index = startPos;
        else if (m_options & KFindDialog::FindBackwards)
            m_index = m_text.length();
        else
            m_index = 0;
#ifdef DEBUG_FIND
        kdDebug() << "setData: '" << m_text << "' m_index=" << m_index << endl;
#endif
        Q_ASSERT( m_index != INDEX_NOMATCH );
        m_lastResult = NoMatch;

        d->currentId = id;
    }
}

KDialogBase* KFind::findNextDialog( bool create )
{
    if ( !m_dialog && create )
    {
        m_dialog = new KFindNextDialog( m_pattern, tqparentWidget() );
        connect( m_dialog, TQT_SIGNAL( user1Clicked() ), this, TQT_SLOT( slotFindNext() ) );
        connect( m_dialog, TQT_SIGNAL( finished() ), this, TQT_SLOT( slotDialogClosed() ) );
    }
    return m_dialog;
}

KFind::Result KFind::find()
{
    Q_ASSERT( m_index != INDEX_NOMATCH || d->patternChanged );

    if ( m_lastResult == Match && !d->patternChanged )
    {
        // Move on before looking for the next match, _if_ we just found a match
        if (m_options & KFindDialog::FindBackwards) {
            m_index--;
            if ( m_index == -1 ) // don't call KFind::find with -1, it has a special meaning
            {
                m_lastResult = NoMatch;
                return NoMatch;
            }
        } else
            m_index++;
    }
    d->patternChanged = false;

    if ( m_options & KFindDialog::FindIncremental )
    {
        // if the current pattern is shorter than the matchedPattern we can
        // probably look up the match in the incrementalPath
        if ( m_pattern.length() < d->matchedPattern.length() )
        {
            Private::Match *match = m_pattern.isEmpty() ? d->emptyMatch : d->incrementalPath[m_pattern];
            TQString previousPattern = d->matchedPattern;
            d->matchedPattern = m_pattern;
            if ( match != 0 )
            {
                bool clean = true;

                // find the first result backwards on the path that isn't dirty
                while ( d->data[match->dataId]->dirty == true &&
                        !m_pattern.isEmpty() )
                {
                    m_pattern.truncate( m_pattern.length() - 1 );

                    match = d->incrementalPath[m_pattern];

                    clean = false;
                }

                // remove all matches that lie after the current match
                while ( m_pattern.length() < previousPattern.length() )
                {
                    d->incrementalPath.remove(previousPattern);
                    previousPattern.truncate(previousPattern.length() - 1);
                }

                // set the current text, index, etc. to the found match
                m_text = d->data[match->dataId]->text;
                m_index = match->index;
                m_matchedLength = match->matchedLength;
                d->currentId = match->dataId;

                // if the result is clean we can return it now
                if ( clean )
                {
                    if ( d->customIds )
                        emit highlight(d->currentId, m_index, m_matchedLength);
                    else
                        emit highlight(m_text, m_index, m_matchedLength);

                    m_lastResult = Match;
                    d->matchedPattern = m_pattern;
                    return Match;
                }
            }
            // if we couldn't look up the match, the new pattern isn't a
            // substring of the matchedPattern, so we start a new search
            else
            {
                startNewIncrementalSearch();
            }
        }
        // if the new pattern is longer than the matchedPattern we might be
        // able to proceed from the last search
        else if ( m_pattern.length() > d->matchedPattern.length() )
        {
            // continue from the previous pattern
            if ( m_pattern.startsWith(d->matchedPattern) )
            {
                // we can't proceed from the previous position if the previous
                // position already failed
                if ( m_index == INDEX_NOMATCH )
                    return NoMatch;

                TQString temp = m_pattern;
                m_pattern.truncate(d->matchedPattern.length() + 1);
                d->matchedPattern = temp;
            }
            // start a new search
            else
            {
                startNewIncrementalSearch();
            }
        }
        // if the new pattern is as long as the matchedPattern, we reset if
        // they are not equal
        else if ( m_pattern != d->matchedPattern )
        {
             startNewIncrementalSearch();
        }
    }

#ifdef DEBUG_FIND
    kdDebug() << k_funcinfo << "m_index=" << m_index << endl;
#endif
    do
    {
        // if we have multiple data blocks in our cache, walk through these
        // blocks till we either searched all blocks or we find a match
        do
        {
            // Find the next candidate match.
            if ( m_options & KFindDialog::RegularExpression )
                m_index = KFind::find(m_text, *m_regExp, m_index, m_options, &m_matchedLength);
            else
                m_index = KFind::find(m_text, m_pattern, m_index, m_options, &m_matchedLength);

            if ( m_options & KFindDialog::FindIncremental )
                d->data[d->currentId]->dirty = false;

            if ( m_index == -1 && d->currentId < (int) d->data.count() - 1 )
            {
                m_text = d->data[++d->currentId]->text;

                if ( m_options & KFindDialog::FindBackwards )
                    m_index = m_text.length();
                else
                    m_index = 0;
            }
            else
                break;
        } while ( !(m_options & KFindDialog::RegularExpression) );

        if ( m_index != -1 )
        {
            // Flexibility: the app can add more rules to validate a possible match
            if ( validateMatch( m_text, m_index, m_matchedLength ) )
            {
                bool done = true;

                if ( m_options & KFindDialog::FindIncremental )
                {
                    if ( m_pattern.isEmpty() ) {
                        delete d->emptyMatch;
                        d->emptyMatch = new Private::Match( d->currentId, m_index, m_matchedLength );
                    } else
                        d->incrementalPath.tqreplace(m_pattern, new Private::Match(d->currentId, m_index, m_matchedLength));

                    if ( m_pattern.length() < d->matchedPattern.length() )
                    {
                        m_pattern += d->matchedPattern.mid(m_pattern.length(), 1);
                        done = false;
                    }
                }

                if ( done )
                {
                    m_matches++;
                    // Tell the world about the match we found, in case someone wants to
                    // highlight it.
                    if ( d->customIds )
                        emit highlight(d->currentId, m_index, m_matchedLength);
                    else
                        emit highlight(m_text, m_index, m_matchedLength);

                    if ( !m_dialogClosed )
                        findNextDialog(true)->show();

#ifdef DEBUG_FIND
                    kdDebug() << k_funcinfo << "Match. Next m_index=" << m_index << endl;
#endif
                    m_lastResult = Match;
                    return Match;
                }
            }
            else // Skip match
            {
                if (m_options & KFindDialog::FindBackwards)
                    m_index--;
                else
                    m_index++;
            }
        }
        else
        {
            if ( m_options & KFindDialog::FindIncremental )
            {
                TQString temp = m_pattern;
                temp.truncate(temp.length() - 1);
                m_pattern = d->matchedPattern;
                d->matchedPattern = temp;
            }

            m_index = INDEX_NOMATCH;
        }
    }
    while (m_index != INDEX_NOMATCH);

#ifdef DEBUG_FIND
    kdDebug() << k_funcinfo << "NoMatch. m_index=" << m_index << endl;
#endif
    m_lastResult = NoMatch;
    return NoMatch;
}

void KFind::startNewIncrementalSearch()
{
    Private::Match *match = d->emptyMatch;
    if(match == 0)
    {
        m_text = TQString::null;
        m_index = 0;
        d->currentId = 0;
    }
    else
    {
        m_text = d->data[match->dataId]->text;
        m_index = match->index;
        d->currentId = match->dataId;
    }
    m_matchedLength = 0;
    d->incrementalPath.clear();
    delete d->emptyMatch;
    d->emptyMatch = 0;
    d->matchedPattern = m_pattern;
    m_pattern = TQString::null;
}

// static
int KFind::find(const TQString &text, const TQString &pattern, int index, long options, int *matchedLength)
{
    // Handle regular expressions in the appropriate way.
    if (options & KFindDialog::RegularExpression)
    {
        TQRegExp regExp(pattern, options & KFindDialog::CaseSensitive);

        return find(text, regExp, index, options, matchedLength);
    }

    bool caseSensitive = (options & KFindDialog::CaseSensitive);

    if (options & KFindDialog::WholeWordsOnly)
    {
        if (options & KFindDialog::FindBackwards)
        {
            // Backward search, until the beginning of the line...
            while (index >= 0)
            {
                // ...find the next match.
                index = text.tqfindRev(pattern, index, caseSensitive);
                if (index == -1)
                    break;

                // Is the match delimited correctly?
                *matchedLength = pattern.length();
                if (isWholeWords(text, index, *matchedLength))
                    break;
                index--;
            }
        }
        else
        {
            // Forward search, until the end of the line...
            while (index < (int)text.length())
            {
                // ...find the next match.
                index = text.tqfind(pattern, index, caseSensitive);
                if (index == -1)
                    break;

                // Is the match delimited correctly?
                *matchedLength = pattern.length();
                if (isWholeWords(text, index, *matchedLength))
                    break;
                index++;
            }
            if (index >= (int)text.length()) // end of line
                index = -1; // not found
        }
    }
    else
    {
        // Non-whole-word search.
        if (options & KFindDialog::FindBackwards)
        {
            index = text.tqfindRev(pattern, index, caseSensitive);
        }
        else
        {
            index = text.tqfind(pattern, index, caseSensitive);
        }
        if (index != -1)
        {
            *matchedLength = pattern.length();
        }
    }
    return index;
}

// static
int KFind::find(const TQString &text, const TQRegExp &pattern, int index, long options, int *matchedLength)
{
    if (options & KFindDialog::WholeWordsOnly)
    {
        if (options & KFindDialog::FindBackwards)
        {
            // Backward search, until the beginning of the line...
            while (index >= 0)
            {
                // ...find the next match.
                index = text.tqfindRev(pattern, index);
                if (index == -1)
                    break;

                // Is the match delimited correctly?
                //pattern.match(text, index, matchedLength, false);
                /*int pos =*/ pattern.search( text.mid(index) );
                *matchedLength = pattern.matchedLength();
                if (isWholeWords(text, index, *matchedLength))
                    break;
                index--;
            }
        }
        else
        {
            // Forward search, until the end of the line...
            while (index < (int)text.length())
            {
                // ...find the next match.
                index = text.tqfind(pattern, index);
                if (index == -1)
                    break;

                // Is the match delimited correctly?
                //pattern.match(text, index, matchedLength, false);
                /*int pos =*/ pattern.search( text.mid(index) );
                *matchedLength = pattern.matchedLength();
                if (isWholeWords(text, index, *matchedLength))
                    break;
                index++;
            }
            if (index >= (int)text.length()) // end of line
                index = -1; // not found
        }
    }
    else
    {
        // Non-whole-word search.
        if (options & KFindDialog::FindBackwards)
        {
            index = text.tqfindRev(pattern, index);
        }
        else
        {
            index = text.tqfind(pattern, index);
        }
        if (index != -1)
        {
            //pattern.match(text, index, matchedLength, false);
            /*int pos =*/ pattern.search( text.mid(index) );
            *matchedLength = pattern.matchedLength();
        }
    }
    return index;
}

bool KFind::isInWord(TQChar ch)
{
    return ch.isLetter() || ch.isDigit() || ch == '_';
}

bool KFind::isWholeWords(const TQString &text, int starts, int matchedLength)
{
    if ((starts == 0) || (!isInWord(text[starts - 1])))
    {
        int ends = starts + matchedLength;

        if ((ends == (int)text.length()) || (!isInWord(text[ends])))
            return true;
    }
    return false;
}

void KFind::slotFindNext()
{
    emit findNext();
}

void KFind::slotDialogClosed()
{
    emit dialogClosed();
    m_dialogClosed = true;
}

void KFind::displayFinalDialog() const
{
    TQString message;
    if ( numMatches() )
        message = i18n( "1 match found.", "%n matches found.", numMatches() );
    else
        message = i18n("<qt>No matches found for '<b>%1</b>'.</qt>").arg(TQStyleSheet::escape(m_pattern));
    KMessageBox::information(dialogsParent(), message);
}

bool KFind::shouldRestart( bool forceAsking, bool showNumMatches ) const
{
    // Only ask if we did a "find from cursor", otherwise it's pointless.
    // Well, unless the user can modify the document during a search operation,
    // hence the force boolean.
    if ( !forceAsking && (m_options & KFindDialog::FromCursor) == 0 )
    {
        displayFinalDialog();
        return false;
    }
    TQString message;
    if ( showNumMatches )
    {
        if ( numMatches() )
            message = i18n( "1 match found.", "%n matches found.", numMatches() );
        else
            message = i18n("No matches found for '<b>%1</b>'.").arg(TQStyleSheet::escape(m_pattern));
    }
    else
    {
        if ( m_options & KFindDialog::FindBackwards )
            message = i18n( "Beginning of document reached." );
        else
            message = i18n( "End of document reached." );
    }

    message += "<br><br>"; // can't be in the i18n() of the first if() because of the plural form.
    // Hope this word puzzle is ok, it's a different sentence
    message +=
        ( m_options & KFindDialog::FindBackwards ) ?
        i18n("Continue from the end?")
        : i18n("Continue from the beginning?");

    int ret = KMessageBox::questionYesNo( dialogsParent(), TQString("<qt>")+message+TQString("</qt>"),
                                          TQString::null, KStdGuiItem::cont(), KStdGuiItem::stop() );
    bool yes = ( ret == KMessageBox::Yes );
    if ( yes )
        const_cast<KFind*>(this)->m_options &= ~KFindDialog::FromCursor; // clear FromCursor option
    return yes;
}

void KFind::setOptions( long options )
{
    m_options = options;

    delete m_regExp;
    if (m_options & KFindDialog::RegularExpression)
        m_regExp = new TQRegExp(m_pattern, m_options & KFindDialog::CaseSensitive);
    else
        m_regExp = 0;
}

void KFind::closeFindNextDialog()
{
    delete m_dialog;
    m_dialog = 0L;
    m_dialogClosed = true;
}

int KFind::index() const
{
    return m_index;
}

void KFind::setPattern( const TQString& pattern )
{
    if ( m_options & KFindDialog::FindIncremental && m_pattern != pattern )
        d->patternChanged = true;

    m_pattern = pattern;
    setOptions( options() ); // rebuild m_regExp if necessary
}

TQWidget* KFind::dialogsParent() const
{
    // If the find dialog is still up, it should get the focus when closing a message box
    // Otherwise, maybe the "find next?" dialog is up
    // Otherwise, the "view" is the parent.
    return d->findDialog ? (TQWidget*)d->findDialog : ( m_dialog ? m_dialog : tqparentWidget() );
}

#include "kfind.moc"