2 * \file FindAndReplace.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Tommaso Cucinotta
8 * Full author contact details are available in file CREDITS.
13 #include "FindAndReplace.h"
15 #include "GuiApplication.h"
17 #include "GuiWorkArea.h"
18 #include "qt_helpers.h"
20 #include "buffer_funcs.h"
21 #include "BufferParams.h"
22 #include "BufferList.h"
24 #include "FuncRequest.h"
26 #include "output_latex.h"
27 #include "OutputParams.h"
30 #include "frontends/alert.h"
32 #include "support/debug.h"
33 #include "support/filetools.h"
34 #include "support/FileName.h"
35 #include "support/gettext.h"
36 #include "support/lassert.h"
37 #include "support/lstrings.h"
39 #include <QCloseEvent>
46 using namespace lyx::support;
52 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
56 find_work_area_->setGuiView(view_);
57 find_work_area_->init();
58 find_work_area_->setFrameStyle(QFrame::StyledPanel);
59 setFocusProxy(find_work_area_);
60 replace_work_area_->setGuiView(view_);
61 replace_work_area_->init();
62 replace_work_area_->setFrameStyle(QFrame::StyledPanel);
63 // We don't want two cursors blinking.
64 replace_work_area_->stopBlinkingCursor();
68 bool FindAndReplaceWidget::eventFilter(QObject * obj, QEvent * event)
70 if (event->type() != QEvent::KeyPress
71 || (obj != find_work_area_ && obj != replace_work_area_))
72 return QWidget::eventFilter(obj, event);
74 QKeyEvent * e = static_cast<QKeyEvent *> (event);
77 if (e->modifiers() == Qt::NoModifier) {
84 case Qt::Key_Return: {
85 // with shift we (temporarily) change search/replace direction
86 bool const searchback = searchbackCB->isChecked();
87 if (e->modifiers() == Qt::ShiftModifier && !searchback)
88 searchbackCB->setChecked(!searchback);
90 if (obj == find_work_area_)
91 on_findNextPB_clicked();
93 on_replacePB_clicked();
95 searchbackCB->setChecked(searchback);
100 if (e->modifiers() == Qt::NoModifier) {
101 if (obj == find_work_area_){
102 LYXERR(Debug::FIND, "Focusing replace WA");
103 replace_work_area_->setFocus();
109 case Qt::Key_Backtab:
110 if (obj == replace_work_area_) {
111 LYXERR(Debug::FIND, "Focusing find WA");
112 find_work_area_->setFocus();
120 // standard event processing
121 return QWidget::eventFilter(obj, event);
125 static docstring buffer_to_latex(Buffer & buffer)
127 OutputParams runparams(&buffer.params().encoding());
129 runparams.nice = true;
130 runparams.flavor = OutputParams::LATEX;
131 runparams.linelen = 80; //lyxrc.plaintext_linelen;
132 // No side effect of file copying and image conversion
133 runparams.dryrun = true;
134 buffer.texrow().reset();
135 pit_type const endpit = buffer.paragraphs().size();
136 for (pit_type pit = 0; pit != endpit; ++pit) {
137 TeXOnePar(buffer, buffer.text(),
138 pit, os, buffer.texrow(), runparams);
139 LYXERR(Debug::FIND, "searchString up to here: "
146 static vector<string> const & allManualsFiles()
148 static vector<string> v;
149 static const char * files[] = {
150 "Intro", "UserGuide", "Tutorial", "Additional",
151 "EmbeddedObjects", "Math", "Customization", "Shortcuts",
152 "LFUNs", "LaTeXConfig"
156 for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); ++i) {
157 fname = i18nLibFileSearch("doc", files[i], "lyx");
158 v.push_back(fname.absFileName());
165 /** Switch p_buf to point to next document buffer.
167 ** Return true if restarted from master-document buffer.
169 static bool nextDocumentBuffer(Buffer * & buf)
171 ListOfBuffers const children = buf->allRelatives();
172 LYXERR(Debug::FIND, "children.size()=" << children.size());
173 ListOfBuffers::const_iterator it =
174 find(children.begin(), children.end(), buf);
175 LASSERT(it != children.end(), /**/)
177 if (it == children.end()) {
178 buf = *children.begin();
186 /** Switch p_buf to point to previous document buffer.
188 ** Return true if restarted from last child buffer.
190 static bool prevDocumentBuffer(Buffer * & buf)
192 ListOfBuffers const children = buf->allRelatives();
193 LYXERR(Debug::FIND, "children.size()=" << children.size());
194 ListOfBuffers::const_iterator it =
195 find(children.begin(), children.end(), buf);
196 LASSERT(it != children.end(), /**/)
197 if (it == children.begin()) {
209 /** Switch buf to point to next or previous buffer in search scope.
211 ** Return true if restarted from scratch.
213 static bool nextPrevBuffer(Buffer * & buf,
214 FindAndReplaceOptions const & opt)
216 bool restarted = false;
218 case FindAndReplaceOptions::S_BUFFER:
221 case FindAndReplaceOptions::S_DOCUMENT:
223 restarted = nextDocumentBuffer(buf);
225 restarted = prevDocumentBuffer(buf);
227 case FindAndReplaceOptions::S_OPEN_BUFFERS:
229 buf = theBufferList().next(buf);
230 restarted = (buf == *theBufferList().begin());
232 buf = theBufferList().previous(buf);
233 restarted = (buf == *(theBufferList().end() - 1));
236 case FindAndReplaceOptions::S_ALL_MANUALS:
237 vector<string> const & manuals = allManualsFiles();
238 vector<string>::const_iterator it =
239 find(manuals.begin(), manuals.end(), buf->absFileName());
240 if (it == manuals.end())
241 it = manuals.begin();
242 else if (opt.forward) {
244 if (it == manuals.end()) {
245 it = manuals.begin();
249 if (it == manuals.begin()) {
255 FileName const & fname = FileName(*it);
256 if (!theBufferList().exists(fname)) {
257 guiApp->currentView()->setBusy(false);
258 guiApp->currentView()->loadDocument(fname, false);
259 guiApp->currentView()->setBusy(true);
261 buf = theBufferList().getBuffer(fname);
268 /** Find the finest question message to post to the user */
269 docstring getQuestionString(FindAndReplaceOptions const & opt)
273 case FindAndReplaceOptions::S_BUFFER:
276 case FindAndReplaceOptions::S_DOCUMENT:
277 scope = _("Master document");
279 case FindAndReplaceOptions::S_OPEN_BUFFERS:
280 scope = _("Open files");
282 case FindAndReplaceOptions::S_ALL_MANUALS:
283 scope = _("Manuals");
286 docstring message = opt.forward ?
287 bformat(_("%1$s: the end was reached while searching forward.\n"
288 "Continue searching from the beginning?"),
290 bformat(_("%1$s: the beginning was reached while searching backward.\n"
291 "Continue searching from the end?"),
298 void FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt)
300 int wrap_answer = -1;
303 FuncRequest cmd(LFUN_WORD_FINDADV, from_utf8(oss.str()));
304 BufferView * bv = view_.documentBufferView();
305 Buffer * buf = &bv->buffer();
307 Buffer * buf_orig = &bv->buffer();
308 DocIterator cur_orig(bv->cursor());
310 if (opt.scope == FindAndReplaceOptions::S_ALL_MANUALS) {
311 vector<string> const & v = allManualsFiles();
312 if (std::find(v.begin(), v.end(), buf->absFileName()) == v.end()) {
313 FileName const & fname = FileName(*v.begin());
314 if (!theBufferList().exists(fname)) {
315 guiApp->currentView()->setBusy(false);
316 guiApp->currentView()->loadDocument(fname, false);
317 guiApp->currentView()->setBusy(true);
319 buf = theBufferList().getBuffer(fname);
320 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
321 buf->absFileName()));
322 bv = view_.documentBufferView();
323 bv->cursor().clear();
324 bv->cursor().push_back(CursorSlice(buf->inset()));
329 LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
331 LYXERR(Debug::FIND, "dispatched");
332 if (bv->cursor().result().dispatched()) {
333 // New match found and selected (old selection replaced if needed)
337 // No match found in current buffer (however old selection might have been replaced)
338 // select next buffer in scope, if any
339 bool prompt = nextPrevBuffer(buf, opt);
341 if (wrap_answer != -1)
343 docstring q = getQuestionString(opt);
344 wrap_answer = frontend::Alert::prompt(
345 _("Wrap search?"), q,
346 0, 1, _("&Yes"), _("&No"));
347 if (wrap_answer == 1)
350 if (buf != &view_.documentBufferView()->buffer())
351 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
352 buf->absFileName()));
353 bv = view_.documentBufferView();
355 bv->cursor().clear();
356 bv->cursor().push_back(CursorSlice(buf->inset()));
358 //lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
359 bv->cursor().setCursor(doc_iterator_end(buf));
360 bv->cursor().backwardPos();
361 LYXERR(Debug::FIND, "findBackAdv5: cur: "
364 bv->clearSelection();
365 } while (wrap_answer != 1);
366 if (buf_orig != &view_.documentBufferView()->buffer())
367 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
368 buf_orig->absFileName()));
369 bv = view_.documentBufferView();
370 // This may happen after a replace occurred
371 if (cur_orig.pos() > cur_orig.lastpos())
372 cur_orig.pos() = cur_orig.lastpos();
373 bv->cursor().setCursor(cur_orig);
377 void FindAndReplaceWidget::findAndReplace(
378 bool casesensitive, bool matchword, bool backwards,
379 bool expandmacros, bool ignoreformat, bool replace,
382 Buffer & buffer = find_work_area_->bufferView().buffer();
383 docstring searchString;
385 searchString = buffer_to_latex(buffer);
387 ParIterator it = buffer.par_iterator_begin();
388 ParIterator end = buffer.par_iterator_end();
389 OutputParams runparams(&buffer.params().encoding());
391 runparams.nice = true;
392 runparams.flavor = OutputParams::LATEX;
393 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
394 runparams.dryrun = true;
395 for (; it != end; ++it) {
396 LYXERR(Debug::FIND, "Adding to search string: '"
397 << it->asString(false)
400 it->stringify(pos_type(0), it->size(),
401 AS_STR_INSETS, runparams);
404 if (to_utf8(searchString).empty()) {
405 buffer.message(_("Nothing to search"));
409 to_utf8(searchString).find("\\regexp") != std::string::npos;
410 docstring replaceString;
412 Buffer & repl_buffer =
413 replace_work_area_->bufferView().buffer();
415 repl_buffer.write(oss);
416 //buffer_to_latex(replace_buffer);
417 replaceString = from_utf8(oss.str());
419 replaceString = from_utf8(LYX_FR_NULL_STRING);
421 FindAndReplaceOptions::SearchScope scope =
422 FindAndReplaceOptions::S_BUFFER;
423 if (CurrentDocument->isChecked())
424 scope = FindAndReplaceOptions::S_BUFFER;
425 else if (MasterDocument->isChecked())
426 scope = FindAndReplaceOptions::S_DOCUMENT;
427 else if (OpenDocuments->isChecked())
428 scope = FindAndReplaceOptions::S_OPEN_BUFFERS;
429 else if (AllManualsRB->isChecked())
430 scope = FindAndReplaceOptions::S_ALL_MANUALS;
432 LASSERT(false, /**/);
433 LYXERR(Debug::FIND, "FindAndReplaceOptions: "
434 << "searchstring=" << searchString
435 << ", casesensitiv=" << casesensitive
436 << ", matchword=" << matchword
437 << ", backwards=" << backwards
438 << ", expandmacros=" << expandmacros
439 << ", ignoreformat=" << ignoreformat
440 << ", regexp=" << regexp
441 << ", replaceString" << replaceString
442 << ", keep_case=" << keep_case
443 << ", scope=" << scope);
444 FindAndReplaceOptions opt(searchString, casesensitive, matchword,
445 !backwards, expandmacros, ignoreformat,
446 regexp, replaceString, keep_case, scope);
448 findAndReplaceScope(opt);
449 view_.setBusy(false);
453 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
455 if (! view_.currentMainWorkArea()) {
456 view_.message(_("No open document(s) in which to search"));
459 // Finalize macros that are being typed, both in main document and in search or replacement WAs
460 if (view_.currentWorkArea()->bufferView().cursor().macroModeClose())
461 view_.currentWorkArea()->bufferView().processUpdateFlags(Update::Force);
462 if (view_.currentMainWorkArea()->bufferView().cursor().macroModeClose())
463 view_.currentMainWorkArea()->bufferView().processUpdateFlags(Update::Force);
465 // FIXME: create a Dialog::returnFocus()
466 // or something instead of this:
467 view_.setCurrentWorkArea(view_.currentMainWorkArea());
468 findAndReplace(caseCB->isChecked(),
469 wordsCB->isChecked(),
471 expandMacrosCB->isChecked(),
472 ignoreFormatCB->isChecked(),
474 keepCaseCB->isChecked());
478 void FindAndReplaceWidget::hideDialog()
480 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
484 void FindAndReplaceWidget::on_findNextPB_clicked()
486 findAndReplace(searchbackCB->isChecked(), false);
487 find_work_area_->setFocus();
491 void FindAndReplaceWidget::on_replacePB_clicked()
493 findAndReplace(searchbackCB->isChecked(), true);
494 replace_work_area_->setFocus();
498 void FindAndReplaceWidget::on_replaceallPB_clicked()
500 replace_work_area_->setFocus();
504 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
506 view_.setCurrentWorkArea(find_work_area_);
507 LYXERR(Debug::FIND, "Selecting entire find buffer");
508 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
509 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
510 find_work_area_->installEventFilter(this);
511 replace_work_area_->installEventFilter(this);
515 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
517 replace_work_area_->removeEventFilter(this);
518 find_work_area_->removeEventFilter(this);
519 this->QWidget::hideEvent(ev);
523 bool FindAndReplaceWidget::initialiseParams(std::string const & /*params*/)
529 FindAndReplace::FindAndReplace(GuiView & parent,
530 Qt::DockWidgetArea area, Qt::WindowFlags flags)
531 : DockView(parent, "Find LyX", qt_("Advanced Find and Replace"),
534 widget_ = new FindAndReplaceWidget(parent);
536 setFocusProxy(widget_);
540 FindAndReplace::~FindAndReplace()
547 bool FindAndReplace::initialiseParams(std::string const & params)
549 return widget_->initialiseParams(params);
553 Dialog * createGuiSearchAdv(GuiView & lv)
555 FindAndReplace * gui = new FindAndReplace(lv, Qt::RightDockWidgetArea);
557 // On Mac show and floating
558 gui->setFloating(true);
564 } // namespace frontend
568 #include "moc_FindAndReplace.cpp"