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 "buffer_funcs.h"
23 #include "BufferParams.h"
24 #include "BufferList.h"
26 #include "FuncRequest.h"
28 #include "output_latex.h"
29 #include "OutputParams.h"
32 #include "frontends/alert.h"
34 #include "support/debug.h"
35 #include "support/filetools.h"
36 #include "support/FileName.h"
37 #include "support/gettext.h"
38 #include "support/lassert.h"
39 #include "support/lstrings.h"
41 #include <QCloseEvent>
48 using namespace lyx::support;
54 /// Apply to buf the parameters supplied through bp
55 static void ApplyParams(Buffer &buf, BufferParams const & bp) {
57 ss << "\\begin_header\n";
59 ss << "\\end_header\n";
60 istringstream iss(ss.str());
63 int unknown_tokens = buf.readHeader(lex);
64 LASSERT(unknown_tokens == 0, /* */);
68 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
72 find_work_area_->setGuiView(view_);
73 find_work_area_->init();
74 find_work_area_->setFrameStyle(QFrame::StyledPanel);
76 setFocusProxy(find_work_area_);
77 replace_work_area_->setGuiView(view_);
78 replace_work_area_->init();
79 replace_work_area_->setFrameStyle(QFrame::StyledPanel);
81 // We don't want two cursors blinking.
82 find_work_area_->stopBlinkingCursor();
83 replace_work_area_->stopBlinkingCursor();
87 bool FindAndReplaceWidget::eventFilter(QObject * obj, QEvent * event)
89 if (event->type() != QEvent::KeyPress
90 || (obj != find_work_area_ && obj != replace_work_area_))
91 return QWidget::eventFilter(obj, event);
93 QKeyEvent * e = static_cast<QKeyEvent *> (event);
96 if (e->modifiers() == Qt::NoModifier) {
103 case Qt::Key_Return: {
104 // with shift we (temporarily) change search/replace direction
105 bool const searchback = searchbackCB->isChecked();
106 if (e->modifiers() == Qt::ShiftModifier && !searchback)
107 searchbackCB->setChecked(!searchback);
109 if (obj == find_work_area_)
110 on_findNextPB_clicked();
112 on_replacePB_clicked();
113 // back to how it was
114 searchbackCB->setChecked(searchback);
119 if (e->modifiers() == Qt::NoModifier) {
120 if (obj == find_work_area_){
121 LYXERR(Debug::FIND, "Focusing replace WA");
122 replace_work_area_->setFocus();
123 LYXERR(Debug::FIND, "Selecting entire replace buffer");
124 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
125 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
131 case Qt::Key_Backtab:
132 if (obj == replace_work_area_) {
133 LYXERR(Debug::FIND, "Focusing find WA");
134 find_work_area_->setFocus();
135 LYXERR(Debug::FIND, "Selecting entire find buffer");
136 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
137 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
145 // standard event processing
146 return QWidget::eventFilter(obj, event);
150 static docstring buffer_to_latex(Buffer & buffer)
152 OutputParams runparams(&buffer.params().encoding());
153 odocstringstream ods;
155 runparams.nice = true;
156 runparams.flavor = OutputParams::LATEX;
157 runparams.linelen = 80; //lyxrc.plaintext_linelen;
158 // No side effect of file copying and image conversion
159 runparams.dryrun = true;
160 buffer.texrow().reset();
161 pit_type const endpit = buffer.paragraphs().size();
162 for (pit_type pit = 0; pit != endpit; ++pit) {
163 TeXOnePar(buffer, buffer.text(),
164 pit, os, buffer.texrow(), runparams);
165 LYXERR(Debug::FIND, "searchString up to here: " << ods.str());
171 static vector<string> const & allManualsFiles()
173 static vector<string> v;
174 static const char * files[] = {
175 "Intro", "UserGuide", "Tutorial", "Additional",
176 "EmbeddedObjects", "Math", "Customization", "Shortcuts",
177 "LFUNs", "LaTeXConfig"
181 for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); ++i) {
182 fname = i18nLibFileSearch("doc", files[i], "lyx");
183 v.push_back(fname.absFileName());
190 /** Switch p_buf to point to next document buffer.
192 ** Return true if restarted from master-document buffer.
194 static bool nextDocumentBuffer(Buffer * & buf)
196 ListOfBuffers const children = buf->allRelatives();
197 LYXERR(Debug::FIND, "children.size()=" << children.size());
198 ListOfBuffers::const_iterator it =
199 find(children.begin(), children.end(), buf);
200 LASSERT(it != children.end(), /**/)
202 if (it == children.end()) {
203 buf = *children.begin();
211 /** Switch p_buf to point to previous document buffer.
213 ** Return true if restarted from last child buffer.
215 static bool prevDocumentBuffer(Buffer * & buf)
217 ListOfBuffers const children = buf->allRelatives();
218 LYXERR(Debug::FIND, "children.size()=" << children.size());
219 ListOfBuffers::const_iterator it =
220 find(children.begin(), children.end(), buf);
221 LASSERT(it != children.end(), /**/)
222 if (it == children.begin()) {
234 /** Switch buf to point to next or previous buffer in search scope.
236 ** Return true if restarted from scratch.
238 static bool nextPrevBuffer(Buffer * & buf,
239 FindAndReplaceOptions const & opt)
241 bool restarted = false;
243 case FindAndReplaceOptions::S_BUFFER:
246 case FindAndReplaceOptions::S_DOCUMENT:
248 restarted = nextDocumentBuffer(buf);
250 restarted = prevDocumentBuffer(buf);
252 case FindAndReplaceOptions::S_OPEN_BUFFERS:
254 buf = theBufferList().next(buf);
255 restarted = (buf == *theBufferList().begin());
257 buf = theBufferList().previous(buf);
258 restarted = (buf == *(theBufferList().end() - 1));
261 case FindAndReplaceOptions::S_ALL_MANUALS:
262 vector<string> const & manuals = allManualsFiles();
263 vector<string>::const_iterator it =
264 find(manuals.begin(), manuals.end(), buf->absFileName());
265 if (it == manuals.end())
266 it = manuals.begin();
267 else if (opt.forward) {
269 if (it == manuals.end()) {
270 it = manuals.begin();
274 if (it == manuals.begin()) {
280 FileName const & fname = FileName(*it);
281 if (!theBufferList().exists(fname)) {
282 guiApp->currentView()->setBusy(false);
283 guiApp->currentView()->loadDocument(fname, false);
284 guiApp->currentView()->setBusy(true);
286 buf = theBufferList().getBuffer(fname);
293 /** Find the finest question message to post to the user */
294 docstring getQuestionString(FindAndReplaceOptions const & opt)
298 case FindAndReplaceOptions::S_BUFFER:
301 case FindAndReplaceOptions::S_DOCUMENT:
302 scope = _("Master document");
304 case FindAndReplaceOptions::S_OPEN_BUFFERS:
305 scope = _("Open files");
307 case FindAndReplaceOptions::S_ALL_MANUALS:
308 scope = _("Manuals");
311 docstring message = opt.forward ?
312 bformat(_("%1$s: the end was reached while searching forward.\n"
313 "Continue searching from the beginning?"),
315 bformat(_("%1$s: the beginning was reached while searching backward.\n"
316 "Continue searching from the end?"),
323 void FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt)
325 BufferView * bv = view_.documentBufferView();
328 Buffer * buf = &bv->buffer();
329 Buffer * buf_orig = &bv->buffer();
330 DocIterator cur_orig(bv->cursor());
331 int wrap_answer = -1;
334 FuncRequest cmd(LFUN_WORD_FINDADV, from_utf8(oss.str()));
337 if (opt.scope == FindAndReplaceOptions::S_ALL_MANUALS) {
338 vector<string> const & v = allManualsFiles();
339 if (std::find(v.begin(), v.end(), buf->absFileName()) == v.end()) {
340 FileName const & fname = FileName(*v.begin());
341 if (!theBufferList().exists(fname)) {
342 guiApp->currentView()->setBusy(false);
343 guiApp->currentView()->loadDocument(fname, false);
344 guiApp->currentView()->setBusy(true);
346 buf = theBufferList().getBuffer(fname);
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()));
356 LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
358 LYXERR(Debug::FIND, "dispatched");
359 if (bv->cursor().result().dispatched()) {
360 // New match found and selected (old selection replaced if needed)
361 view_.setBusy(false);
365 // No match found in current buffer (however old selection might have been replaced)
366 // select next buffer in scope, if any
367 bool prompt = nextPrevBuffer(buf, opt);
369 if (wrap_answer != -1)
371 docstring q = getQuestionString(opt);
372 view_.setBusy(false);
373 wrap_answer = frontend::Alert::prompt(
374 _("Wrap search?"), q,
375 0, 1, _("&Yes"), _("&No"));
377 if (wrap_answer == 1)
380 if (buf != &view_.documentBufferView()->buffer())
381 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
382 buf->absFileName()));
383 bv = view_.documentBufferView();
385 bv->cursor().clear();
386 bv->cursor().push_back(CursorSlice(buf->inset()));
388 //lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
389 bv->cursor().setCursor(doc_iterator_end(buf));
390 bv->cursor().backwardPos();
391 LYXERR(Debug::FIND, "findBackAdv5: cur: "
394 bv->clearSelection();
395 } while (wrap_answer != 1);
396 if (buf_orig != &view_.documentBufferView()->buffer())
397 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
398 buf_orig->absFileName()));
399 bv = view_.documentBufferView();
400 // This may happen after a replace occurred
401 if (cur_orig.pos() > cur_orig.lastpos())
402 cur_orig.pos() = cur_orig.lastpos();
403 bv->cursor().setCursor(cur_orig);
404 view_.setBusy(false);
408 void FindAndReplaceWidget::findAndReplace(
409 bool casesensitive, bool matchword, bool backwards,
410 bool expandmacros, bool ignoreformat, bool replace,
413 Buffer & buffer = find_work_area_->bufferView().buffer();
414 docstring searchString;
416 searchString = buffer_to_latex(buffer);
418 ParIterator it = buffer.par_iterator_begin();
419 ParIterator end = buffer.par_iterator_end();
420 OutputParams runparams(&buffer.params().encoding());
422 runparams.nice = true;
423 runparams.flavor = OutputParams::LATEX;
424 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
425 runparams.dryrun = true;
426 for (; it != end; ++it) {
427 LYXERR(Debug::FIND, "Adding to search string: '"
428 << it->asString(false)
431 it->stringify(pos_type(0), it->size(),
432 AS_STR_INSETS, runparams);
435 if (to_utf8(searchString).empty()) {
436 buffer.message(_("Nothing to search"));
440 to_utf8(searchString).find("\\regexp") != std::string::npos;
441 docstring replaceString;
443 Buffer & repl_buffer =
444 replace_work_area_->bufferView().buffer();
446 repl_buffer.write(oss);
447 //buffer_to_latex(replace_buffer);
448 replaceString = from_utf8(oss.str());
450 replaceString = from_utf8(LYX_FR_NULL_STRING);
452 FindAndReplaceOptions::SearchScope scope =
453 FindAndReplaceOptions::S_BUFFER;
454 if (CurrentDocument->isChecked())
455 scope = FindAndReplaceOptions::S_BUFFER;
456 else if (MasterDocument->isChecked())
457 scope = FindAndReplaceOptions::S_DOCUMENT;
458 else if (OpenDocuments->isChecked())
459 scope = FindAndReplaceOptions::S_OPEN_BUFFERS;
460 else if (AllManualsRB->isChecked())
461 scope = FindAndReplaceOptions::S_ALL_MANUALS;
463 LASSERT(false, /**/);
464 LYXERR(Debug::FIND, "FindAndReplaceOptions: "
465 << "searchstring=" << searchString
466 << ", casesensitiv=" << casesensitive
467 << ", matchword=" << matchword
468 << ", backwards=" << backwards
469 << ", expandmacros=" << expandmacros
470 << ", ignoreformat=" << ignoreformat
471 << ", regexp=" << regexp
472 << ", replaceString" << replaceString
473 << ", keep_case=" << keep_case
474 << ", scope=" << scope);
475 FindAndReplaceOptions opt(searchString, casesensitive, matchword,
476 !backwards, expandmacros, ignoreformat,
477 regexp, replaceString, keep_case, scope);
478 findAndReplaceScope(opt);
482 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
484 if (! view_.currentMainWorkArea()) {
485 view_.message(_("No open document(s) in which to search"));
488 // Finalize macros that are being typed, both in main document and in search or replacement WAs
489 if (view_.currentWorkArea()->bufferView().cursor().macroModeClose())
490 view_.currentWorkArea()->bufferView().processUpdateFlags(Update::Force);
491 if (view_.currentMainWorkArea()->bufferView().cursor().macroModeClose())
492 view_.currentMainWorkArea()->bufferView().processUpdateFlags(Update::Force);
494 // FIXME: create a Dialog::returnFocus()
495 // or something instead of this:
496 view_.setCurrentWorkArea(view_.currentMainWorkArea());
497 findAndReplace(caseCB->isChecked(),
498 wordsCB->isChecked(),
500 expandMacrosCB->isChecked(),
501 ignoreFormatCB->isChecked(),
503 keepCaseCB->isChecked());
507 void FindAndReplaceWidget::hideDialog()
509 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
513 void FindAndReplaceWidget::on_findNextPB_clicked()
515 findAndReplace(searchbackCB->isChecked(), false);
516 find_work_area_->setFocus();
520 void FindAndReplaceWidget::on_replacePB_clicked()
522 findAndReplace(searchbackCB->isChecked(), true);
523 replace_work_area_->setFocus();
527 void FindAndReplaceWidget::on_replaceallPB_clicked()
529 replace_work_area_->setFocus();
533 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
535 LYXERR(Debug::DEBUG, "showEvent()" << endl);
536 BufferView * bv = view_.documentBufferView();
538 Buffer & doc_buf = bv->buffer();
539 BufferParams & doc_bp = doc_buf.params();
540 Buffer & find_buf = find_work_area_->bufferView().buffer();
541 LYXERR(Debug::FIND, "Applying document params to find buffer");
542 ApplyParams(find_buf, doc_bp);
543 Buffer & replace_buf = replace_work_area_->bufferView().buffer();
544 LYXERR(Debug::FIND, "Applying document params to replace buffer");
545 ApplyParams(replace_buf, doc_bp);
547 string lang = doc_bp.language->lang();
548 LYXERR(Debug::FIND, "Setting current editing language to " << lang << endl);
549 FuncRequest cmd(LFUN_LANGUAGE, lang);
550 find_buf.text().dispatch(find_work_area_->bufferView().cursor(), cmd);
551 replace_buf.text().dispatch(replace_work_area_->bufferView().cursor(), cmd);
554 find_work_area_->installEventFilter(this);
555 replace_work_area_->installEventFilter(this);
557 view_.setCurrentWorkArea(find_work_area_);
558 LYXERR(Debug::FIND, "Selecting entire find buffer");
559 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
560 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
564 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
566 replace_work_area_->removeEventFilter(this);
567 find_work_area_->removeEventFilter(this);
568 this->QWidget::hideEvent(ev);
572 bool FindAndReplaceWidget::initialiseParams(std::string const & /*params*/)
578 FindAndReplace::FindAndReplace(GuiView & parent,
579 Qt::DockWidgetArea area, Qt::WindowFlags flags)
580 : DockView(parent, "findreplaceadv", qt_("Advanced Find and Replace"),
583 widget_ = new FindAndReplaceWidget(parent);
585 setFocusProxy(widget_);
589 FindAndReplace::~FindAndReplace()
596 bool FindAndReplace::initialiseParams(std::string const & params)
598 return widget_->initialiseParams(params);
602 Dialog * createGuiSearchAdv(GuiView & lv)
604 FindAndReplace * gui = new FindAndReplace(lv, Qt::RightDockWidgetArea);
606 // On Mac show and floating
607 gui->setFloating(true);
613 } // namespace frontend
617 #include "moc_FindAndReplace.cpp"