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"
|
|
|
|
#include "GuiView.h"
|
|
|
|
#include "GuiWorkArea.h"
|
2010-01-10 12:37:50 +00:00
|
|
|
#include "qt_helpers.h"
|
2008-11-16 00:15:51 +00:00
|
|
|
|
|
|
|
#include "buffer_funcs.h"
|
|
|
|
#include "BufferParams.h"
|
2010-01-04 12:29:38 +00:00
|
|
|
#include "BufferList.h"
|
2008-11-16 00:15:51 +00:00
|
|
|
#include "Cursor.h"
|
|
|
|
#include "FuncRequest.h"
|
|
|
|
#include "lyxfind.h"
|
|
|
|
#include "output_latex.h"
|
2010-01-10 12:37:50 +00:00
|
|
|
#include "OutputParams.h"
|
2008-11-16 00:15:51 +00:00
|
|
|
#include "TexRow.h"
|
2010-01-10 12:37:50 +00:00
|
|
|
|
|
|
|
#include "frontends/alert.h"
|
2008-11-16 00:15:51 +00:00
|
|
|
|
|
|
|
#include "support/debug.h"
|
2010-01-10 12:37:50 +00:00
|
|
|
#include "support/filetools.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"
|
2010-01-22 17:57:16 +00:00
|
|
|
#include "support/lstrings.h"
|
2008-11-16 00:15:51 +00:00
|
|
|
|
|
|
|
#include <QCloseEvent>
|
2008-11-22 15:21:45 +00:00
|
|
|
#include <QLineEdit>
|
2010-01-25 06:58:54 +00:00
|
|
|
#include <QMenu>
|
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);
|
2009-09-24 14:28:14 +00:00
|
|
|
#if QT_VERSION < 0x040400
|
|
|
|
scrollArea->setWidget(scrollAreaWidgetContents);
|
|
|
|
#endif
|
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();
|
2010-01-25 06:58:54 +00:00
|
|
|
|
|
|
|
QMenu * menu = new QMenu();
|
2010-01-25 07:23:58 +00:00
|
|
|
QAction * regAny = menu->addAction(qt_("&Anything"));
|
2010-01-25 06:58:54 +00:00
|
|
|
regAny->setData(".*");
|
2010-01-25 07:23:58 +00:00
|
|
|
QAction * regAnyNonEmpty = menu->addAction(qt_("Any non-&empty"));
|
2010-01-25 06:58:54 +00:00
|
|
|
regAnyNonEmpty->setData(".+");
|
2010-01-25 07:23:58 +00:00
|
|
|
QAction * regAnyWord = menu->addAction(qt_("Any &word"));
|
2010-01-25 06:58:54 +00:00
|
|
|
regAnyWord->setData("[a-z]+");
|
2010-01-25 07:23:58 +00:00
|
|
|
QAction * regAnyNumber = menu->addAction(qt_("Any &number"));
|
2010-01-25 06:58:54 +00:00
|
|
|
regAnyNumber->setData("[0-9]+");
|
2010-01-25 07:23:58 +00:00
|
|
|
QAction * regCustom = menu->addAction(qt_("&User-defined"));
|
2010-01-25 06:58:54 +00:00
|
|
|
regCustom->setData("");
|
|
|
|
regexpInsertPB->setMenu(menu);
|
|
|
|
|
2010-01-25 08:06:26 +00:00
|
|
|
connect(menu, SIGNAL(triggered(QAction *)),
|
|
|
|
this, SLOT(insertRegexp(QAction *)));
|
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
|
|
|
{
|
2010-01-25 08:06:26 +00:00
|
|
|
if (obj == find_work_area_
|
|
|
|
&& event->type() == QEvent::KeyPress) {
|
2008-11-16 00:15:51 +00:00
|
|
|
QKeyEvent *e = static_cast<QKeyEvent *> (event);
|
2010-01-25 08:06:26 +00:00
|
|
|
if (e->key() == Qt::Key_Escape
|
|
|
|
&& e->modifiers() == Qt::NoModifier) {
|
2010-01-27 18:38:43 +00:00
|
|
|
hideDialog();
|
2008-11-16 00:15:51 +00:00
|
|
|
return true;
|
2010-01-25 08:06:26 +00:00
|
|
|
} else if (e->key() == Qt::Key_Enter
|
|
|
|
|| e->key() == Qt::Key_Return) {
|
2008-11-16 00:15:51 +00:00
|
|
|
if (e->modifiers() == Qt::ShiftModifier) {
|
|
|
|
on_findPrevPB_clicked();
|
|
|
|
return true;
|
|
|
|
} else if (e->modifiers() == Qt::NoModifier) {
|
|
|
|
on_findNextPB_clicked();
|
|
|
|
return true;
|
|
|
|
}
|
2010-01-25 08:06:26 +00:00
|
|
|
} else if (e->key() == Qt::Key_Tab
|
|
|
|
&& e->modifiers() == Qt::NoModifier) {
|
2009-08-21 16:51:07 +00:00
|
|
|
LYXERR(Debug::FIND, "Focusing replace WA");
|
|
|
|
replace_work_area_->setFocus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-01-25 08:06:26 +00:00
|
|
|
if (obj == replace_work_area_
|
|
|
|
&& event->type() == QEvent::KeyPress) {
|
2009-08-21 16:51:07 +00:00
|
|
|
QKeyEvent *e = static_cast<QKeyEvent *> (event);
|
2010-01-25 08:06:26 +00:00
|
|
|
if (e->key() == Qt::Key_Escape
|
|
|
|
&& e->modifiers() == Qt::NoModifier) {
|
2010-01-27 18:38:43 +00:00
|
|
|
hideDialog();
|
2009-08-21 16:51:07 +00:00
|
|
|
return true;
|
2010-01-25 08:06:26 +00:00
|
|
|
} else if (e->key() == Qt::Key_Enter
|
|
|
|
|| e->key() == Qt::Key_Return) {
|
2009-08-21 16:51:07 +00:00
|
|
|
if (e->modifiers() == Qt::ShiftModifier) {
|
|
|
|
on_replacePrevPB_clicked();
|
|
|
|
return true;
|
|
|
|
} else if (e->modifiers() == Qt::NoModifier) {
|
|
|
|
on_replaceNextPB_clicked();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (e->key() == Qt::Key_Backtab) {
|
|
|
|
LYXERR(Debug::FIND, "Focusing find WA");
|
|
|
|
find_work_area_->setFocus();
|
|
|
|
return true;
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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) {
|
2010-01-25 08:06:26 +00:00
|
|
|
TeXOnePar(buffer, buffer.text(),
|
|
|
|
pit, os, buffer.texrow(), runparams);
|
|
|
|
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
|
|
|
|
2010-01-04 12:29:38 +00:00
|
|
|
|
2010-01-09 22:49:35 +00:00
|
|
|
static vector<string> const & allManualsFiles() {
|
|
|
|
static vector<string> v;
|
|
|
|
static const char * files[] = {
|
2010-01-25 08:06:26 +00:00
|
|
|
"Intro", "UserGuide", "Tutorial", "Additional",
|
|
|
|
"EmbeddedObjects", "Math", "Customization", "Shortcuts",
|
|
|
|
"LFUNs", "LaTeXConfig"
|
2010-01-09 22:49:35 +00:00
|
|
|
};
|
|
|
|
if (v.empty()) {
|
|
|
|
FileName fname;
|
|
|
|
for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); ++i) {
|
|
|
|
fname = i18nLibFileSearch("doc", files[i], "lyx");
|
|
|
|
v.push_back(fname.absFilename());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-04 12:29:38 +00:00
|
|
|
/** Switch p_buf to point to next document buffer.
|
|
|
|
**
|
|
|
|
** Return true if restarted from master-document buffer.
|
|
|
|
**
|
|
|
|
** @note
|
|
|
|
** Not using p_buf->allRelatives() here, because I'm not sure
|
|
|
|
** whether or not the returned order is independent of p_buf.
|
|
|
|
**/
|
|
|
|
static bool next_document_buffer(Buffer * & p_buf) {
|
|
|
|
Buffer *p_master = p_buf;
|
|
|
|
Buffer *p_old;
|
|
|
|
do {
|
|
|
|
p_old = p_master;
|
|
|
|
p_master = const_cast<Buffer *>(p_master->masterBuffer());
|
2010-01-25 08:06:26 +00:00
|
|
|
LYXERR(Debug::FIND, "p_old="
|
|
|
|
<< p_old
|
|
|
|
<< ", p_master="
|
|
|
|
<< p_master);
|
2010-01-04 12:29:38 +00:00
|
|
|
} while (p_master != p_old);
|
|
|
|
LASSERT(p_master != NULL, /**/);
|
|
|
|
vector<Buffer *> v_children;
|
|
|
|
/* Root master added as first buffer in the vector */
|
|
|
|
v_children.push_back(p_master);
|
|
|
|
p_master->getChildren(v_children, true);
|
|
|
|
LYXERR(Debug::FIND, "v_children.size()=" << v_children.size());
|
2010-01-25 08:06:26 +00:00
|
|
|
vector<Buffer *>::const_iterator it =
|
|
|
|
find(v_children.begin(), v_children.end(), p_buf);
|
2010-01-04 12:29:38 +00:00
|
|
|
LASSERT(it != v_children.end(), /**/)
|
|
|
|
++it;
|
|
|
|
if (it == v_children.end()) {
|
|
|
|
p_buf = *v_children.begin();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
p_buf = *it;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Switch p_buf to point to previous document buffer.
|
|
|
|
**
|
|
|
|
** Return true if restarted from last child buffer.
|
|
|
|
**
|
|
|
|
** @note
|
|
|
|
** Not using p_buf->allRelatives() here, because I'm not sure
|
|
|
|
** whether or not the returned order is independent of p_buf.
|
|
|
|
**/
|
|
|
|
static bool prev_document_buffer(Buffer * & p_buf) {
|
|
|
|
Buffer *p_master = p_buf;
|
|
|
|
Buffer *p_old;
|
|
|
|
do {
|
|
|
|
p_old = p_master;
|
|
|
|
p_master = const_cast<Buffer *>(p_master->masterBuffer());
|
2010-01-25 08:06:26 +00:00
|
|
|
LYXERR(Debug::FIND,
|
|
|
|
"p_old=" << p_old
|
|
|
|
<< ", p_master=" << p_master);
|
2010-01-04 12:29:38 +00:00
|
|
|
} while (p_master != p_old);
|
|
|
|
LASSERT(p_master != NULL, /**/);
|
|
|
|
vector<Buffer *> v_children;
|
|
|
|
/* Root master added as first buffer in the vector */
|
|
|
|
v_children.push_back(p_master);
|
|
|
|
p_master->getChildren(v_children, true);
|
|
|
|
LYXERR(Debug::FIND, "v_children.size()=" << v_children.size());
|
2010-01-25 08:06:26 +00:00
|
|
|
vector<Buffer *>::const_iterator it =
|
|
|
|
find(v_children.begin(), v_children.end(), p_buf);
|
2010-01-04 12:29:38 +00:00
|
|
|
LASSERT(it != v_children.end(), /**/)
|
|
|
|
if (it == v_children.begin()) {
|
|
|
|
it = v_children.end();
|
|
|
|
--it;
|
|
|
|
p_buf = *it;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
--it;
|
|
|
|
p_buf = *it;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Switch buf to point to next or previous buffer in search scope.
|
|
|
|
**
|
|
|
|
** Return true if restarted from scratch.
|
|
|
|
**/
|
2010-01-25 08:06:26 +00:00
|
|
|
static bool next_prev_buffer(Buffer * & buf,
|
|
|
|
FindAndReplaceOptions const & opt)
|
|
|
|
{
|
2010-01-04 12:29:38 +00:00
|
|
|
bool restarted = false;
|
|
|
|
switch (opt.scope) {
|
|
|
|
case FindAndReplaceOptions::S_BUFFER:
|
|
|
|
restarted = true;
|
|
|
|
break;
|
|
|
|
case FindAndReplaceOptions::S_DOCUMENT:
|
|
|
|
if (opt.forward)
|
|
|
|
restarted = next_document_buffer(buf);
|
|
|
|
else
|
|
|
|
restarted = prev_document_buffer(buf);
|
|
|
|
break;
|
|
|
|
case FindAndReplaceOptions::S_OPEN_BUFFERS:
|
|
|
|
if (opt.forward) {
|
|
|
|
buf = theBufferList().next(buf);
|
|
|
|
restarted = buf == *theBufferList().begin();
|
|
|
|
} else {
|
|
|
|
buf = theBufferList().previous(buf);
|
|
|
|
restarted = buf == *(theBufferList().end() - 1);
|
|
|
|
}
|
|
|
|
break;
|
2010-01-09 22:49:35 +00:00
|
|
|
case FindAndReplaceOptions::S_ALL_MANUALS:
|
|
|
|
vector<string> const & v = allManualsFiles();
|
2010-01-25 08:06:26 +00:00
|
|
|
vector<string>::const_iterator it =
|
|
|
|
find(v.begin(), v.end(), buf->absFileName());
|
2010-01-09 22:49:35 +00:00
|
|
|
if (it == v.end()) {
|
|
|
|
it = v.begin();
|
|
|
|
} else if (opt.forward) {
|
|
|
|
++it;
|
|
|
|
if (it == v.end()) {
|
|
|
|
it = v.begin();
|
|
|
|
restarted = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (it == v.begin()) {
|
|
|
|
it = v.end();
|
|
|
|
restarted = true;
|
|
|
|
}
|
|
|
|
--it;
|
|
|
|
}
|
|
|
|
FileName const & fname = FileName(*it);
|
2010-01-10 23:54:45 +00:00
|
|
|
if (!theBufferList().exists(fname)) {
|
|
|
|
guiApp->currentView()->setBusy(false);
|
2010-01-09 22:49:35 +00:00
|
|
|
guiApp->currentView()->loadDocument(fname, false);
|
2010-01-10 23:54:45 +00:00
|
|
|
guiApp->currentView()->setBusy(true);
|
|
|
|
}
|
2010-01-09 22:49:35 +00:00
|
|
|
buf = theBufferList().getBuffer(fname);
|
|
|
|
break;
|
2010-01-04 12:29:38 +00:00
|
|
|
}
|
|
|
|
return restarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Find the finest question message to post to the user */
|
|
|
|
docstring question_string(FindAndReplaceOptions const & opt)
|
|
|
|
{
|
|
|
|
docstring scope;
|
|
|
|
switch (opt.scope) {
|
|
|
|
case FindAndReplaceOptions::S_BUFFER:
|
2010-01-23 09:15:37 +00:00
|
|
|
scope = _("file[[scope]]");
|
2010-01-04 12:29:38 +00:00
|
|
|
break;
|
|
|
|
case FindAndReplaceOptions::S_DOCUMENT:
|
2010-01-23 09:15:37 +00:00
|
|
|
scope = _("master document[[scope]]");
|
2010-01-04 12:29:38 +00:00
|
|
|
break;
|
|
|
|
case FindAndReplaceOptions::S_OPEN_BUFFERS:
|
2010-01-23 09:15:37 +00:00
|
|
|
scope = _("open files[[scope]]");
|
2010-01-04 12:29:38 +00:00
|
|
|
break;
|
2010-01-09 22:49:35 +00:00
|
|
|
case FindAndReplaceOptions::S_ALL_MANUALS:
|
2010-01-23 09:15:37 +00:00
|
|
|
scope = _("manuals[[scope]]");
|
2010-01-09 22:49:35 +00:00
|
|
|
break;
|
2010-01-04 12:29:38 +00:00
|
|
|
}
|
2010-01-23 09:15:37 +00:00
|
|
|
docstring message = opt.forward ?
|
|
|
|
bformat(_("End of %1$s reached while searching forward.\n"
|
|
|
|
"Continue searching from begin?"),
|
|
|
|
scope) :
|
|
|
|
bformat(_("Beginning of %1$s reached while searching backwards.\n"
|
|
|
|
"Continue searching from end?"),
|
|
|
|
scope);
|
2010-01-22 17:57:16 +00:00
|
|
|
|
2010-01-23 09:15:37 +00:00
|
|
|
return message;
|
2010-01-04 12:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-25 08:06:26 +00:00
|
|
|
void FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt)
|
|
|
|
{
|
2010-01-04 12:29:38 +00:00
|
|
|
int wrap_answer = -1;
|
|
|
|
ostringstream oss;
|
|
|
|
oss << opt;
|
|
|
|
FuncRequest cmd(LFUN_WORD_FINDADV, from_utf8(oss.str()));
|
|
|
|
BufferView * bv = view_.documentBufferView();
|
|
|
|
Buffer * buf = &bv->buffer();
|
|
|
|
|
|
|
|
Buffer * buf_orig = &bv->buffer();
|
|
|
|
Cursor cur_orig(bv->cursor());
|
|
|
|
|
2010-01-09 22:49:35 +00:00
|
|
|
if (opt.scope == FindAndReplaceOptions::S_ALL_MANUALS) {
|
|
|
|
vector<string> const & v = allManualsFiles();
|
|
|
|
if (std::find(v.begin(), v.end(), buf->absFileName()) == v.end()) {
|
|
|
|
FileName const & fname = FileName(*v.begin());
|
2010-01-10 23:54:45 +00:00
|
|
|
if (!theBufferList().exists(fname)) {
|
|
|
|
guiApp->currentView()->setBusy(false);
|
2010-01-09 22:49:35 +00:00
|
|
|
guiApp->currentView()->loadDocument(fname, false);
|
2010-01-10 23:54:45 +00:00
|
|
|
guiApp->currentView()->setBusy(true);
|
|
|
|
}
|
2010-01-09 22:49:35 +00:00
|
|
|
buf = theBufferList().getBuffer(fname);
|
|
|
|
lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
|
|
|
|
buf->absFileName()));
|
|
|
|
bv = view_.documentBufferView();
|
|
|
|
bv->cursor().clear();
|
|
|
|
bv->cursor().push_back(CursorSlice(buf->inset()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-04 12:29:38 +00:00
|
|
|
do {
|
|
|
|
LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
|
|
|
|
dispatch(cmd);
|
|
|
|
if (bv->cursor().result().dispatched()) {
|
|
|
|
// Match found, selected and replaced if needed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-25 08:06:26 +00:00
|
|
|
// No match found in current buffer:
|
|
|
|
// select next buffer in scope, if any
|
2010-01-04 12:29:38 +00:00
|
|
|
bool prompt = next_prev_buffer(buf, opt);
|
|
|
|
if (prompt) {
|
|
|
|
if (wrap_answer != -1)
|
|
|
|
break;
|
|
|
|
docstring q = question_string(opt);
|
|
|
|
wrap_answer = frontend::Alert::prompt(
|
|
|
|
_("Wrap search?"), q,
|
|
|
|
0, 1, _("&Yes"), _("&No"));
|
|
|
|
if (wrap_answer == 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
|
|
|
|
buf->absFileName()));
|
|
|
|
bv = view_.documentBufferView();
|
|
|
|
if (opt.forward) {
|
|
|
|
bv->cursor().clear();
|
|
|
|
bv->cursor().push_back(CursorSlice(buf->inset()));
|
|
|
|
} else {
|
|
|
|
lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
|
|
|
|
bv->cursor().setCursor(doc_iterator_end(buf));
|
|
|
|
bv->cursor().backwardPos();
|
2010-01-25 08:06:26 +00:00
|
|
|
LYXERR(Debug::FIND, "findBackAdv5: cur: "
|
|
|
|
<< bv->cursor());
|
2010-01-04 12:29:38 +00:00
|
|
|
}
|
|
|
|
bv->clearSelection();
|
|
|
|
} while (wrap_answer != 1);
|
2010-01-20 18:18:04 +00:00
|
|
|
if (buf != buf_orig)
|
|
|
|
lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
|
|
|
|
buf_orig->absFileName()));
|
2010-01-04 12:29:38 +00:00
|
|
|
bv = view_.documentBufferView();
|
|
|
|
bv->cursor() = cur_orig;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-14 15:34:56 +00:00
|
|
|
void FindAndReplaceWidget::findAndReplace(
|
|
|
|
bool casesensitive, bool matchword, bool backwards,
|
2009-08-19 23:05:31 +00:00
|
|
|
bool expandmacros, bool ignoreformat, bool replace,
|
|
|
|
bool keep_case)
|
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) {
|
2010-01-25 08:06:26 +00:00
|
|
|
LYXERR(Debug::FIND, "Adding to search string: '"
|
|
|
|
<< it->asString(false)
|
|
|
|
<< "'");
|
|
|
|
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;
|
|
|
|
}
|
2010-01-25 08:06:26 +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) {
|
2010-01-25 08:06:26 +00:00
|
|
|
Buffer & repl_buffer =
|
|
|
|
replace_work_area_->bufferView().buffer();
|
2009-08-19 15:14:28 +00:00
|
|
|
ostringstream oss;
|
|
|
|
repl_buffer.write(oss);
|
2010-01-25 08:06:26 +00:00
|
|
|
//buffer_to_latex(replace_buffer);
|
|
|
|
replaceString = from_utf8(oss.str());
|
2009-01-14 15:34:56 +00:00
|
|
|
} else {
|
|
|
|
replaceString = from_utf8(LYX_FR_NULL_STRING);
|
|
|
|
}
|
2010-01-25 08:06:26 +00:00
|
|
|
FindAndReplaceOptions::SearchScope scope =
|
|
|
|
FindAndReplaceOptions::S_BUFFER;
|
2009-12-30 18:40:18 +00:00
|
|
|
if (CurrentDocument->isChecked())
|
|
|
|
scope = FindAndReplaceOptions::S_BUFFER;
|
|
|
|
else if (MasterDocument->isChecked())
|
|
|
|
scope = FindAndReplaceOptions::S_DOCUMENT;
|
2009-12-30 21:50:55 +00:00
|
|
|
else if (OpenDocuments->isChecked())
|
|
|
|
scope = FindAndReplaceOptions::S_OPEN_BUFFERS;
|
2010-01-09 22:49:35 +00:00
|
|
|
else if (AllManualsRB->isChecked())
|
|
|
|
scope = FindAndReplaceOptions::S_ALL_MANUALS;
|
2009-12-30 18:40:18 +00:00
|
|
|
else
|
|
|
|
LASSERT(false, /**/);
|
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-19 23:05:31 +00:00
|
|
|
<< ", replaceString" << replaceString
|
2009-12-30 18:40:18 +00:00
|
|
|
<< ", keep_case=" << keep_case
|
|
|
|
<< ", scope=" << scope);
|
2010-01-25 08:06:26 +00:00
|
|
|
FindAndReplaceOptions opt(searchString, casesensitive, matchword,
|
|
|
|
!backwards, expandmacros, ignoreformat,
|
|
|
|
regexp, replaceString, keep_case, scope);
|
2010-01-10 23:54:45 +00:00
|
|
|
view_.setBusy(true);
|
2010-01-04 12:29:38 +00:00
|
|
|
findAndReplaceScope(opt);
|
2010-01-10 23:54:45 +00:00
|
|
|
view_.setBusy(false);
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-14 15:34:56 +00:00
|
|
|
void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2009-08-22 16:09:44 +00:00
|
|
|
if (! view_.currentMainWorkArea()) {
|
|
|
|
view_.message(_("No open document(s) in which to search"));
|
|
|
|
return;
|
|
|
|
}
|
2010-01-25 08:06:26 +00:00
|
|
|
// FIXME: create a Dialog::returnFocus()
|
|
|
|
// or something instead of this:
|
2008-12-23 08:18:11 +00:00
|
|
|
view_.setCurrentWorkArea(view_.currentMainWorkArea());
|
2009-01-14 15:34:56 +00:00
|
|
|
findAndReplace(caseCB->isChecked(),
|
|
|
|
wordsCB->isChecked(),
|
|
|
|
backwards,
|
|
|
|
expandMacrosCB->isChecked(),
|
|
|
|
ignoreFormatCB->isChecked(),
|
2009-08-19 23:05:31 +00:00
|
|
|
replace,
|
|
|
|
keepCaseCB->isChecked());
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-25 06:58:54 +00:00
|
|
|
void FindAndReplaceWidget::insertRegexp(QAction * action)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2010-01-25 06:58:54 +00:00
|
|
|
string const regexp = fromqstr(action->data().toString());
|
|
|
|
LYXERR(Debug::FIND, "Regexp: " << regexp);
|
|
|
|
find_work_area_->setFocus();
|
|
|
|
Cursor & cur = find_work_area_->bufferView().cursor();
|
|
|
|
if (!cur.inRegexped())
|
|
|
|
dispatch(FuncRequest(LFUN_REGEXP_MODE));
|
|
|
|
dispatch(FuncRequest(LFUN_SELF_INSERT, regexp));
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-27 18:38:43 +00:00
|
|
|
void FindAndReplaceWidget::hideDialog()
|
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);
|
2009-08-21 16:51:07 +00:00
|
|
|
find_work_area_->setFocus();
|
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);
|
2009-08-21 16:51:07 +00:00
|
|
|
find_work_area_->setFocus();
|
2009-01-14 15:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplaceWidget::on_replaceNextPB_clicked()
|
|
|
|
{
|
|
|
|
findAndReplace(false, true);
|
2009-08-21 16:51:07 +00:00
|
|
|
replace_work_area_->setFocus();
|
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);
|
2009-08-21 16:51:07 +00:00
|
|
|
replace_work_area_->setFocus();
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
void FindAndReplaceWidget::on_replaceallPB_clicked()
|
|
|
|
{
|
2009-08-21 16:51:07 +00:00
|
|
|
replace_work_area_->setFocus();
|
2008-12-23 08:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-12 14:30:29 +00:00
|
|
|
void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
|
2008-12-23 08:18:11 +00:00
|
|
|
{
|
2009-12-17 07:48:29 +00:00
|
|
|
view_.setCurrentWorkArea(find_work_area_);
|
|
|
|
LYXERR(Debug::FIND, "Selecting entire find buffer");
|
2008-12-23 08:18:11 +00:00
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
|
|
|
|
find_work_area_->installEventFilter(this);
|
2009-08-21 16:51:07 +00:00
|
|
|
replace_work_area_->installEventFilter(this);
|
2008-12-23 08:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2009-08-21 16:51:07 +00:00
|
|
|
replace_work_area_->removeEventFilter(this);
|
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
|
|
|
|
2010-01-25 08:06:26 +00:00
|
|
|
bool FindAndReplaceWidget::initialiseParams(std::string const & /*params*/)
|
2008-12-23 08:18:11 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FindAndReplace::FindAndReplace(GuiView & parent,
|
|
|
|
Qt::DockWidgetArea area, Qt::WindowFlags flags)
|
2010-01-25 08:06:26 +00:00
|
|
|
: DockView(parent, "Find LyX", qt_("Advanced Find and Replace"),
|
|
|
|
area, flags)
|
2008-12-23 08:18:11 +00:00
|
|
|
{
|
|
|
|
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"
|