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"
|
|
|
|
|
2010-12-31 14:35:16 +00:00
|
|
|
#include "Lexer.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"
|
2010-12-31 15:07:03 +00:00
|
|
|
#include "Language.h"
|
2008-11-16 00:15:51 +00:00
|
|
|
|
|
|
|
#include "BufferParams.h"
|
2010-01-04 12:29:38 +00:00
|
|
|
#include "BufferList.h"
|
2011-05-22 21:09:57 +00:00
|
|
|
#include "TextClass.h"
|
2008-11-16 00:15:51 +00:00
|
|
|
#include "Cursor.h"
|
|
|
|
#include "FuncRequest.h"
|
|
|
|
#include "lyxfind.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
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace lyx::support;
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
|
|
|
|
FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
|
2011-03-29 20:51:40 +00:00
|
|
|
: QTabWidget(&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);
|
2010-12-31 14:35:16 +00:00
|
|
|
|
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);
|
2010-12-31 14:35:16 +00:00
|
|
|
|
2008-11-22 18:18:51 +00:00
|
|
|
// We don't want two cursors blinking.
|
2011-02-02 08:55:59 +00:00
|
|
|
find_work_area_->stopBlinkingCursor();
|
2008-11-22 18:18:51 +00:00
|
|
|
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();
|
2011-02-02 08:55:59 +00:00
|
|
|
LYXERR(Debug::FIND, "Selecting entire replace buffer");
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
|
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();
|
2011-02-02 08:55:59 +00:00
|
|
|
LYXERR(Debug::FIND, "Selecting entire find buffer");
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
|
2009-08-21 16:51:07 +00:00
|
|
|
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 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);
|
2011-03-04 22:19:19 +00:00
|
|
|
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-11-27 03:04:56 +00:00
|
|
|
scope = _("File");
|
2010-01-04 12:29:38 +00:00
|
|
|
break;
|
|
|
|
case FindAndReplaceOptions::S_DOCUMENT:
|
2010-11-27 03:04:56 +00:00
|
|
|
scope = _("Master document");
|
2010-01-04 12:29:38 +00:00
|
|
|
break;
|
|
|
|
case FindAndReplaceOptions::S_OPEN_BUFFERS:
|
2010-11-27 03:04:56 +00:00
|
|
|
scope = _("Open files");
|
2010-01-04 12:29:38 +00:00
|
|
|
break;
|
2010-01-09 22:49:35 +00:00
|
|
|
case FindAndReplaceOptions::S_ALL_MANUALS:
|
2010-11-27 03:04:56 +00:00
|
|
|
scope = _("Manuals");
|
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 ?
|
2010-11-27 03:04:56 +00:00
|
|
|
bformat(_("%1$s: the end was 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-11-27 03:04:56 +00:00
|
|
|
bformat(_("%1$s: the beginning was reached while searching backward.\n"
|
2010-01-29 08:59:09 +00:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-04 22:19:19 +00:00
|
|
|
/// Return true if a match was found
|
|
|
|
bool FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt, bool replace_all)
|
2010-01-25 08:06:26 +00:00
|
|
|
{
|
2010-01-04 12:29:38 +00:00
|
|
|
BufferView * bv = view_.documentBufferView();
|
2011-01-05 15:08:08 +00:00
|
|
|
if (!bv)
|
2011-03-04 22:19:19 +00:00
|
|
|
return false;
|
2010-01-04 12:29:38 +00:00
|
|
|
Buffer * buf = &bv->buffer();
|
|
|
|
Buffer * buf_orig = &bv->buffer();
|
2010-03-20 13:59:46 +00:00
|
|
|
DocIterator cur_orig(bv->cursor());
|
2011-01-05 15:08:08 +00:00
|
|
|
int wrap_answer = -1;
|
|
|
|
ostringstream oss;
|
|
|
|
oss << opt;
|
|
|
|
FuncRequest cmd(LFUN_WORD_FINDADV, from_utf8(oss.str()));
|
2010-01-04 12:29:38 +00:00
|
|
|
|
2012-03-06 23:26:19 +00:00
|
|
|
view_.message(_("Advanced search in progress (press ESC to cancel) . . ."));
|
2012-03-06 23:21:12 +00:00
|
|
|
theApp()->startLongOperation();
|
2011-01-05 15:08:08 +00:00
|
|
|
view_.setBusy(true);
|
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);
|
2012-03-06 23:21:12 +00:00
|
|
|
theApp()->stopLongOperation();
|
2010-01-09 22:49:35 +00:00
|
|
|
guiApp->currentView()->loadDocument(fname, false);
|
2012-03-06 23:21:12 +00:00
|
|
|
theApp()->startLongOperation();
|
2010-01-10 23:54:45 +00:00
|
|
|
guiApp->currentView()->setBusy(true);
|
|
|
|
}
|
2010-01-09 22:49:35 +00:00
|
|
|
buf = theBufferList().getBuffer(fname);
|
2011-10-27 09:31:17 +00:00
|
|
|
if (!buf) {
|
|
|
|
view_.setBusy(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-09 22:49:35 +00:00
|
|
|
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()) {
|
2010-12-29 19:59:41 +00:00
|
|
|
// New match found and selected (old selection replaced if needed)
|
2011-03-04 22:19:19 +00:00
|
|
|
if (replace_all)
|
|
|
|
continue;
|
2011-01-03 20:04:03 +00:00
|
|
|
view_.setBusy(false);
|
2012-03-06 23:21:12 +00:00
|
|
|
theApp()->stopLongOperation();
|
2011-03-04 22:19:19 +00:00
|
|
|
return true;
|
2011-12-20 00:02:51 +00:00
|
|
|
} else if (replace_all)
|
|
|
|
bv->clearSelection();
|
2010-01-04 12:29:38 +00:00
|
|
|
|
2012-03-06 23:21:12 +00:00
|
|
|
if (theApp()->longOperationCancelled()) {
|
|
|
|
// Search aborted by user
|
|
|
|
view_.message(_("Advanced search cancelled by user"));
|
|
|
|
view_.setBusy(false);
|
|
|
|
theApp()->stopLongOperation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-29 19:59:41 +00:00
|
|
|
// No match found in current buffer (however old selection might have been replaced)
|
2010-01-25 08:06:26 +00:00
|
|
|
// select next buffer in scope, if any
|
2011-10-27 09:31:17 +00:00
|
|
|
bool const prompt = nextPrevBuffer(buf, opt);
|
|
|
|
if (!buf)
|
|
|
|
break;
|
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);
|
2011-01-03 20:04:03 +00:00
|
|
|
view_.setBusy(false);
|
2012-03-06 23:21:12 +00:00
|
|
|
theApp()->stopLongOperation();
|
2010-01-04 12:29:38 +00:00
|
|
|
wrap_answer = frontend::Alert::prompt(
|
|
|
|
_("Wrap search?"), q,
|
|
|
|
0, 1, _("&Yes"), _("&No"));
|
2012-03-06 23:21:12 +00:00
|
|
|
theApp()->startLongOperation();
|
2011-01-03 20:04:03 +00:00
|
|
|
view_.setBusy(true);
|
2010-01-04 12:29:38 +00:00
|
|
|
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-12-29 19:59:41 +00:00
|
|
|
// This may happen after a replace occurred
|
|
|
|
if (cur_orig.pos() > cur_orig.lastpos())
|
|
|
|
cur_orig.pos() = cur_orig.lastpos();
|
2010-03-20 13:59:46 +00:00
|
|
|
bv->cursor().setCursor(cur_orig);
|
2011-01-03 20:04:03 +00:00
|
|
|
view_.setBusy(false);
|
2012-03-06 23:21:12 +00:00
|
|
|
theApp()->stopLongOperation();
|
2011-03-04 22:19:19 +00:00
|
|
|
return false;
|
2010-01-04 12:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-04 22:19:19 +00:00
|
|
|
/// Return true if a match was found
|
|
|
|
bool FindAndReplaceWidget::findAndReplace(
|
2009-01-14 15:34:56 +00:00
|
|
|
bool casesensitive, bool matchword, bool backwards,
|
2009-08-19 23:05:31 +00:00
|
|
|
bool expandmacros, bool ignoreformat, bool replace,
|
2011-03-04 22:19:19 +00:00
|
|
|
bool keep_case, bool replace_all)
|
2008-11-16 00:15:51 +00:00
|
|
|
{
|
2011-02-07 20:36:40 +00:00
|
|
|
Buffer & find_buf = find_work_area_->bufferView().buffer();
|
|
|
|
docstring const & find_buf_name = find_buf.fileName().absoluteFilePath();
|
|
|
|
|
|
|
|
if (find_buf.text().empty()) {
|
|
|
|
view_.message(_("Nothing to search"));
|
2011-03-04 22:19:19 +00:00
|
|
|
return false;
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
2011-02-07 20:36:40 +00:00
|
|
|
|
|
|
|
Buffer & repl_buf = replace_work_area_->bufferView().buffer();
|
|
|
|
docstring const & repl_buf_name = replace ?
|
|
|
|
repl_buf.fileName().absoluteFilePath() : docstring();
|
|
|
|
|
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: "
|
2011-02-07 20:36:40 +00:00
|
|
|
<< "find_buf_name=" << find_buf_name
|
2009-08-15 15:35:14 +00:00
|
|
|
<< ", casesensitiv=" << casesensitive
|
|
|
|
<< ", matchword=" << matchword
|
|
|
|
<< ", backwards=" << backwards
|
|
|
|
<< ", expandmacros=" << expandmacros
|
|
|
|
<< ", ignoreformat=" << ignoreformat
|
2011-02-07 20:36:40 +00:00
|
|
|
<< ", repl_buf_name" << repl_buf_name
|
2009-12-30 18:40:18 +00:00
|
|
|
<< ", keep_case=" << keep_case
|
|
|
|
<< ", scope=" << scope);
|
2011-02-07 20:36:40 +00:00
|
|
|
FindAndReplaceOptions opt(find_buf_name, casesensitive, matchword,
|
2010-01-25 08:06:26 +00:00
|
|
|
!backwards, expandmacros, ignoreformat,
|
2011-02-07 20:36:40 +00:00
|
|
|
repl_buf_name, keep_case, scope);
|
2011-03-04 22:19:19 +00:00
|
|
|
return findAndReplaceScope(opt, replace_all);
|
2008-11-16 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-04 22:19:19 +00:00
|
|
|
bool FindAndReplaceWidget::findAndReplace(bool backwards, bool replace, bool replace_all)
|
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"));
|
2011-03-04 22:19:19 +00:00
|
|
|
return false;
|
2009-08-22 16:09:44 +00:00
|
|
|
}
|
2010-11-21 15:45:35 +00:00
|
|
|
// Finalize macros that are being typed, both in main document and in search or replacement WAs
|
|
|
|
if (view_.currentWorkArea()->bufferView().cursor().macroModeClose())
|
|
|
|
view_.currentWorkArea()->bufferView().processUpdateFlags(Update::Force);
|
|
|
|
if (view_.currentMainWorkArea()->bufferView().cursor().macroModeClose())
|
|
|
|
view_.currentMainWorkArea()->bufferView().processUpdateFlags(Update::Force);
|
|
|
|
|
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());
|
2011-03-04 22:19:19 +00:00
|
|
|
return findAndReplace(caseCB->isChecked(),
|
2009-01-14 15:34:56 +00:00
|
|
|
wordsCB->isChecked(),
|
|
|
|
backwards,
|
|
|
|
expandMacrosCB->isChecked(),
|
|
|
|
ignoreFormatCB->isChecked(),
|
2009-08-19 23:05:31 +00:00
|
|
|
replace,
|
2011-03-04 22:19:19 +00:00
|
|
|
keepCaseCB->isChecked(),
|
|
|
|
replace_all);
|
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()
|
|
|
|
{
|
2011-03-04 22:19:19 +00:00
|
|
|
findAndReplace(searchbackCB->isChecked(), true, true);
|
2009-08-21 16:51:07 +00:00
|
|
|
replace_work_area_->setFocus();
|
2008-12-23 08:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-22 21:09:57 +00:00
|
|
|
/** Copy selected elements from bv's BufferParams to the dest_bv's one
|
|
|
|
** We don't want to copy'em all, e.g., not the default master **/
|
|
|
|
static void copy_params(BufferView const & bv, BufferView & dest_bv) {
|
|
|
|
Buffer const & doc_buf = bv.buffer();
|
|
|
|
BufferParams const & doc_bp = doc_buf.params();
|
|
|
|
string const & lang = doc_bp.language->lang();
|
|
|
|
string const & doc_class = doc_bp.documentClass().name();
|
|
|
|
Buffer & dest_buf = dest_bv.buffer();
|
|
|
|
dest_buf.params().setLanguage(lang);
|
|
|
|
dest_buf.params().setBaseClass(doc_class);
|
2012-06-04 17:39:24 +00:00
|
|
|
dest_bv.makeDocumentClass();
|
2011-05-22 21:09:57 +00:00
|
|
|
dest_bv.cursor().current_font.setLanguage(doc_bp.language);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-12 14:30:29 +00:00
|
|
|
void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
|
2008-12-23 08:18:11 +00:00
|
|
|
{
|
2011-02-02 08:55:59 +00:00
|
|
|
LYXERR(Debug::DEBUG, "showEvent()" << endl);
|
2011-01-05 15:08:08 +00:00
|
|
|
BufferView * bv = view_.documentBufferView();
|
|
|
|
if (bv) {
|
2011-05-22 21:09:57 +00:00
|
|
|
copy_params(*bv, find_work_area_->bufferView());
|
|
|
|
copy_params(*bv, replace_work_area_->bufferView());
|
2011-01-05 15:08:08 +00:00
|
|
|
}
|
2011-02-02 08:55:59 +00:00
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
find_work_area_->installEventFilter(this);
|
2009-08-21 16:51:07 +00:00
|
|
|
replace_work_area_->installEventFilter(this);
|
2011-02-02 08:55:59 +00:00
|
|
|
|
|
|
|
view_.setCurrentWorkArea(find_work_area_);
|
|
|
|
LYXERR(Debug::FIND, "Selecting entire find buffer");
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-30 21:26:51 +00:00
|
|
|
void FindAndReplace::updateView()
|
|
|
|
{
|
|
|
|
widget_->updateGUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-23 08:18:11 +00:00
|
|
|
FindAndReplace::FindAndReplace(GuiView & parent,
|
|
|
|
Qt::DockWidgetArea area, Qt::WindowFlags flags)
|
2011-01-06 00:52:46 +00:00
|
|
|
: DockView(parent, "findreplaceadv", qt_("Advanced Find and Replace"),
|
2010-01-25 08:06:26 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-30 21:26:51 +00:00
|
|
|
void FindAndReplaceWidget::updateGUI()
|
|
|
|
{
|
|
|
|
bool replace_enabled = view_.documentBufferView()
|
|
|
|
&& !view_.documentBufferView()->buffer().isReadonly();
|
|
|
|
replace_work_area_->setEnabled(replace_enabled);
|
|
|
|
replacePB->setEnabled(replace_enabled);
|
|
|
|
replaceallPB->setEnabled(replace_enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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"
|