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/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 {
|
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
|
|
|
|
FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
|
|
|
|
: view_(view)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
|
|
|
setupUi(this);
|
2008-12-23 08:18:11 +00:00
|
|
|
find_work_area_->setGuiView(view_);
|
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-12-23 08:18:11 +00:00
|
|
|
replace_work_area_->setGuiView(view_);
|
2008-11-22 18:18:51 +00:00
|
|
|
replace_work_area_->init();
|
|
|
|
// We don't want two cursors blinking.
|
|
|
|
replace_work_area_->stopBlinkingCursor();
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2009-08-17 14:39:00 +00:00
|
|
|
LYXERR(Debug::FIND, "FindAndReplace::eventFilter()");
|
2008-11-16 00:15:51 +00:00
|
|
|
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
|
2008-12-23 08:18:11 +00:00
|
|
|
return QWidget::eventFilter(obj, event);
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
2009-01-14 15:34:56 +00:00
|
|
|
static docstring buffer_to_latex(Buffer & buffer) {
|
|
|
|
OutputParams runparams(&buffer.params().encoding());
|
|
|
|
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;
|
|
|
|
buffer.texrow().reset();
|
|
|
|
ParagraphList::const_iterator pit = buffer.paragraphs().begin();
|
|
|
|
ParagraphList::const_iterator const end = buffer.paragraphs().end();
|
|
|
|
for (; pit != end; ++pit) {
|
|
|
|
TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
|
2009-08-17 14:39:00 +00:00
|
|
|
LYXERR(Debug::FIND, "searchString up to here: " << os.str());
|
2009-01-14 15:34:56 +00:00
|
|
|
}
|
|
|
|
return os.str();
|
|
|
|
}
|
2008-11-16 00:15:51 +00:00
|
|
|
|
2009-01-14 15:34:56 +00:00
|
|
|
void FindAndReplaceWidget::findAndReplace(
|
|
|
|
bool casesensitive, bool matchword, bool backwards,
|
|
|
|
bool expandmacros, bool ignoreformat, bool replace)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
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) {
|
2009-01-14 15:34:56 +00:00
|
|
|
searchString = buffer_to_latex(buffer);
|
2008-11-16 00:15:51 +00:00
|
|
|
} else {
|
2008-11-22 15:21:45 +00:00
|
|
|
ParIterator it = buffer.par_iterator_begin();
|
|
|
|
ParIterator end = buffer.par_iterator_end();
|
2008-12-20 16:00:47 +00:00
|
|
|
OutputParams runparams(&buffer.params().encoding());
|
|
|
|
odocstringstream os;
|
|
|
|
runparams.nice = true;
|
|
|
|
runparams.flavor = OutputParams::LATEX;
|
|
|
|
runparams.linelen = 100000; //lyxrc.plaintext_linelen;
|
|
|
|
runparams.dryrun = true;
|
2008-11-22 15:21:45 +00:00
|
|
|
for (; it != end; ++it) {
|
2009-08-17 14:39:00 +00:00
|
|
|
LYXERR(Debug::FIND, "Adding to search string: '" << it->asString(false) << "'");
|
2008-12-20 16:00:47 +00:00
|
|
|
searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2009-01-14 15:34:56 +00:00
|
|
|
docstring replaceString;
|
|
|
|
if (replace) {
|
2009-08-19 15:14:28 +00:00
|
|
|
Buffer & repl_buffer = replace_work_area_->bufferView().buffer();
|
|
|
|
ostringstream oss;
|
|
|
|
repl_buffer.write(oss);
|
|
|
|
replaceString = from_utf8(oss.str()); //buffer_to_latex(replace_buffer);
|
2009-01-14 15:34:56 +00:00
|
|
|
} else {
|
|
|
|
replaceString = from_utf8(LYX_FR_NULL_STRING);
|
|
|
|
}
|
2009-08-17 14:39:00 +00:00
|
|
|
LYXERR(Debug::FIND, "FindAndReplaceOptions: "
|
2009-08-15 15:35:14 +00:00
|
|
|
<< "searchstring=" << searchString
|
|
|
|
<< ", casesensitiv=" << casesensitive
|
|
|
|
<< ", matchword=" << matchword
|
|
|
|
<< ", backwards=" << backwards
|
|
|
|
<< ", expandmacros=" << expandmacros
|
|
|
|
<< ", ignoreformat=" << ignoreformat
|
|
|
|
<< ", regexp=" << regexp
|
2009-08-17 14:39:00 +00:00
|
|
|
<< ", replaceString" << replaceString);
|
2009-01-14 15:34:56 +00:00
|
|
|
FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
|
|
|
|
expandmacros, ignoreformat, regexp, replaceString);
|
2009-08-17 14:39:00 +00:00
|
|
|
LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
|
2008-11-16 00:15:51 +00:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << opt;
|
2009-08-17 14:39:00 +00:00
|
|
|
LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
|
2008-11-16 00:15:51 +00:00
|
|
|
dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
|
|
|
|
|
|
|
|
// findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
|
|
|
|
// searchString, len, casesensitive, matchword, ! backwards, expandmacros);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-14 15:34:56 +00:00
|
|
|
void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2008-11-22 17:54:59 +00:00
|
|
|
// FIXME: create a Dialog::returnFocus() or something instead of this:
|
2008-12-23 08:18:11 +00:00
|
|
|
view_.setCurrentWorkArea(view_.currentMainWorkArea());
|
2008-11-22 17:54:59 +00:00
|
|
|
// FIXME: This should be an LFUN.
|
2009-01-14 15:34:56 +00:00
|
|
|
findAndReplace(caseCB->isChecked(),
|
|
|
|
wordsCB->isChecked(),
|
|
|
|
backwards,
|
|
|
|
expandMacrosCB->isChecked(),
|
|
|
|
ignoreFormatCB->isChecked(),
|
|
|
|
replace);
|
2008-12-23 08:18:11 +00:00
|
|
|
view_.currentMainWorkArea()->redraw();
|
2008-11-16 00:15:51 +00:00
|
|
|
find_work_area_->setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
|
|
|
static char const * regexps[] = {
|
|
|
|
".*", ".+", "[a-z]+", "[0-9]+"
|
|
|
|
};
|
2009-08-17 14:39:00 +00:00
|
|
|
LYXERR(Debug::FIND, "Index: " << index);
|
2008-11-16 00:15:51 +00:00
|
|
|
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-12-23 08:18:11 +00:00
|
|
|
void FindAndReplaceWidget::on_closePB_clicked()
|
2008-11-22 15:21:45 +00:00
|
|
|
{
|
2008-11-22 17:54:59 +00:00
|
|
|
dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
void FindAndReplaceWidget::on_findNextPB_clicked() {
|
2009-01-14 15:34:56 +00:00
|
|
|
findAndReplace(false, false);
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
void FindAndReplaceWidget::on_findPrevPB_clicked() {
|
2009-01-14 15:34:56 +00:00
|
|
|
findAndReplace(true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplaceWidget::on_replaceNextPB_clicked()
|
|
|
|
{
|
|
|
|
findAndReplace(false, true);
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-14 15:34:56 +00:00
|
|
|
void FindAndReplaceWidget::on_replacePrevPB_clicked()
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2009-01-14 15:34:56 +00:00
|
|
|
findAndReplace(true, true);
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
void FindAndReplaceWidget::on_replaceallPB_clicked()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-12 14:30:29 +00:00
|
|
|
void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
|
2008-12-23 08:18:11 +00:00
|
|
|
{
|
|
|
|
replace_work_area_->setEnabled(true);
|
|
|
|
replace_work_area_->redraw();
|
|
|
|
find_work_area_->setFocus();
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
|
|
|
|
find_work_area_->redraw();
|
|
|
|
find_work_area_->installEventFilter(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2008-12-23 08:18:11 +00:00
|
|
|
find_work_area_->removeEventFilter(this);
|
|
|
|
this->QWidget::hideEvent(ev);
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
|
2009-02-12 14:30:29 +00:00
|
|
|
bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
|
2008-12-23 08:18:11 +00:00
|
|
|
{
|
|
|
|
find_work_area_->redraw();
|
|
|
|
replace_work_area_->setEnabled(true);
|
|
|
|
replace_work_area_->redraw();
|
|
|
|
find_work_area_->setFocus();
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FindAndReplace::FindAndReplace(GuiView & parent,
|
|
|
|
Qt::DockWidgetArea area, Qt::WindowFlags flags)
|
|
|
|
: DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
|
|
|
|
{
|
|
|
|
widget_ = new FindAndReplaceWidget(parent);
|
|
|
|
setWidget(widget_);
|
|
|
|
setFocusProxy(widget_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FindAndReplace::~FindAndReplace()
|
|
|
|
{
|
|
|
|
setFocusProxy(0);
|
|
|
|
delete widget_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FindAndReplace::initialiseParams(std::string const & params)
|
|
|
|
{
|
|
|
|
return widget_->initialiseParams(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-16 00:15:51 +00:00
|
|
|
Dialog * createGuiSearchAdv(GuiView & lv)
|
|
|
|
{
|
2008-12-23 08:18:11 +00:00
|
|
|
return new FindAndReplace(lv, Qt::RightDockWidgetArea);
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
|
|
|
|
|
|
|
|
|
|
|
#include "moc_FindAndReplace.cpp"
|