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);
|
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();
|
2010-03-21 11:50:13 +00:00
|
|
|
find_work_area_->setFrameStyle(QFrame::StyledPanel);
|
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();
|
2010-03-21 11:50:13 +00:00
|
|
|
replace_work_area_->setFrameStyle(QFrame::StyledPanel);
|
2008-11-22 18:18:51 +00:00
|
|
|
// We don't want two cursors blinking.
|
|
|
|
replace_work_area_->stopBlinkingCursor();
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-27 19:17:18 +00:00
|
|
|
bool FindAndReplaceWidget::eventFilter(QObject * obj, QEvent * event)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2010-01-27 19:17:18 +00:00
|
|
|
if (event->type() != QEvent::KeyPress
|
|
|
|
|| (obj != find_work_area_ && obj != replace_work_area_))
|
|
|
|
return QWidget::eventFilter(obj, event);
|
|
|
|
|
|
|
|
QKeyEvent * e = static_cast<QKeyEvent *> (event);
|
|
|
|
switch (e->key()) {
|
|
|
|
case Qt::Key_Escape:
|
|
|
|
if (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-27 19:17:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qt::Key_Enter:
|
2010-03-25 16:57:39 +00:00
|
|
|
case Qt::Key_Return: {
|
|
|
|
// with shift we (temporarily) change search/replace direction
|
|
|
|
bool const searchback = searchbackCB->isChecked();
|
|
|
|
if (e->modifiers() == Qt::ShiftModifier && !searchback)
|
|
|
|
searchbackCB->setChecked(!searchback);
|
|
|
|
|
|
|
|
if (obj == find_work_area_)
|
|
|
|
on_findNextPB_clicked();
|
|
|
|
else
|
|
|
|
on_replacePB_clicked();
|
|
|
|
// back to how it was
|
|
|
|
searchbackCB->setChecked(searchback);
|
|
|
|
return true;
|
2010-01-27 19:17:18 +00:00
|
|
|
break;
|
2010-03-25 16:57:39 +00:00
|
|
|
}
|
2010-01-27 19:17:18 +00:00
|
|
|
case Qt::Key_Tab:
|
|
|
|
if (e->modifiers() == Qt::NoModifier) {
|
|
|
|
if (obj == find_work_area_){
|
|
|
|
LYXERR(Debug::FIND, "Focusing replace WA");
|
|
|
|
replace_work_area_->setFocus();
|
2009-08-21 16:51:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-01-27 19:17:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qt::Key_Backtab:
|
|
|
|
if (obj == replace_work_area_) {
|
2009-08-21 16:51:07 +00:00
|
|
|
LYXERR(Debug::FIND, "Focusing find WA");
|
|
|
|
find_work_area_->setFocus();
|
|
|
|
return true;
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
2010-01-27 19:17:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
|
2010-01-27 19:17:18 +00:00
|
|
|
|
2010-09-29 11:55:10 +00:00
|
|
|
static docstring buffer_to_latex(Buffer & buffer)
|
|
|
|
{
|
2009-01-14 15:34:56 +00:00
|
|
|
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-09-29 11:55:10 +00:00
|
|
|
static vector<string> const & allManualsFiles()
|
|
|
|
{
|
2010-01-09 22:49:35 +00:00
|
|
|
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");
|
2010-04-21 01:19:09 +00:00
|
|
|
v.push_back(fname.absFileName());
|
2010-01-09 22:49:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
**/
|
2010-09-29 13:14:20 +00:00
|
|
|
static bool nextDocumentBuffer(Buffer * & buf)
|
2010-09-29 11:55:10 +00:00
|
|
|
{
|
2010-09-29 13:14:20 +00:00
|
|
|
ListOfBuffers const children = buf->allRelatives();
|
|
|
|
LYXERR(Debug::FIND, "children.size()=" << children.size());
|
2010-09-29 11:55:10 +00:00
|
|
|
ListOfBuffers::const_iterator it =
|
2010-09-29 13:14:20 +00:00
|
|
|
find(children.begin(), children.end(), buf);
|
|
|
|
LASSERT(it != children.end(), /**/)
|
2010-01-04 12:29:38 +00:00
|
|
|
++it;
|
2010-09-29 13:14:20 +00:00
|
|
|
if (it == children.end()) {
|
|
|
|
buf = *children.begin();
|
2010-01-04 12:29:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-09-29 13:14:20 +00:00
|
|
|
buf = *it;
|
2010-01-04 12:29:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Switch p_buf to point to previous document buffer.
|
|
|
|
**
|
|
|
|
** Return true if restarted from last child buffer.
|
|
|
|
**/
|
2010-09-29 13:14:20 +00:00
|
|
|
static bool prevDocumentBuffer(Buffer * & buf)
|
2010-09-29 11:55:10 +00:00
|
|
|
{
|
2010-09-29 13:14:20 +00:00
|
|
|
ListOfBuffers const children = buf->allRelatives();
|
|
|
|
LYXERR(Debug::FIND, "children.size()=" << children.size());
|
2010-09-29 11:55:10 +00:00
|
|
|
ListOfBuffers::const_iterator it =
|
2010-09-29 13:14:20 +00:00
|
|
|
find(children.begin(), children.end(), buf);
|
|
|
|
LASSERT(it != children.end(), /**/)
|
|
|
|
if (it == children.begin()) {
|
|
|
|
it = children.end();
|
2010-01-04 12:29:38 +00:00
|
|
|
--it;
|
2010-09-29 13:14:20 +00:00
|
|
|
buf = *it;
|
2010-01-04 12:29:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
--it;
|
2010-09-29 13:14:20 +00:00
|
|
|
buf = *it;
|
2010-01-04 12:29:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Switch buf to point to next or previous buffer in search scope.
|
|
|
|
**
|
|
|
|
** Return true if restarted from scratch.
|
|
|
|
**/
|
2010-09-29 13:14:20 +00:00
|
|
|
static bool nextPrevBuffer(Buffer * & buf,
|
2010-01-25 08:06:26 +00:00
|
|
|
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)
|
2010-09-29 13:14:20 +00:00
|
|
|
restarted = nextDocumentBuffer(buf);
|
2010-01-04 12:29:38 +00:00
|
|
|
else
|
2010-09-29 13:14:20 +00:00
|
|
|
restarted = prevDocumentBuffer(buf);
|
2010-01-04 12:29:38 +00:00
|
|
|
break;
|
|
|
|
case FindAndReplaceOptions::S_OPEN_BUFFERS:
|
|
|
|
if (opt.forward) {
|
|
|
|
buf = theBufferList().next(buf);
|
2010-09-29 13:14:20 +00:00
|
|
|
restarted = (buf == *theBufferList().begin());
|
2010-01-04 12:29:38 +00:00
|
|
|
} else {
|
|
|
|
buf = theBufferList().previous(buf);
|
2010-09-29 13:14:20 +00:00
|
|
|
restarted = (buf == *(theBufferList().end() - 1));
|
2010-01-04 12:29:38 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-01-09 22:49:35 +00:00
|
|
|
case FindAndReplaceOptions::S_ALL_MANUALS:
|
2010-09-29 13:14:20 +00:00
|
|
|
vector<string> const & manuals = allManualsFiles();
|
2010-01-25 08:06:26 +00:00
|
|
|
vector<string>::const_iterator it =
|
2010-09-29 13:14:20 +00:00
|
|
|
find(manuals.begin(), manuals.end(), buf->absFileName());
|
|
|
|
if (it == manuals.end())
|
|
|
|
it = manuals.begin();
|
|
|
|
else if (opt.forward) {
|
2010-01-09 22:49:35 +00:00
|
|
|
++it;
|
2010-09-29 13:14:20 +00:00
|
|
|
if (it == manuals.end()) {
|
|
|
|
it = manuals.begin();
|
2010-01-09 22:49:35 +00:00
|
|
|
restarted = true;
|
|
|
|
}
|
|
|
|
} else {
|
2010-09-29 13:14:20 +00:00
|
|
|
if (it == manuals.begin()) {
|
|
|
|
it = manuals.end();
|
2010-01-09 22:49:35 +00:00
|
|
|
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 */
|
2010-09-29 13:14:20 +00:00
|
|
|
docstring getQuestionString(FindAndReplaceOptions const & opt)
|
2010-01-04 12:29:38 +00:00
|
|
|
{
|
|
|
|
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"
|
2010-01-29 08:59:09 +00:00
|
|
|
"Continue searching from the beginning?"),
|
2010-01-23 09:15:37 +00:00
|
|
|
scope) :
|
2010-01-29 08:59:09 +00:00
|
|
|
bformat(_("Beginning of %1$s reached while searching backward.\n"
|
|
|
|
"Continue searching from the end?"),
|
2010-01-23 09:15:37 +00:00
|
|
|
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();
|
2010-03-20 13:59:46 +00:00
|
|
|
DocIterator cur_orig(bv->cursor());
|
2010-01-04 12:29:38 +00:00
|
|
|
|
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);
|
2010-03-20 13:59:46 +00:00
|
|
|
LYXERR(Debug::FIND, "dispatched");
|
2010-01-04 12:29:38 +00:00
|
|
|
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-09-29 13:14:20 +00:00
|
|
|
bool prompt = nextPrevBuffer(buf, opt);
|
2010-01-04 12:29:38 +00:00
|
|
|
if (prompt) {
|
|
|
|
if (wrap_answer != -1)
|
|
|
|
break;
|
2010-09-29 13:14:20 +00:00
|
|
|
docstring q = getQuestionString(opt);
|
2010-01-04 12:29:38 +00:00
|
|
|
wrap_answer = frontend::Alert::prompt(
|
|
|
|
_("Wrap search?"), q,
|
|
|
|
0, 1, _("&Yes"), _("&No"));
|
|
|
|
if (wrap_answer == 1)
|
|
|
|
break;
|
|
|
|
}
|
2010-03-20 13:59:46 +00:00
|
|
|
if (buf != &view_.documentBufferView()->buffer())
|
|
|
|
lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
|
|
|
|
buf->absFileName()));
|
2010-01-04 12:29:38 +00:00
|
|
|
bv = view_.documentBufferView();
|
|
|
|
if (opt.forward) {
|
|
|
|
bv->cursor().clear();
|
|
|
|
bv->cursor().push_back(CursorSlice(buf->inset()));
|
|
|
|
} else {
|
2010-03-20 13:59:46 +00:00
|
|
|
//lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
|
2010-01-04 12:29:38 +00:00
|
|
|
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-03-20 13:59:46 +00:00
|
|
|
if (buf_orig != &view_.documentBufferView()->buffer())
|
2010-01-20 18:18:04 +00:00
|
|
|
lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
|
|
|
|
buf_orig->absFileName()));
|
2010-01-04 12:29:38 +00:00
|
|
|
bv = view_.documentBufferView();
|
2010-03-20 13:59:46 +00:00
|
|
|
bv->cursor().setCursor(cur_orig);
|
2010-01-04 12:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 16:57:39 +00:00
|
|
|
void FindAndReplaceWidget::on_findNextPB_clicked()
|
2009-01-14 15:34:56 +00:00
|
|
|
{
|
2010-03-25 16:57:39 +00:00
|
|
|
findAndReplace(searchbackCB->isChecked(), false);
|
|
|
|
find_work_area_->setFocus();
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 16:57:39 +00:00
|
|
|
void FindAndReplaceWidget::on_replacePB_clicked()
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2010-03-25 16:57:39 +00:00
|
|
|
findAndReplace(searchbackCB->isChecked(), 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)
|
|
|
|
{
|
2010-03-11 19:33:08 +00:00
|
|
|
FindAndReplace * gui = new FindAndReplace(lv, Qt::RightDockWidgetArea);
|
|
|
|
#ifdef Q_WS_MACX
|
|
|
|
// On Mac show and floating
|
|
|
|
gui->setFloating(true);
|
|
|
|
#endif
|
|
|
|
return gui;
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
|
|
|
|
|
|
|
|
|
|
|
#include "moc_FindAndReplace.cpp"
|