You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
329 lines
10 KiB
329 lines
10 KiB
15 years ago
|
/*
|
||
|
Copyright (C) 2001, S.R.Haque <srhaque@iee.org>.
|
||
|
Copyright (C) 2002, David Faure <david@mandrakesoft.com>
|
||
|
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.
|
||
|
*/
|
||
|
|
||
15 years ago
|
#include <tqlabel.h>
|
||
15 years ago
|
#include <kapplication.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
#include <klocale.h>
|
||
|
#include <kmessagebox.h>
|
||
|
#include "kreplace.h"
|
||
|
#include "kreplacedialog.h"
|
||
15 years ago
|
#include <tqregexp.h>
|
||
15 years ago
|
|
||
|
//#define DEBUG_REPLACE
|
||
|
#define INDEX_NOMATCH -1
|
||
|
|
||
|
class KReplaceNextDialog : public KDialogBase
|
||
|
{
|
||
|
public:
|
||
15 years ago
|
KReplaceNextDialog( TQWidget *parent );
|
||
14 years ago
|
void setLabel( const TQString& pattern, const TQString& replacement );
|
||
15 years ago
|
private:
|
||
15 years ago
|
TQLabel* m_mainLabel;
|
||
15 years ago
|
};
|
||
|
|
||
15 years ago
|
KReplaceNextDialog::KReplaceNextDialog(TQWidget *parent) :
|
||
15 years ago
|
KDialogBase(parent, 0, false, // non-modal!
|
||
|
i18n("Replace"),
|
||
|
User3 | User2 | User1 | Close,
|
||
|
User3,
|
||
|
false,
|
||
|
i18n("&All"), i18n("&Skip"), i18n("Replace"))
|
||
|
{
|
||
15 years ago
|
m_mainLabel = new TQLabel( this );
|
||
15 years ago
|
setMainWidget( m_mainLabel );
|
||
13 years ago
|
resize(minimumSize());
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void KReplaceNextDialog::setLabel( const TQString& pattern, const TQString& replacement )
|
||
15 years ago
|
{
|
||
14 years ago
|
m_mainLabel->setText( i18n("Replace '%1' with '%2'?").arg(pattern).arg(replacement) );
|
||
15 years ago
|
}
|
||
|
|
||
|
////
|
||
|
|
||
14 years ago
|
KReplace::KReplace(const TQString &pattern, const TQString &replacement, long options, TQWidget *parent) :
|
||
15 years ago
|
KFind( pattern, options, parent )
|
||
|
{
|
||
14 years ago
|
m_replacements = 0;
|
||
|
m_replacement = replacement;
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
KReplace::KReplace(const TQString &pattern, const TQString &replacement, long options, TQWidget *parent, TQWidget *dlg) :
|
||
15 years ago
|
KFind( pattern, options, parent, dlg )
|
||
|
{
|
||
14 years ago
|
m_replacements = 0;
|
||
|
m_replacement = replacement;
|
||
15 years ago
|
}
|
||
|
|
||
|
KReplace::~KReplace()
|
||
|
{
|
||
|
// KFind::~KFind will delete m_dialog
|
||
|
}
|
||
|
|
||
14 years ago
|
KDialogBase* KReplace::replaceNextDialog( bool create )
|
||
15 years ago
|
{
|
||
|
if ( m_dialog || create )
|
||
|
return dialog();
|
||
|
return 0L;
|
||
|
}
|
||
|
|
||
|
KReplaceNextDialog* KReplace::dialog()
|
||
|
{
|
||
|
if ( !m_dialog )
|
||
|
{
|
||
14 years ago
|
m_dialog = new KReplaceNextDialog( parentWidget() );
|
||
15 years ago
|
connect( m_dialog, TQT_SIGNAL( user1Clicked() ), this, TQT_SLOT( slotReplaceAll() ) );
|
||
|
connect( m_dialog, TQT_SIGNAL( user2Clicked() ), this, TQT_SLOT( slotSkip() ) );
|
||
|
connect( m_dialog, TQT_SIGNAL( user3Clicked() ), this, TQT_SLOT( slotReplace() ) );
|
||
|
connect( m_dialog, TQT_SIGNAL( finished() ), this, TQT_SLOT( slotDialogClosed() ) );
|
||
15 years ago
|
}
|
||
|
return static_cast<KReplaceNextDialog *>(m_dialog);
|
||
|
}
|
||
|
|
||
|
void KReplace::displayFinalDialog() const
|
||
|
{
|
||
14 years ago
|
if ( !m_replacements )
|
||
14 years ago
|
KMessageBox::information(parentWidget(), i18n("No text was replaced."));
|
||
15 years ago
|
else
|
||
14 years ago
|
KMessageBox::information(parentWidget(), i18n("1 replacement done.", "%n replacements done.", m_replacements ) );
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
KFind::Result KReplace::replace()
|
||
15 years ago
|
{
|
||
|
#ifdef DEBUG_REPLACE
|
||
|
kdDebug() << k_funcinfo << "m_index=" << m_index << endl;
|
||
|
#endif
|
||
|
if ( m_index == INDEX_NOMATCH && m_lastResult == Match )
|
||
|
{
|
||
|
m_lastResult = NoMatch;
|
||
|
return NoMatch;
|
||
|
}
|
||
|
|
||
|
do // this loop is only because validateMatch can fail
|
||
|
{
|
||
|
#ifdef DEBUG_REPLACE
|
||
|
kdDebug() << k_funcinfo << "beginning of loop: m_index=" << m_index << endl;
|
||
|
#endif
|
||
|
// Find the next match.
|
||
|
if ( m_options & KReplaceDialog::RegularExpression )
|
||
14 years ago
|
m_index = KFind::find(m_text, *m_regExp, m_index, m_options, &m_matchedLength);
|
||
15 years ago
|
else
|
||
14 years ago
|
m_index = KFind::find(m_text, m_pattern, m_index, m_options, &m_matchedLength);
|
||
15 years ago
|
#ifdef DEBUG_REPLACE
|
||
14 years ago
|
kdDebug() << k_funcinfo << "KFind::find returned m_index=" << m_index << endl;
|
||
15 years ago
|
#endif
|
||
|
if ( m_index != -1 )
|
||
|
{
|
||
|
// Flexibility: the app can add more rules to validate a possible match
|
||
|
if ( validateMatch( m_text, m_index, m_matchedLength ) )
|
||
|
{
|
||
|
if ( m_options & KReplaceDialog::PromptOnReplace )
|
||
|
{
|
||
|
#ifdef DEBUG_REPLACE
|
||
|
kdDebug() << k_funcinfo << "PromptOnReplace" << endl;
|
||
|
#endif
|
||
14 years ago
|
// Display accurate initial string and replacement string, they can vary
|
||
15 years ago
|
TQString matchedText = m_text.mid( m_index, m_matchedLength );
|
||
|
TQString rep = matchedText;
|
||
14 years ago
|
KReplace::replace(rep, m_replacement, 0, m_options, m_matchedLength);
|
||
15 years ago
|
dialog()->setLabel( matchedText, rep );
|
||
|
dialog()->show();
|
||
|
|
||
|
// Tell the world about the match we found, in case someone wants to
|
||
|
// highlight it.
|
||
|
emit highlight(m_text, m_index, m_matchedLength);
|
||
|
|
||
|
m_lastResult = Match;
|
||
|
return Match;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
doReplace(); // this moves on too
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// not validated -> move on
|
||
|
if (m_options & KFindDialog::FindBackwards)
|
||
|
m_index--;
|
||
|
else
|
||
|
m_index++;
|
||
|
}
|
||
|
} else
|
||
|
m_index = INDEX_NOMATCH; // will exit the loop
|
||
|
}
|
||
|
while (m_index != INDEX_NOMATCH);
|
||
|
|
||
|
m_lastResult = NoMatch;
|
||
|
return NoMatch;
|
||
|
}
|
||
|
|
||
14 years ago
|
int KReplace::replace(TQString &text, const TQString &pattern, const TQString &replacement, int index, long options, int *replacedLength)
|
||
15 years ago
|
{
|
||
|
int matchedLength;
|
||
|
|
||
14 years ago
|
index = KFind::find(text, pattern, index, options, &matchedLength);
|
||
15 years ago
|
if (index != -1)
|
||
|
{
|
||
14 years ago
|
*replacedLength = replace(text, replacement, index, options, matchedLength);
|
||
15 years ago
|
if (options & KReplaceDialog::FindBackwards)
|
||
|
index--;
|
||
|
else
|
||
14 years ago
|
index += *replacedLength;
|
||
15 years ago
|
}
|
||
|
return index;
|
||
|
}
|
||
|
|
||
14 years ago
|
int KReplace::replace(TQString &text, const TQRegExp &pattern, const TQString &replacement, int index, long options, int *replacedLength)
|
||
15 years ago
|
{
|
||
|
int matchedLength;
|
||
|
|
||
14 years ago
|
index = KFind::find(text, pattern, index, options, &matchedLength);
|
||
15 years ago
|
if (index != -1)
|
||
|
{
|
||
14 years ago
|
*replacedLength = replace(text, replacement, index, options, matchedLength);
|
||
15 years ago
|
if (options & KReplaceDialog::FindBackwards)
|
||
|
index--;
|
||
|
else
|
||
14 years ago
|
index += *replacedLength;
|
||
15 years ago
|
}
|
||
|
return index;
|
||
|
}
|
||
|
|
||
14 years ago
|
int KReplace::replace(TQString &text, const TQString &replacement, int index, long options, int length)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString rep = replacement;
|
||
|
// Backreferences: replace \0 with the right portion of 'text'
|
||
15 years ago
|
if ( options & KReplaceDialog::BackReference )
|
||
14 years ago
|
rep.replace( "\\0", text.mid( index, length ) );
|
||
|
// Then replace rep into the text
|
||
|
text.replace(index, length, rep);
|
||
15 years ago
|
return rep.length();
|
||
|
}
|
||
|
|
||
|
void KReplace::slotReplaceAll()
|
||
|
{
|
||
|
doReplace();
|
||
|
m_options &= ~KReplaceDialog::PromptOnReplace;
|
||
|
emit optionsChanged();
|
||
14 years ago
|
emit findNext();
|
||
15 years ago
|
}
|
||
|
|
||
|
void KReplace::slotSkip()
|
||
|
{
|
||
|
if (m_options & KReplaceDialog::FindBackwards)
|
||
|
m_index--;
|
||
|
else
|
||
|
m_index++;
|
||
|
if ( m_dialogClosed ) {
|
||
|
delete m_dialog; // hide it again
|
||
|
m_dialog = 0L;
|
||
|
} else
|
||
14 years ago
|
emit findNext();
|
||
15 years ago
|
}
|
||
|
|
||
|
void KReplace::slotReplace()
|
||
|
{
|
||
|
doReplace();
|
||
|
if ( m_dialogClosed ) {
|
||
|
delete m_dialog; // hide it again
|
||
|
m_dialog = 0L;
|
||
|
} else
|
||
14 years ago
|
emit findNext();
|
||
15 years ago
|
}
|
||
|
|
||
|
void KReplace::doReplace()
|
||
|
{
|
||
14 years ago
|
int replacedLength = KReplace::replace(m_text, m_replacement, m_index, m_options, m_matchedLength);
|
||
15 years ago
|
|
||
14 years ago
|
// Tell the world about the replacement we made, in case someone wants to
|
||
15 years ago
|
// highlight it.
|
||
14 years ago
|
emit replace(m_text, m_index, replacedLength, m_matchedLength);
|
||
15 years ago
|
#ifdef DEBUG_REPLACE
|
||
14 years ago
|
kdDebug() << k_funcinfo << "after replace() signal: m_index=" << m_index << " replacedLength=" << replacedLength << endl;
|
||
15 years ago
|
#endif
|
||
14 years ago
|
m_replacements++;
|
||
15 years ago
|
if (m_options & KReplaceDialog::FindBackwards)
|
||
|
m_index--;
|
||
|
else {
|
||
14 years ago
|
m_index += replacedLength;
|
||
15 years ago
|
// when replacing the empty pattern, move on. See also kjs/regexp.cpp for how this should be done for regexps.
|
||
|
if ( m_pattern.isEmpty() )
|
||
|
++m_index;
|
||
|
}
|
||
|
#ifdef DEBUG_REPLACE
|
||
|
kdDebug() << k_funcinfo << "after adjustement: m_index=" << m_index << endl;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
void KReplace::resetCounts()
|
||
|
{
|
||
|
KFind::resetCounts();
|
||
14 years ago
|
m_replacements = 0;
|
||
15 years ago
|
}
|
||
|
|
||
|
bool KReplace::shouldRestart( bool forceAsking, bool showNumMatches ) const
|
||
|
{
|
||
14 years ago
|
// Only ask if we did a "find from cursor", otherwise it's pointless.
|
||
|
// ... Or if the prompt-on-replace option was set.
|
||
15 years ago
|
// Well, unless the user can modify the document during a search operation,
|
||
|
// hence the force boolean.
|
||
|
if ( !forceAsking && (m_options & KFindDialog::FromCursor) == 0
|
||
|
&& (m_options & KReplaceDialog::PromptOnReplace) == 0 )
|
||
|
{
|
||
|
displayFinalDialog();
|
||
|
return false;
|
||
|
}
|
||
15 years ago
|
TQString message;
|
||
15 years ago
|
if ( showNumMatches )
|
||
|
{
|
||
14 years ago
|
if ( !m_replacements )
|
||
|
message = i18n("No text was replaced.");
|
||
15 years ago
|
else
|
||
14 years ago
|
message = i18n("1 replacement done.", "%n replacements done.", m_replacements );
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
if ( m_options & KFindDialog::FindBackwards )
|
||
|
message = i18n( "Beginning of document reached." );
|
||
|
else
|
||
|
message = i18n( "End of document reached." );
|
||
|
}
|
||
|
|
||
|
message += "\n";
|
||
|
// Hope this word puzzle is ok, it's a different sentence
|
||
|
message +=
|
||
|
( m_options & KFindDialog::FindBackwards ) ?
|
||
|
i18n("Do you want to restart search from the end?")
|
||
|
: i18n("Do you want to restart search at the beginning?");
|
||
|
|
||
14 years ago
|
int ret = KMessageBox::questionYesNo( parentWidget(), message, TQString::null, i18n("Restart"), i18n("Stop") );
|
||
15 years ago
|
return( ret == KMessageBox::Yes );
|
||
|
}
|
||
|
|
||
|
void KReplace::closeReplaceNextDialog()
|
||
|
{
|
||
|
closeFindNextDialog();
|
||
|
}
|
||
|
|
||
|
#include "kreplace.moc"
|