2008-11-16 00:15:51 +00:00
|
|
|
/**
|
|
|
|
* \file FindAndReplace.cpp
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Tommaso Cucinotta
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "FindAndReplace.h"
|
|
|
|
|
2008-11-22 15:21:45 +00:00
|
|
|
#include "GuiApplication.h"
|
2008-11-16 00:15:51 +00:00
|
|
|
#include "qt_helpers.h"
|
2008-11-22 15:21:45 +00:00
|
|
|
#include "GuiView.h"
|
|
|
|
#include "GuiWorkArea.h"
|
2008-11-16 00:15:51 +00:00
|
|
|
|
|
|
|
#include "buffer_funcs.h"
|
|
|
|
#include "BufferParams.h"
|
|
|
|
#include "Cursor.h"
|
|
|
|
#include "FuncRequest.h"
|
|
|
|
#include "lyxfind.h"
|
|
|
|
#include "OutputParams.h"
|
|
|
|
#include "output_latex.h"
|
|
|
|
#include "TexRow.h"
|
|
|
|
|
|
|
|
#include "support/convert.h"
|
|
|
|
#include "support/debug.h"
|
2008-11-22 15:21:45 +00:00
|
|
|
#include "support/FileName.h"
|
2008-11-16 00:15:51 +00:00
|
|
|
#include "support/gettext.h"
|
|
|
|
#include "support/lassert.h"
|
|
|
|
|
|
|
|
#include <QCloseEvent>
|
2008-11-22 15:21:45 +00:00
|
|
|
#include <QLineEdit>
|
2008-11-16 00:15:51 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace lyx::support;
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
|
|
|
FindAndReplace::FindAndReplace(GuiView & parent)
|
2008-11-22 17:54:59 +00:00
|
|
|
: DockView(parent, "Find LyX", "Find LyX Dialog", Qt::RightDockWidgetArea)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
|
|
|
setupUi(this);
|
2008-11-22 17:54:59 +00:00
|
|
|
find_work_area_->setGuiView(parent);
|
2008-11-22 14:45:47 +00:00
|
|
|
find_work_area_->init();
|
2008-11-22 17:35:17 +00:00
|
|
|
setFocusProxy(find_work_area_);
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FindAndReplace::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
{
|
|
|
|
LYXERR(Debug::DEBUG, "FindAndReplace::eventFilter()" << std::endl);
|
|
|
|
if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
|
|
|
|
QKeyEvent *e = static_cast<QKeyEvent *> (event);
|
|
|
|
if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
|
|
|
|
on_closePB_clicked();
|
|
|
|
return true;
|
|
|
|
} else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
|
|
|
|
if (e->modifiers() == Qt::ShiftModifier) {
|
|
|
|
on_findPrevPB_clicked();
|
|
|
|
return true;
|
|
|
|
} else if (e->modifiers() == Qt::NoModifier) {
|
|
|
|
on_findNextPB_clicked();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// standard event processing
|
|
|
|
return QObject::eventFilter(obj, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplace::selectAll()
|
|
|
|
{
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
|
|
|
|
find_work_area_->redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplace::findAdv(bool casesensitive,
|
|
|
|
bool matchword, bool backwards,
|
|
|
|
bool expandmacros, bool ignoreformat)
|
|
|
|
{
|
2008-11-22 14:45:47 +00:00
|
|
|
Buffer & buffer = find_work_area_->bufferView().buffer();
|
2008-11-16 00:15:51 +00:00
|
|
|
docstring searchString;
|
2008-11-22 15:21:45 +00:00
|
|
|
if (!ignoreformat) {
|
2008-11-22 14:45:47 +00:00
|
|
|
OutputParams runparams(&buffer.params().encoding());
|
2008-11-16 00:15:51 +00:00
|
|
|
odocstringstream os;
|
|
|
|
runparams.nice = true;
|
|
|
|
runparams.flavor = OutputParams::LATEX;
|
|
|
|
runparams.linelen = 80; //lyxrc.plaintext_linelen;
|
|
|
|
// No side effect of file copying and image conversion
|
|
|
|
runparams.dryrun = true;
|
2008-11-22 14:45:47 +00:00
|
|
|
buffer.texrow().reset();
|
|
|
|
// latexParagraphs(buffer, buffer.paragraphs(), os, buffer.texrow(), runparams);
|
2008-11-22 15:21:45 +00:00
|
|
|
ParagraphList::const_iterator pit = buffer.paragraphs().begin();
|
|
|
|
ParagraphList::const_iterator const end = buffer.paragraphs().end();
|
|
|
|
for (; pit != end; ++pit) {
|
2008-11-22 14:45:47 +00:00
|
|
|
TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
|
2008-11-22 15:21:45 +00:00
|
|
|
LYXERR0("searchString up to here: " << os.str());
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
searchString = os.str();
|
|
|
|
} else {
|
2008-11-22 15:21:45 +00:00
|
|
|
ParIterator it = buffer.par_iterator_begin();
|
|
|
|
ParIterator end = buffer.par_iterator_end();
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
LYXERR0("Adding to search string: '" << it->asString(false) << "'");
|
2008-11-16 00:15:51 +00:00
|
|
|
searchString += it->asString(AS_STR_INSETS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
|
|
|
|
if (to_utf8(searchString).empty()) {
|
2008-11-22 14:45:47 +00:00
|
|
|
buffer.message(_("Nothing to search"));
|
2008-11-16 00:15:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-11-22 15:21:45 +00:00
|
|
|
bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
|
|
|
|
FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards,
|
|
|
|
expandmacros, ignoreformat, regexp);
|
2008-11-16 00:15:51 +00:00
|
|
|
std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << opt;
|
|
|
|
std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
|
|
|
|
dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
|
|
|
|
|
|
|
|
// findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
|
|
|
|
// searchString, len, casesensitive, matchword, ! backwards, expandmacros);
|
|
|
|
}
|
|
|
|
|
2008-11-22 17:35:17 +00:00
|
|
|
bool FindAndReplace::initialiseParams(std::string const &)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2008-11-22 17:35:17 +00:00
|
|
|
find_work_area_->setFocus();
|
2008-11-16 00:15:51 +00:00
|
|
|
selectAll();
|
2008-11-22 17:35:17 +00:00
|
|
|
return true;
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplace::find(bool backwards)
|
|
|
|
{
|
2008-11-22 17:54:59 +00:00
|
|
|
// FIXME: create a Dialog::returnFocus() or something instead of this:
|
|
|
|
GuiView & gv = const_cast<GuiView &>(lyxview());
|
|
|
|
gv.setCurrentWorkArea(gv.currentMainWorkArea());
|
|
|
|
// FIXME: This should be an LFUN.
|
2008-11-16 00:15:51 +00:00
|
|
|
findAdv(caseCB->isChecked(),
|
|
|
|
wordsCB->isChecked(),
|
|
|
|
backwards,
|
|
|
|
expandMacrosCB->isChecked(),
|
|
|
|
ignoreFormatCB->isChecked());
|
2008-11-22 17:54:59 +00:00
|
|
|
gv.currentMainWorkArea()->redraw();
|
2008-11-16 00:15:51 +00:00
|
|
|
find_work_area_->setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplace::on_regexpInsertCombo_currentIndexChanged(int index)
|
|
|
|
{
|
|
|
|
static char const * regexps[] = {
|
|
|
|
".*", ".+", "[a-z]+", "[0-9]+"
|
|
|
|
};
|
|
|
|
//lyxerr << "Index: " << index << std::endl;
|
|
|
|
if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
|
|
|
|
find_work_area_->setFocus();
|
|
|
|
Cursor & cur = find_work_area_->bufferView().cursor();
|
|
|
|
if (! cur.inRegexped())
|
|
|
|
dispatch(FuncRequest(LFUN_REGEXP_MODE));
|
|
|
|
dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
|
|
|
|
regexpInsertCombo->setCurrentIndex(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-22 15:21:45 +00:00
|
|
|
void FindAndReplace::on_closePB_clicked()
|
|
|
|
{
|
2008-11-22 17:54:59 +00:00
|
|
|
dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplace::on_findNextPB_clicked() {
|
|
|
|
find(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplace::on_findPrevPB_clicked() {
|
|
|
|
find(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplace::on_replacePB_clicked()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplace::on_replaceallPB_clicked()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Dialog * createGuiSearchAdv(GuiView & lv)
|
|
|
|
{
|
|
|
|
return new FindAndReplace(lv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
|
|
|
|
|
|
|
|
|
|
|
#include "moc_FindAndReplace.cpp"
|