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"
16 #include "GuiApplication.h"
18 #include "GuiWorkArea.h"
19 #include "qt_helpers.h"
22 #include "BufferParams.h"
23 #include "BufferList.h"
25 #include "FuncRequest.h"
28 #include "frontends/alert.h"
30 #include "support/debug.h"
31 #include "support/filetools.h"
32 #include "support/FileName.h"
33 #include "support/gettext.h"
34 #include "support/lassert.h"
35 #include "support/lstrings.h"
37 #include <QCloseEvent>
42 using namespace lyx::support;
48 /// Apply to buf the parameters supplied through bp
49 static void ApplyParams(Buffer &buf, BufferParams const & bp) {
51 ss << "\\begin_header\n";
53 ss << "\\end_header\n";
54 istringstream iss(ss.str());
57 int unknown_tokens = buf.readHeader(lex);
58 LASSERT(unknown_tokens == 0, /* */);
62 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
66 find_work_area_->setGuiView(view_);
67 find_work_area_->init();
68 find_work_area_->setFrameStyle(QFrame::StyledPanel);
70 setFocusProxy(find_work_area_);
71 replace_work_area_->setGuiView(view_);
72 replace_work_area_->init();
73 replace_work_area_->setFrameStyle(QFrame::StyledPanel);
75 // We don't want two cursors blinking.
76 find_work_area_->stopBlinkingCursor();
77 replace_work_area_->stopBlinkingCursor();
81 bool FindAndReplaceWidget::eventFilter(QObject * obj, QEvent * event)
83 if (event->type() != QEvent::KeyPress
84 || (obj != find_work_area_ && obj != replace_work_area_))
85 return QWidget::eventFilter(obj, event);
87 QKeyEvent * e = static_cast<QKeyEvent *> (event);
90 if (e->modifiers() == Qt::NoModifier) {
97 case Qt::Key_Return: {
98 // with shift we (temporarily) change search/replace direction
99 bool const searchback = searchbackCB->isChecked();
100 if (e->modifiers() == Qt::ShiftModifier && !searchback)
101 searchbackCB->setChecked(!searchback);
103 if (obj == find_work_area_)
104 on_findNextPB_clicked();
106 on_replacePB_clicked();
107 // back to how it was
108 searchbackCB->setChecked(searchback);
113 if (e->modifiers() == Qt::NoModifier) {
114 if (obj == find_work_area_){
115 LYXERR(Debug::FIND, "Focusing replace WA");
116 replace_work_area_->setFocus();
117 LYXERR(Debug::FIND, "Selecting entire replace buffer");
118 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
119 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
125 case Qt::Key_Backtab:
126 if (obj == replace_work_area_) {
127 LYXERR(Debug::FIND, "Focusing find WA");
128 find_work_area_->setFocus();
129 LYXERR(Debug::FIND, "Selecting entire find buffer");
130 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
131 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
139 // standard event processing
140 return QWidget::eventFilter(obj, event);
144 static vector<string> const & allManualsFiles()
146 static vector<string> v;
147 static const char * files[] = {
148 "Intro", "UserGuide", "Tutorial", "Additional",
149 "EmbeddedObjects", "Math", "Customization", "Shortcuts",
150 "LFUNs", "LaTeXConfig"
154 for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); ++i) {
155 fname = i18nLibFileSearch("doc", files[i], "lyx");
156 v.push_back(fname.absFileName());
163 /** Switch p_buf to point to next document buffer.
165 ** Return true if restarted from master-document buffer.
167 static bool nextDocumentBuffer(Buffer * & buf)
169 ListOfBuffers const children = buf->allRelatives();
170 LYXERR(Debug::FIND, "children.size()=" << children.size());
171 ListOfBuffers::const_iterator it =
172 find(children.begin(), children.end(), buf);
173 LASSERT(it != children.end(), /**/);
175 if (it == children.end()) {
176 buf = *children.begin();
184 /** Switch p_buf to point to previous document buffer.
186 ** Return true if restarted from last child buffer.
188 static bool prevDocumentBuffer(Buffer * & buf)
190 ListOfBuffers const children = buf->allRelatives();
191 LYXERR(Debug::FIND, "children.size()=" << children.size());
192 ListOfBuffers::const_iterator it =
193 find(children.begin(), children.end(), buf);
194 LASSERT(it != children.end(), /**/)
195 if (it == children.begin()) {
207 /** Switch buf to point to next or previous buffer in search scope.
209 ** Return true if restarted from scratch.
211 static bool nextPrevBuffer(Buffer * & buf,
212 FindAndReplaceOptions const & opt)
214 bool restarted = false;
216 case FindAndReplaceOptions::S_BUFFER:
219 case FindAndReplaceOptions::S_DOCUMENT:
221 restarted = nextDocumentBuffer(buf);
223 restarted = prevDocumentBuffer(buf);
225 case FindAndReplaceOptions::S_OPEN_BUFFERS:
227 buf = theBufferList().next(buf);
228 restarted = (buf == *theBufferList().begin());
230 buf = theBufferList().previous(buf);
231 restarted = (buf == *(theBufferList().end() - 1));
234 case FindAndReplaceOptions::S_ALL_MANUALS:
235 vector<string> const & manuals = allManualsFiles();
236 vector<string>::const_iterator it =
237 find(manuals.begin(), manuals.end(), buf->absFileName());
238 if (it == manuals.end())
239 it = manuals.begin();
240 else if (opt.forward) {
242 if (it == manuals.end()) {
243 it = manuals.begin();
247 if (it == manuals.begin()) {
253 FileName const & fname = FileName(*it);
254 if (!theBufferList().exists(fname)) {
255 guiApp->currentView()->setBusy(false);
256 guiApp->currentView()->loadDocument(fname, false);
257 guiApp->currentView()->setBusy(true);
259 buf = theBufferList().getBuffer(fname);
266 /** Find the finest question message to post to the user */
267 docstring getQuestionString(FindAndReplaceOptions const & opt)
271 case FindAndReplaceOptions::S_BUFFER:
274 case FindAndReplaceOptions::S_DOCUMENT:
275 scope = _("Master document");
277 case FindAndReplaceOptions::S_OPEN_BUFFERS:
278 scope = _("Open files");
280 case FindAndReplaceOptions::S_ALL_MANUALS:
281 scope = _("Manuals");
284 docstring message = opt.forward ?
285 bformat(_("%1$s: the end was reached while searching forward.\n"
286 "Continue searching from the beginning?"),
288 bformat(_("%1$s: the beginning was reached while searching backward.\n"
289 "Continue searching from the end?"),
296 /// Return true if a match was found
297 bool FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt, bool replace_all)
299 BufferView * bv = view_.documentBufferView();
302 Buffer * buf = &bv->buffer();
303 Buffer * buf_orig = &bv->buffer();
304 DocIterator cur_orig(bv->cursor());
305 int wrap_answer = -1;
308 FuncRequest cmd(LFUN_WORD_FINDADV, from_utf8(oss.str()));
311 if (opt.scope == FindAndReplaceOptions::S_ALL_MANUALS) {
312 vector<string> const & v = allManualsFiles();
313 if (std::find(v.begin(), v.end(), buf->absFileName()) == v.end()) {
314 FileName const & fname = FileName(*v.begin());
315 if (!theBufferList().exists(fname)) {
316 guiApp->currentView()->setBusy(false);
317 guiApp->currentView()->loadDocument(fname, false);
318 guiApp->currentView()->setBusy(true);
320 buf = theBufferList().getBuffer(fname);
321 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
322 buf->absFileName()));
323 bv = view_.documentBufferView();
324 bv->cursor().clear();
325 bv->cursor().push_back(CursorSlice(buf->inset()));
330 LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
332 LYXERR(Debug::FIND, "dispatched");
333 if (bv->cursor().result().dispatched()) {
334 // New match found and selected (old selection replaced if needed)
337 view_.setBusy(false);
341 // No match found in current buffer (however old selection might have been replaced)
342 // select next buffer in scope, if any
343 bool prompt = nextPrevBuffer(buf, opt);
345 if (wrap_answer != -1)
347 docstring q = getQuestionString(opt);
348 view_.setBusy(false);
349 wrap_answer = frontend::Alert::prompt(
350 _("Wrap search?"), q,
351 0, 1, _("&Yes"), _("&No"));
353 if (wrap_answer == 1)
356 if (buf != &view_.documentBufferView()->buffer())
357 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
358 buf->absFileName()));
359 bv = view_.documentBufferView();
361 bv->cursor().clear();
362 bv->cursor().push_back(CursorSlice(buf->inset()));
364 //lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
365 bv->cursor().setCursor(doc_iterator_end(buf));
366 bv->cursor().backwardPos();
367 LYXERR(Debug::FIND, "findBackAdv5: cur: "
370 bv->clearSelection();
371 } while (wrap_answer != 1);
372 if (buf_orig != &view_.documentBufferView()->buffer())
373 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
374 buf_orig->absFileName()));
375 bv = view_.documentBufferView();
376 // This may happen after a replace occurred
377 if (cur_orig.pos() > cur_orig.lastpos())
378 cur_orig.pos() = cur_orig.lastpos();
379 bv->cursor().setCursor(cur_orig);
380 view_.setBusy(false);
385 /// Return true if a match was found
386 bool FindAndReplaceWidget::findAndReplace(
387 bool casesensitive, bool matchword, bool backwards,
388 bool expandmacros, bool ignoreformat, bool replace,
389 bool keep_case, bool replace_all)
391 Buffer & find_buf = find_work_area_->bufferView().buffer();
392 docstring const & find_buf_name = find_buf.fileName().absoluteFilePath();
394 if (find_buf.text().empty()) {
395 view_.message(_("Nothing to search"));
399 Buffer & repl_buf = replace_work_area_->bufferView().buffer();
400 docstring const & repl_buf_name = replace ?
401 repl_buf.fileName().absoluteFilePath() : docstring();
403 FindAndReplaceOptions::SearchScope scope =
404 FindAndReplaceOptions::S_BUFFER;
405 if (CurrentDocument->isChecked())
406 scope = FindAndReplaceOptions::S_BUFFER;
407 else if (MasterDocument->isChecked())
408 scope = FindAndReplaceOptions::S_DOCUMENT;
409 else if (OpenDocuments->isChecked())
410 scope = FindAndReplaceOptions::S_OPEN_BUFFERS;
411 else if (AllManualsRB->isChecked())
412 scope = FindAndReplaceOptions::S_ALL_MANUALS;
414 LASSERT(false, /**/);
415 LYXERR(Debug::FIND, "FindAndReplaceOptions: "
416 << "find_buf_name=" << find_buf_name
417 << ", casesensitiv=" << casesensitive
418 << ", matchword=" << matchword
419 << ", backwards=" << backwards
420 << ", expandmacros=" << expandmacros
421 << ", ignoreformat=" << ignoreformat
422 << ", repl_buf_name" << repl_buf_name
423 << ", keep_case=" << keep_case
424 << ", scope=" << scope);
425 FindAndReplaceOptions opt(find_buf_name, casesensitive, matchword,
426 !backwards, expandmacros, ignoreformat,
427 repl_buf_name, keep_case, scope);
428 return findAndReplaceScope(opt, replace_all);
432 bool FindAndReplaceWidget::findAndReplace(bool backwards, bool replace, bool replace_all)
434 if (! view_.currentMainWorkArea()) {
435 view_.message(_("No open document(s) in which to search"));
438 // Finalize macros that are being typed, both in main document and in search or replacement WAs
439 if (view_.currentWorkArea()->bufferView().cursor().macroModeClose())
440 view_.currentWorkArea()->bufferView().processUpdateFlags(Update::Force);
441 if (view_.currentMainWorkArea()->bufferView().cursor().macroModeClose())
442 view_.currentMainWorkArea()->bufferView().processUpdateFlags(Update::Force);
444 // FIXME: create a Dialog::returnFocus()
445 // or something instead of this:
446 view_.setCurrentWorkArea(view_.currentMainWorkArea());
447 return findAndReplace(caseCB->isChecked(),
448 wordsCB->isChecked(),
450 expandMacrosCB->isChecked(),
451 ignoreFormatCB->isChecked(),
453 keepCaseCB->isChecked(),
458 void FindAndReplaceWidget::hideDialog()
460 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
464 void FindAndReplaceWidget::on_findNextPB_clicked()
466 findAndReplace(searchbackCB->isChecked(), false);
467 find_work_area_->setFocus();
471 void FindAndReplaceWidget::on_replacePB_clicked()
473 findAndReplace(searchbackCB->isChecked(), true);
474 replace_work_area_->setFocus();
478 void FindAndReplaceWidget::on_replaceallPB_clicked()
480 findAndReplace(searchbackCB->isChecked(), true, true);
481 replace_work_area_->setFocus();
485 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
487 LYXERR(Debug::DEBUG, "showEvent()" << endl);
488 BufferView * bv = view_.documentBufferView();
490 Buffer & doc_buf = bv->buffer();
491 BufferParams & doc_bp = doc_buf.params();
492 Buffer & find_buf = find_work_area_->bufferView().buffer();
493 LYXERR(Debug::FIND, "Applying document params to find buffer");
494 ApplyParams(find_buf, doc_bp);
495 Buffer & replace_buf = replace_work_area_->bufferView().buffer();
496 LYXERR(Debug::FIND, "Applying document params to replace buffer");
497 ApplyParams(replace_buf, doc_bp);
499 string lang = doc_bp.language->lang();
500 LYXERR(Debug::FIND, "Setting current editing language to " << lang << endl);
501 FuncRequest cmd(LFUN_LANGUAGE, lang);
502 find_buf.text().dispatch(find_work_area_->bufferView().cursor(), cmd);
503 replace_buf.text().dispatch(replace_work_area_->bufferView().cursor(), cmd);
506 find_work_area_->installEventFilter(this);
507 replace_work_area_->installEventFilter(this);
509 view_.setCurrentWorkArea(find_work_area_);
510 LYXERR(Debug::FIND, "Selecting entire find buffer");
511 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
512 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
516 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
518 replace_work_area_->removeEventFilter(this);
519 find_work_area_->removeEventFilter(this);
520 this->QWidget::hideEvent(ev);
524 bool FindAndReplaceWidget::initialiseParams(std::string const & /*params*/)
530 FindAndReplace::FindAndReplace(GuiView & parent,
531 Qt::DockWidgetArea area, Qt::WindowFlags flags)
532 : DockView(parent, "findreplaceadv", qt_("Advanced Find and Replace"),
535 widget_ = new FindAndReplaceWidget(parent);
537 setFocusProxy(widget_);
541 FindAndReplace::~FindAndReplace()
548 bool FindAndReplace::initialiseParams(std::string const & params)
550 return widget_->initialiseParams(params);
554 Dialog * createGuiSearchAdv(GuiView & lv)
556 FindAndReplace * gui = new FindAndReplace(lv, Qt::RightDockWidgetArea);
558 // On Mac show and floating
559 gui->setFloating(true);
565 } // namespace frontend
569 #include "moc_FindAndReplace.cpp"