Commit 67466d0f authored by con's avatar con

Support regular expressions in basic text editors.

parent 7f4545cb
......@@ -166,14 +166,16 @@ int BaseTextFind::replaceAll(const QString &before, const QString &after,
editCursor.movePosition(QTextCursor::Start);
editCursor.beginEditBlock();
int count = 0;
QTextCursor found;
found = document()->find(before, editCursor, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
QRegExp regexp(before);
regexp.setPatternSyntax((findFlags&IFindSupport::FindRegularExpression) ? QRegExp::RegExp : QRegExp::FixedString);
regexp.setCaseSensitivity((findFlags&IFindSupport::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive);
QTextCursor found = document()->find(regexp, editCursor, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
while (!found.isNull() && inScope(found.selectionStart(), found.selectionEnd())) {
++count;
editCursor.setPosition(found.selectionStart());
editCursor.setPosition(found.selectionEnd(), QTextCursor::KeepAnchor);
editCursor.insertText(after);
found = document()->find(before, editCursor, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
found = document()->find(regexp, editCursor, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
}
editCursor.endEditBlock();
return count;
......@@ -187,17 +189,20 @@ bool BaseTextFind::find(const QString &txt,
setTextCursor(start);
return true;
}
QTextCursor found = document()->find(txt, start, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
QRegExp regexp(txt);
regexp.setPatternSyntax((findFlags&IFindSupport::FindRegularExpression) ? QRegExp::RegExp : QRegExp::FixedString);
regexp.setCaseSensitivity((findFlags&IFindSupport::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive);
QTextCursor found = document()->find(regexp, start, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
if (!m_findScope.isNull()) {
// scoped
if (found.isNull() || !inScope(found.selectionStart(), found.selectionEnd())) {
if ((findFlags&QTextDocument::FindBackward) == 0)
if ((findFlags&IFindSupport::FindBackward) == 0)
start.setPosition(m_findScope.selectionStart());
else
start.setPosition(m_findScope.selectionEnd());
found = document()->find(txt, start, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
found = document()->find(regexp, start, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
if (found.isNull() || !inScope(found.selectionStart(), found.selectionEnd()))
return false;
}
......@@ -205,11 +210,11 @@ bool BaseTextFind::find(const QString &txt,
// entire document
if (found.isNull()) {
if ((findFlags&QTextDocument::FindBackward) == 0)
if ((findFlags&IFindSupport::FindBackward) == 0)
start.movePosition(QTextCursor::Start);
else
start.movePosition(QTextCursor::End);
found = document()->find(txt, start, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
found = document()->find(regexp, start, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
if (found.isNull()) {
return false;
}
......
......@@ -214,6 +214,14 @@ FindToolBar::FindToolBar(FindPlugin *plugin, CurrentDocumentFind *currentDocumen
connect(m_wholeWordAction, SIGNAL(triggered(bool)), this, SLOT(setWholeWord(bool)));
lineEditMenu->addAction(m_wholeWordAction);
m_regularExpressionAction = new QAction(tr("Use Regular Expressions"), this);
m_regularExpressionAction->setCheckable(true);
m_regularExpressionAction->setChecked(false);
cmd = am->registerAction(m_regularExpressionAction, Constants::REGULAR_EXPRESSIONS, globalcontext);
mfind->addAction(cmd, Constants::G_FIND_FLAGS);
connect(m_regularExpressionAction, SIGNAL(triggered(bool)), this, SLOT(setRegularExpressions(bool)));
lineEditMenu->addAction(m_regularExpressionAction);
connect(m_currentDocumentFind, SIGNAL(changed()), this, SLOT(updateActions()));
updateActions();
updateIcons();
......@@ -278,6 +286,7 @@ void FindToolBar::updateActions()
m_replaceAllAction->setEnabled(replaceEnabled);
m_caseSensitiveAction->setEnabled(enabled);
m_wholeWordAction->setEnabled(enabled);
m_regularExpressionAction->setEnabled(enabled);
if (QApplication::clipboard()->supportsFindBuffer())
m_enterFindStringAction->setEnabled(enabled);
bool replaceFocus = m_ui.replaceEdit->hasFocus();
......@@ -447,10 +456,13 @@ void FindToolBar::updateFlagMenus()
{
bool wholeOnly = ((m_findFlags & IFindSupport::FindWholeWords));
bool sensitive = ((m_findFlags & IFindSupport::FindCaseSensitively));
bool regexp = ((m_findFlags & IFindSupport::FindRegularExpression));
if (m_wholeWordAction->isChecked() != wholeOnly)
m_wholeWordAction->setChecked(wholeOnly);
if (m_caseSensitiveAction->isChecked() != sensitive)
m_caseSensitiveAction->setChecked(sensitive);
if (m_regularExpressionAction->isChecked() != regexp)
m_regularExpressionAction->setChecked(regexp);
}
bool FindToolBar::setFocusToCurrentFindSupport()
......@@ -506,6 +518,7 @@ void FindToolBar::writeSettings()
settings->setValue("Backward", QVariant((m_findFlags & IFindSupport::FindBackward) != 0));
settings->setValue("CaseSensitively", QVariant((m_findFlags & IFindSupport::FindCaseSensitively) != 0));
settings->setValue("WholeWords", QVariant((m_findFlags & IFindSupport::FindWholeWords) != 0));
settings->setValue("RegularExpression", QVariant((m_findFlags & IFindSupport::FindRegularExpression) != 0));
settings->endGroup();
settings->endGroup();
}
......@@ -522,6 +535,8 @@ void FindToolBar::readSettings()
flags |= IFindSupport::FindCaseSensitively;
if (settings->value("WholeWords", false).toBool())
flags |= IFindSupport::FindWholeWords;
if (settings->value("RegularExpression", false).toBool())
flags |= IFindSupport::FindRegularExpression;
settings->endGroup();
settings->endGroup();
m_findFlags = flags;
......@@ -555,3 +570,8 @@ void FindToolBar::setWholeWord(bool wholeOnly)
{
setFindFlag(IFindSupport::FindWholeWords, wholeOnly);
}
void FindToolBar::setRegularExpressions(bool regexp)
{
setFindFlag(IFindSupport::FindRegularExpression, regexp);
}
......@@ -78,6 +78,7 @@ private slots:
void setCaseSensitive(bool sensitive);
void setWholeWord(bool wholeOnly);
void setRegularExpressions(bool regexp);
protected:
bool focusNextPrevChild(bool next);
......@@ -110,6 +111,7 @@ private:
QAction *m_replaceAllAction;
QAction *m_caseSensitiveAction;
QAction *m_wholeWordAction;
QAction *m_regularExpressionAction;
QWidget *m_widget;
IFindSupport::FindFlags m_findFlags;
};
......
......@@ -46,6 +46,7 @@ public:
FindBackward = 0x01,
FindCaseSensitively = 0x02,
FindWholeWords = 0x04,
FindRegularExpression = 0x08
};
Q_DECLARE_FLAGS(FindFlags, FindFlag);
......
......@@ -49,6 +49,7 @@ const char * const REPLACE_PREVIOUS = "Find.ReplacePrevious";
const char * const REPLACE_ALL = "Find.ReplaceAll";
const char * const CASE_SENSITIVE = "Find.CaseSensitive";
const char * const WHOLE_WORDS = "Find.WholeWords";
const char * const REGULAR_EXPRESSIONS="Find.RegularExpressions";
const char * const TASK_SEARCH = "Find.Task.Search";
} // namespace Constants
......
......@@ -3446,7 +3446,8 @@ void BaseTextEditor::highlightSearchResults(const QString &txt, Find::IFindSuppo
if (d->m_searchExpr.pattern() == txt)
return;
d->m_searchExpr.setPattern(txt);
d->m_searchExpr.setPatternSyntax(QRegExp::FixedString);
d->m_searchExpr.setPatternSyntax((findFlags & Find::IFindSupport::FindRegularExpression) ?
QRegExp::RegExp : QRegExp::FixedString);
d->m_searchExpr.setCaseSensitivity((findFlags & Find::IFindSupport::FindCaseSensitively) ?
Qt::CaseSensitive : Qt::CaseInsensitive);
d->m_findFlags = findFlags;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment