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"
19 #include "BufferList.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
26 #include "KeySequence.h"
32 #include "frontends/alert.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/filetools.h"
37 #include "support/FileName.h"
38 #include "support/gettext.h"
39 #include "support/lassert.h"
40 #include "support/lstrings.h"
42 #include <QCloseEvent>
47 using namespace lyx::support;
53 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
54 : QTabWidget(&view), view_(view)
57 find_work_area_->setGuiView(view_);
58 find_work_area_->init();
59 find_work_area_->setFrameStyle(QFrame::StyledPanel);
61 setFocusProxy(find_work_area_);
62 replace_work_area_->setGuiView(view_);
63 replace_work_area_->init();
64 replace_work_area_->setFrameStyle(QFrame::StyledPanel);
66 // We don't want two cursors blinking.
67 find_work_area_->stopBlinkingCaret();
68 replace_work_area_->stopBlinkingCaret();
69 old_buffer_ = view_.documentBufferView() ?
70 &(view_.documentBufferView()->buffer()) : 0;
73 cbVerticalLayout->setAlignment(Qt::AlignTop);
74 pbVerticalLayout->setAlignment(Qt::AlignTop);
78 void FindAndReplaceWidget::dockLocationChanged(Qt::DockWidgetArea area)
80 if (area == Qt::RightDockWidgetArea || area == Qt::LeftDockWidgetArea) {
81 dynamicLayoutBasic_->setDirection(QBoxLayout::TopToBottom);
82 dynamicLayoutAdvanced_->setDirection(QBoxLayout::TopToBottom);
84 dynamicLayoutBasic_->setDirection(QBoxLayout::LeftToRight);
85 dynamicLayoutAdvanced_->setDirection(QBoxLayout::LeftToRight);
90 bool FindAndReplaceWidget::eventFilter(QObject * obj, QEvent * event)
93 if (event->type() != QEvent::KeyPress
94 || (obj != find_work_area_ && obj != replace_work_area_))
95 return QWidget::eventFilter(obj, event);
97 QKeyEvent * e = static_cast<QKeyEvent *> (event);
100 if (e->modifiers() == Qt::NoModifier) {
107 case Qt::Key_Return: {
108 bool const searchback = (e->modifiers() == Qt::ShiftModifier);
109 bool const replace = (obj == replace_work_area_);
110 findAndReplace(searchback, replace);
112 replace_work_area_->setFocus();
114 find_work_area_->setFocus();
119 if (e->modifiers() == Qt::NoModifier && obj == find_work_area_){
122 FuncRequest func = theTopLevelKeymap().getBinding(seq);
123 if (!getStatus(func).enabled()) {
124 LYXERR(Debug::FINDVERBOSE, "Focusing replace WA");
125 replace_work_area_->setFocus();
126 LYXERR(Debug::FINDVERBOSE, "Selecting entire replace buffer");
127 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
128 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
134 case Qt::Key_Backtab:
135 if (obj == replace_work_area_) {
137 seq.parse("~S-BackTab");
138 FuncRequest func = theTopLevelKeymap().getBinding(seq);
139 if (!getStatus(func).enabled()) {
140 LYXERR(Debug::FINDVERBOSE, "Focusing find WA");
141 find_work_area_->setFocus();
142 LYXERR(Debug::FINDVERBOSE, "Selecting entire find buffer");
143 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
144 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
153 // standard event processing
154 return QWidget::eventFilter(obj, event);
158 static vector<string> const & allManualsFiles()
160 static const char * files[] = {
161 "Intro", "UserGuide", "Tutorial", "Additional",
162 "EmbeddedObjects", "Math", "Customization", "Shortcuts",
163 "LFUNs", "LaTeXConfig"
166 static vector<string> v;
169 for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); ++i) {
170 fname = i18nLibFileSearch("doc", files[i], "lyx");
171 v.push_back(fname.absFileName());
179 /** Switch buf to point to next document buffer.
181 ** Return true if restarted from master-document buffer.
183 static bool nextDocumentBuffer(Buffer * & buf)
185 ListOfBuffers const children = buf->allRelatives();
186 LYXERR(Debug::FINDVERBOSE, "children.size()=" << children.size());
187 ListOfBuffers::const_iterator it =
188 find(children.begin(), children.end(), buf);
189 LASSERT(it != children.end(), return false);
191 if (it == children.end()) {
192 buf = *children.begin();
200 /** Switch p_buf to point to previous document buffer.
202 ** Return true if restarted from last child buffer.
204 static bool prevDocumentBuffer(Buffer * & buf)
206 ListOfBuffers const children = buf->allRelatives();
207 LYXERR(Debug::FINDVERBOSE, "children.size()=" << children.size());
208 ListOfBuffers::const_iterator it =
209 find(children.begin(), children.end(), buf);
210 LASSERT(it != children.end(), return false)
211 if (it == children.begin()) {
223 /** Switch buf to point to next or previous buffer in search scope.
225 ** Return true if restarted from scratch.
227 static bool nextPrevBuffer(Buffer * & buf,
228 FindAndReplaceOptions const & opt)
230 bool restarted = false;
232 case FindAndReplaceOptions::S_BUFFER:
235 case FindAndReplaceOptions::S_DOCUMENT:
237 restarted = nextDocumentBuffer(buf);
239 restarted = prevDocumentBuffer(buf);
241 case FindAndReplaceOptions::S_OPEN_BUFFERS:
243 buf = theBufferList().next(buf);
244 restarted = (buf == *theBufferList().begin());
246 buf = theBufferList().previous(buf);
247 restarted = (buf == *(theBufferList().end() - 1));
250 case FindAndReplaceOptions::S_ALL_MANUALS:
251 vector<string> const & manuals = allManualsFiles();
252 vector<string>::const_iterator it =
253 find(manuals.begin(), manuals.end(), buf->absFileName());
254 if (it == manuals.end())
255 it = manuals.begin();
256 else if (opt.forward) {
258 if (it == manuals.end()) {
259 it = manuals.begin();
263 if (it == manuals.begin()) {
269 FileName const & fname = FileName(*it);
270 if (!theBufferList().exists(fname)) {
271 guiApp->currentView()->setBusy(false);
272 guiApp->currentView()->loadDocument(fname, false);
273 guiApp->currentView()->setBusy(true);
275 buf = theBufferList().getBuffer(fname);
282 /** Find the finest question message to post to the user */
283 docstring getQuestionString(FindAndReplaceOptions const & opt)
287 case FindAndReplaceOptions::S_BUFFER:
290 case FindAndReplaceOptions::S_DOCUMENT:
291 scope = _("Master document");
293 case FindAndReplaceOptions::S_OPEN_BUFFERS:
294 scope = _("Open files");
296 case FindAndReplaceOptions::S_ALL_MANUALS:
297 scope = _("Manuals");
300 docstring message = opt.forward ?
301 bformat(_("%1$s: the end was reached while searching forward.\n"
302 "Continue searching from the beginning?"),
304 bformat(_("%1$s: the beginning was reached while searching backward.\n"
305 "Continue searching from the end?"),
312 /// Return true if a match was found
313 bool FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt, bool replace_all)
315 BufferView * bv = view_.documentBufferView();
318 Buffer * buf = &bv->buffer();
319 Buffer * buf_orig = &bv->buffer();
320 DocIterator cur_orig(bv->cursor());
321 int wrap_answer = -1;
322 opt.replace_all = replace_all;
325 FuncRequest cmd(LFUN_WORD_FINDADV, from_utf8(oss.str()));
327 view_.message(_("Advanced search in progress (press ESC to cancel) . . ."));
328 theApp()->startLongOperation();
330 if (opt.scope == FindAndReplaceOptions::S_ALL_MANUALS) {
331 vector<string> const & v = allManualsFiles();
332 if (std::find(v.begin(), v.end(), buf->absFileName()) == v.end()) {
333 FileName const & fname = FileName(*v.begin());
334 if (!theBufferList().exists(fname)) {
335 guiApp->currentView()->setBusy(false);
336 theApp()->stopLongOperation();
337 guiApp->currentView()->loadDocument(fname, false);
338 theApp()->startLongOperation();
339 guiApp->currentView()->setBusy(true);
341 buf = theBufferList().getBuffer(fname);
343 view_.setBusy(false);
347 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
348 buf->absFileName()));
349 bv = view_.documentBufferView();
350 bv->cursor().clear();
351 bv->cursor().push_back(CursorSlice(buf->inset()));
355 UndoGroupHelper helper(buf);
358 LYXERR(Debug::FINDVERBOSE, "Dispatching LFUN_WORD_FINDADV");
360 LYXERR(Debug::FINDVERBOSE, "dispatched");
361 if (bv->cursor().result().dispatched()) {
362 // New match found and selected (old selection replaced if needed)
365 view_.setBusy(false);
366 theApp()->stopLongOperation();
368 } else if (replace_all)
369 bv->clearSelection();
371 if (theApp()->longOperationCancelled()) {
372 // Search aborted by user
373 view_.message(_("Advanced search cancelled by user"));
374 view_.setBusy(false);
375 theApp()->stopLongOperation();
379 // No match found in current buffer (however old selection might have been replaced)
380 // select next buffer in scope, if any
381 bool const prompt = nextPrevBuffer(buf, opt);
385 if (wrap_answer != -1)
387 docstring q = getQuestionString(opt);
388 view_.setBusy(false);
389 theApp()->stopLongOperation();
390 wrap_answer = frontend::Alert::prompt(
391 _("Wrap search?"), q,
392 0, 1, _("&Yes"), _("&No"));
393 theApp()->startLongOperation();
395 if (wrap_answer == 1)
398 if (buf != &view_.documentBufferView()->buffer())
399 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
400 buf->absFileName()));
402 helper.resetBuffer(buf);
404 bv = view_.documentBufferView();
406 bv->cursor().clear();
407 bv->cursor().push_back(CursorSlice(buf->inset()));
409 //lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
410 bv->cursor().setCursor(doc_iterator_end(buf));
411 bv->cursor().backwardPos();
412 LYXERR(Debug::FINDVERBOSE, "findBackAdv5: cur: "
415 bv->clearSelection();
416 } while (wrap_answer != 1);
418 if (buf_orig != &view_.documentBufferView()->buffer())
419 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
420 buf_orig->absFileName()));
421 bv = view_.documentBufferView();
422 // This may happen after a replace occurred
423 if (cur_orig.pos() > cur_orig.lastpos())
424 cur_orig.pos() = cur_orig.lastpos();
425 bv->cursor().setCursor(cur_orig);
426 view_.setBusy(false);
427 theApp()->stopLongOperation();
432 /// Return true if a match was found
433 bool FindAndReplaceWidget::findAndReplace(
434 bool casesensitive, bool matchword, bool backwards,
435 bool expandmacros, bool adhereformat, bool replace,
436 bool keep_case, bool replace_all)
438 Buffer & find_buf = find_work_area_->bufferView().buffer();
439 docstring const & find_buf_name = find_buf.fileName().absoluteFilePath();
441 if (find_buf.text().empty()) {
442 view_.message(_("Nothing to search"));
446 Buffer & repl_buf = replace_work_area_->bufferView().buffer();
447 docstring const & repl_buf_name = replace ?
448 repl_buf.fileName().absoluteFilePath() : docstring();
450 FindAndReplaceOptions::SearchScope scope =
451 FindAndReplaceOptions::S_BUFFER;
452 if (CurrentDocument->isChecked())
453 scope = FindAndReplaceOptions::S_BUFFER;
454 else if (MasterDocument->isChecked())
455 scope = FindAndReplaceOptions::S_DOCUMENT;
456 else if (OpenDocuments->isChecked())
457 scope = FindAndReplaceOptions::S_OPEN_BUFFERS;
458 else if (AllManualsRB->isChecked())
459 scope = FindAndReplaceOptions::S_ALL_MANUALS;
463 FindAndReplaceOptions::SearchRestriction restr =
464 OnlyMaths->isChecked()
465 ? FindAndReplaceOptions::R_ONLY_MATHS
466 : FindAndReplaceOptions::R_EVERYTHING;
468 LYXERR(Debug::FINDVERBOSE, "FindAndReplaceOptions: "
469 << "find_buf_name=" << find_buf_name
470 << ", casesensitiv=" << casesensitive
471 << ", matchword=" << matchword
472 << ", backwards=" << backwards
473 << ", expandmacros=" << expandmacros
474 << ", adhereformat=" << adhereformat
475 << ", repl_buf_name" << repl_buf_name
476 << ", keep_case=" << keep_case
477 << ", scope=" << scope
478 << ", restr=" << restr);
480 FindAndReplaceOptions opt(find_buf_name, casesensitive, matchword,
481 !backwards, expandmacros, !adhereformat,
482 repl_buf_name, keep_case, scope, restr);
486 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("language",
487 !adhereLanguageCB->isChecked())));
488 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("color",
489 !adhereColorCB->isChecked())));
490 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("family",
491 !adhereFFamilyCB->isChecked())));
492 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("series",
493 !adhereFSeriesCB->isChecked())));
494 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("shape",
495 !adhereFShapeCB->isChecked())));
496 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("size",
497 !adhereFSizeCB->isChecked())));
498 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("markup",
499 !adhereMarkupCB->isChecked())));
500 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("underline",
501 !adhereUnderlineCB->isChecked())));
502 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("strike",
503 !adhereStrikeCB->isChecked())));
504 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("deleted",
505 !adhereDeletedCB->isChecked())));
506 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("sectioning",
507 !adhereSectioningCB->isChecked())));
509 lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("non-output-content",
510 ignoreNonOutputCB->isChecked())));
512 return findAndReplaceScope(opt, replace_all);
516 docstring const FindAndReplaceWidget::checkState(string const s, bool const b)
518 docstring res = from_ascii(s) + from_ascii(" ");
520 res += from_ascii("true");
522 res += from_ascii("false");
527 bool FindAndReplaceWidget::findAndReplace(bool backwards, bool replace, bool replace_all)
529 if (! view_.currentMainWorkArea()) {
530 view_.message(_("No open document(s) in which to search"));
533 // Finalize macros that are being typed, both in main document and in search or replacement WAs
534 if (view_.currentWorkArea()->bufferView().cursor().macroModeClose())
535 view_.currentWorkArea()->bufferView().processUpdateFlags(Update::Force);
536 if (view_.currentMainWorkArea()->bufferView().cursor().macroModeClose())
537 view_.currentMainWorkArea()->bufferView().processUpdateFlags(Update::Force);
539 // FIXME: create a Dialog::returnFocus()
540 // or something instead of this:
541 view_.setCurrentWorkArea(view_.currentMainWorkArea());
542 return findAndReplace(caseCB->isChecked(),
543 wordsCB->isChecked(),
545 expandMacrosCB->isChecked(),
546 adhereFormatGB->isChecked(),
548 keepCaseCB->isChecked(),
553 void FindAndReplaceWidget::hideDialog()
555 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
559 void FindAndReplaceWidget::on_findNextPB_clicked()
561 findAndReplace(searchbackCB->isChecked(), false);
562 find_work_area_->setFocus();
566 void FindAndReplaceWidget::on_replacePB_clicked()
568 findAndReplace(searchbackCB->isChecked(), true);
569 replace_work_area_->setFocus();
573 void FindAndReplaceWidget::on_replaceallPB_clicked()
575 findAndReplace(searchbackCB->isChecked(), true, true);
576 replace_work_area_->setFocus();
580 void FindAndReplaceWidget::on_searchbackCB_clicked()
586 void FindAndReplaceWidget::setFormatIgnores(bool const b)
588 adhereLanguageCB->setChecked(b);
589 adhereColorCB->setChecked(b);
590 adhereFFamilyCB->setChecked(b);
591 adhereFSeriesCB->setChecked(b);
592 adhereFShapeCB->setChecked(b);
593 adhereFSizeCB->setChecked(b);
594 adhereMarkupCB->setChecked(b);
595 adhereUnderlineCB->setChecked(b);
596 adhereStrikeCB->setChecked(b);
597 adhereDeletedCB->setChecked(b);
598 adhereSectioningCB->setChecked(b);
602 void FindAndReplaceWidget::on_selectAllPB_clicked()
604 setFormatIgnores(true);
608 void FindAndReplaceWidget::on_deselectAllPB_clicked()
610 setFormatIgnores(false);
614 // Copy selected elements from bv's BufferParams to the dest_bv's
615 static void copy_params(BufferView const & bv, BufferView & dest_bv) {
616 Buffer const & doc_buf = bv.buffer();
617 BufferParams const & doc_bp = doc_buf.params();
618 Buffer & dest_buf = dest_bv.buffer();
619 dest_buf.params().copyForAdvFR(doc_bp);
620 dest_bv.makeDocumentClass();
621 dest_bv.cursor().current_font.setLanguage(doc_bp.language);
625 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
627 LYXERR(Debug::DEBUG, "showEvent()" << endl);
628 BufferView * bv = view_.documentBufferView();
630 copy_params(*bv, find_work_area_->bufferView());
631 copy_params(*bv, replace_work_area_->bufferView());
634 find_work_area_->installEventFilter(this);
635 replace_work_area_->installEventFilter(this);
637 view_.setCurrentWorkArea(find_work_area_);
638 LYXERR(Debug::FINDVERBOSE, "Selecting entire find buffer");
639 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
640 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
644 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
646 replace_work_area_->removeEventFilter(this);
647 find_work_area_->removeEventFilter(this);
648 this->QWidget::hideEvent(ev);
652 bool FindAndReplaceWidget::initialiseParams(std::string const & /*params*/)
658 FindAndReplace::FindAndReplace(GuiView & parent,
659 Qt::DockWidgetArea area,
660 Qt::WindowFlags flags)
661 : DockView(parent, "findreplaceadv", qt_("Advanced Find and Replace"),
664 widget_ = new FindAndReplaceWidget(parent);
666 setFocusProxy(widget_);
668 // On Mac show and floating
672 connect(this, SIGNAL(dockLocationChanged(Qt::DockWidgetArea)),
673 widget_, SLOT(dockLocationChanged(Qt::DockWidgetArea)));
677 FindAndReplace::~FindAndReplace()
679 setFocusProxy(nullptr);
684 bool FindAndReplace::initialiseParams(std::string const & params)
686 return widget_->initialiseParams(params);
690 void FindAndReplaceWidget::updateWorkAreas()
692 BufferView * bv = view_.documentBufferView();
694 if (old_buffer_ != &bv->buffer()) {
695 old_buffer_ = &bv->buffer();
696 copy_params(*bv, find_work_area_->bufferView());
697 copy_params(*bv, replace_work_area_->bufferView());
700 old_buffer_ = nullptr;
704 void FindAndReplaceWidget::updateButtons()
706 if (searchbackCB->isChecked()) {
707 findNextPB->setText(qt_("&< Find"));
708 findNextPB->setToolTip(qt_("Find previous occurrence (Shift+Enter, forwards: Enter)"));
709 replacePB->setText(qt_("< Rep&lace"));
710 replacePB->setToolTip(qt_("Replace and find previous occurrence (Shift+Enter, forwards: Enter)"));
712 findNextPB->setText(qt_("Find &>"));
713 findNextPB->setToolTip(qt_("Find next occurrence (Enter, backwards: Shift+Enter)"));
714 replacePB->setText(qt_("Rep&lace >"));
715 replacePB->setToolTip(qt_("Replace and find next occurrence (Enter, backwards: Shift+Enter)"));
718 BufferView * bv = view_.documentBufferView();
719 bool const find_enabled = !find_work_area_->bufferView().buffer().empty();
720 findNextPB->setEnabled(find_enabled);
721 bool const replace_enabled = find_enabled && bv && !bv->buffer().isReadonly();
722 replaceLabel->setEnabled(replace_enabled);
723 replace_work_area_->setEnabled(replace_enabled);
724 replacePB->setEnabled(replace_enabled);
725 replaceallPB->setEnabled(replace_enabled);
729 void FindAndReplace::updateView()
731 widget_->updateWorkAreas();
732 widget_->updateButtons();
736 bool FindAndReplaceWidget::hasWorkArea(GuiWorkArea * wa) const
738 return wa == find_work_area_ || wa == replace_work_area_;
741 } // namespace frontend
745 #include "moc_FindAndReplace.cpp"