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();
65 QMenu * menu = new QMenu();
66 QAction * regAny = menu->addAction(qt_("&Anything"));
67 regAny->setData(".*");
68 QAction * regAnyNonEmpty = menu->addAction(qt_("Any non-&empty"));
69 regAnyNonEmpty->setData(".+");
70 QAction * regAnyWord = menu->addAction(qt_("Any &word"));
71 regAnyWord->setData("[a-z]+");
72 QAction * regAnyNumber = menu->addAction(qt_("Any &number"));
73 regAnyNumber->setData("[0-9]+");
74 QAction * regCustom = menu->addAction(qt_("&User-defined"));
75 regCustom->setData("");
76 regexpInsertPB->setMenu(menu);
78 connect(menu, SIGNAL(triggered(QAction *)),
79 this, SLOT(insertRegexp(QAction *)));
83 bool FindAndReplaceWidget::eventFilter(QObject * obj, QEvent * event)
85 if (event->type() != QEvent::KeyPress
86 || (obj != find_work_area_ && obj != replace_work_area_))
87 return QWidget::eventFilter(obj, event);
89 QKeyEvent * e = static_cast<QKeyEvent *> (event);
92 if (e->modifiers() == Qt::NoModifier) {
99 case Qt::Key_Return: {
100 // with shift we (temporarily) change search/replace direction
101 bool const searchback = searchbackCB->isChecked();
102 if (e->modifiers() == Qt::ShiftModifier && !searchback)
103 searchbackCB->setChecked(!searchback);
105 if (obj == find_work_area_)
106 on_findNextPB_clicked();
108 on_replacePB_clicked();
109 // back to how it was
110 searchbackCB->setChecked(searchback);
115 if (e->modifiers() == Qt::NoModifier) {
116 if (obj == find_work_area_){
117 LYXERR(Debug::FIND, "Focusing replace WA");
118 replace_work_area_->setFocus();
124 case Qt::Key_Backtab:
125 if (obj == replace_work_area_) {
126 LYXERR(Debug::FIND, "Focusing find WA");
127 find_work_area_->setFocus();
135 // standard event processing
136 return QWidget::eventFilter(obj, event);
140 static docstring buffer_to_latex(Buffer & buffer) {
141 OutputParams runparams(&buffer.params().encoding());
143 runparams.nice = true;
144 runparams.flavor = OutputParams::LATEX;
145 runparams.linelen = 80; //lyxrc.plaintext_linelen;
146 // No side effect of file copying and image conversion
147 runparams.dryrun = true;
148 buffer.texrow().reset();
149 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
150 ParagraphList::const_iterator const end = buffer.paragraphs().end();
151 for (; pit != end; ++pit) {
152 TeXOnePar(buffer, buffer.text(),
153 pit, os, buffer.texrow(), runparams);
154 LYXERR(Debug::FIND, "searchString up to here: "
161 static vector<string> const & allManualsFiles() {
162 static vector<string> v;
163 static const char * files[] = {
164 "Intro", "UserGuide", "Tutorial", "Additional",
165 "EmbeddedObjects", "Math", "Customization", "Shortcuts",
166 "LFUNs", "LaTeXConfig"
170 for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); ++i) {
171 fname = i18nLibFileSearch("doc", files[i], "lyx");
172 v.push_back(fname.absFilename());
179 /** Switch p_buf to point to next document buffer.
181 ** Return true if restarted from master-document buffer.
184 ** Not using p_buf->allRelatives() here, because I'm not sure
185 ** whether or not the returned order is independent of p_buf.
187 static bool next_document_buffer(Buffer * & p_buf) {
188 Buffer *p_master = p_buf;
192 p_master = const_cast<Buffer *>(p_master->masterBuffer());
193 LYXERR(Debug::FIND, "p_old="
197 } while (p_master != p_old);
198 LASSERT(p_master != NULL, /**/);
199 vector<Buffer *> v_children;
200 /* Root master added as first buffer in the vector */
201 v_children.push_back(p_master);
202 p_master->getChildren(v_children, true);
203 LYXERR(Debug::FIND, "v_children.size()=" << v_children.size());
204 vector<Buffer *>::const_iterator it =
205 find(v_children.begin(), v_children.end(), p_buf);
206 LASSERT(it != v_children.end(), /**/)
208 if (it == v_children.end()) {
209 p_buf = *v_children.begin();
217 /** Switch p_buf to point to previous document buffer.
219 ** Return true if restarted from last child buffer.
222 ** Not using p_buf->allRelatives() here, because I'm not sure
223 ** whether or not the returned order is independent of p_buf.
225 static bool prev_document_buffer(Buffer * & p_buf) {
226 Buffer *p_master = p_buf;
230 p_master = const_cast<Buffer *>(p_master->masterBuffer());
233 << ", p_master=" << p_master);
234 } while (p_master != p_old);
235 LASSERT(p_master != NULL, /**/);
236 vector<Buffer *> v_children;
237 /* Root master added as first buffer in the vector */
238 v_children.push_back(p_master);
239 p_master->getChildren(v_children, true);
240 LYXERR(Debug::FIND, "v_children.size()=" << v_children.size());
241 vector<Buffer *>::const_iterator it =
242 find(v_children.begin(), v_children.end(), p_buf);
243 LASSERT(it != v_children.end(), /**/)
244 if (it == v_children.begin()) {
245 it = v_children.end();
256 /** Switch buf to point to next or previous buffer in search scope.
258 ** Return true if restarted from scratch.
260 static bool next_prev_buffer(Buffer * & buf,
261 FindAndReplaceOptions const & opt)
263 bool restarted = false;
265 case FindAndReplaceOptions::S_BUFFER:
268 case FindAndReplaceOptions::S_DOCUMENT:
270 restarted = next_document_buffer(buf);
272 restarted = prev_document_buffer(buf);
274 case FindAndReplaceOptions::S_OPEN_BUFFERS:
276 buf = theBufferList().next(buf);
277 restarted = buf == *theBufferList().begin();
279 buf = theBufferList().previous(buf);
280 restarted = buf == *(theBufferList().end() - 1);
283 case FindAndReplaceOptions::S_ALL_MANUALS:
284 vector<string> const & v = allManualsFiles();
285 vector<string>::const_iterator it =
286 find(v.begin(), v.end(), buf->absFileName());
289 } else if (opt.forward) {
296 if (it == v.begin()) {
302 FileName const & fname = FileName(*it);
303 if (!theBufferList().exists(fname)) {
304 guiApp->currentView()->setBusy(false);
305 guiApp->currentView()->loadDocument(fname, false);
306 guiApp->currentView()->setBusy(true);
308 buf = theBufferList().getBuffer(fname);
315 /** Find the finest question message to post to the user */
316 docstring question_string(FindAndReplaceOptions const & opt)
320 case FindAndReplaceOptions::S_BUFFER:
321 scope = _("file[[scope]]");
323 case FindAndReplaceOptions::S_DOCUMENT:
324 scope = _("master document[[scope]]");
326 case FindAndReplaceOptions::S_OPEN_BUFFERS:
327 scope = _("open files[[scope]]");
329 case FindAndReplaceOptions::S_ALL_MANUALS:
330 scope = _("manuals[[scope]]");
333 docstring message = opt.forward ?
334 bformat(_("End of %1$s reached while searching forward.\n"
335 "Continue searching from the beginning?"),
337 bformat(_("Beginning of %1$s reached while searching backward.\n"
338 "Continue searching from the end?"),
345 void FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt)
347 int wrap_answer = -1;
350 FuncRequest cmd(LFUN_WORD_FINDADV, from_utf8(oss.str()));
351 BufferView * bv = view_.documentBufferView();
352 Buffer * buf = &bv->buffer();
354 Buffer * buf_orig = &bv->buffer();
355 DocIterator cur_orig(bv->cursor());
357 if (opt.scope == FindAndReplaceOptions::S_ALL_MANUALS) {
358 vector<string> const & v = allManualsFiles();
359 if (std::find(v.begin(), v.end(), buf->absFileName()) == v.end()) {
360 FileName const & fname = FileName(*v.begin());
361 if (!theBufferList().exists(fname)) {
362 guiApp->currentView()->setBusy(false);
363 guiApp->currentView()->loadDocument(fname, false);
364 guiApp->currentView()->setBusy(true);
366 buf = theBufferList().getBuffer(fname);
367 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
368 buf->absFileName()));
369 bv = view_.documentBufferView();
370 bv->cursor().clear();
371 bv->cursor().push_back(CursorSlice(buf->inset()));
376 LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
378 LYXERR(Debug::FIND, "dispatched");
379 if (bv->cursor().result().dispatched()) {
380 // Match found, selected and replaced if needed
384 // No match found in current buffer:
385 // select next buffer in scope, if any
386 bool prompt = next_prev_buffer(buf, opt);
388 if (wrap_answer != -1)
390 docstring q = question_string(opt);
391 wrap_answer = frontend::Alert::prompt(
392 _("Wrap search?"), q,
393 0, 1, _("&Yes"), _("&No"));
394 if (wrap_answer == 1)
397 if (buf != &view_.documentBufferView()->buffer())
398 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
399 buf->absFileName()));
400 bv = view_.documentBufferView();
402 bv->cursor().clear();
403 bv->cursor().push_back(CursorSlice(buf->inset()));
405 //lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
406 bv->cursor().setCursor(doc_iterator_end(buf));
407 bv->cursor().backwardPos();
408 LYXERR(Debug::FIND, "findBackAdv5: cur: "
411 bv->clearSelection();
412 } while (wrap_answer != 1);
413 if (buf_orig != &view_.documentBufferView()->buffer())
414 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
415 buf_orig->absFileName()));
416 bv = view_.documentBufferView();
417 bv->cursor().setCursor(cur_orig);
421 void FindAndReplaceWidget::findAndReplace(
422 bool casesensitive, bool matchword, bool backwards,
423 bool expandmacros, bool ignoreformat, bool replace,
426 Buffer & buffer = find_work_area_->bufferView().buffer();
427 docstring searchString;
429 searchString = buffer_to_latex(buffer);
431 ParIterator it = buffer.par_iterator_begin();
432 ParIterator end = buffer.par_iterator_end();
433 OutputParams runparams(&buffer.params().encoding());
435 runparams.nice = true;
436 runparams.flavor = OutputParams::LATEX;
437 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
438 runparams.dryrun = true;
439 for (; it != end; ++it) {
440 LYXERR(Debug::FIND, "Adding to search string: '"
441 << it->asString(false)
444 it->stringify(pos_type(0), it->size(),
445 AS_STR_INSETS, runparams);
448 if (to_utf8(searchString).empty()) {
449 buffer.message(_("Nothing to search"));
453 to_utf8(searchString).find("\\regexp") != std::string::npos;
454 docstring replaceString;
456 Buffer & repl_buffer =
457 replace_work_area_->bufferView().buffer();
459 repl_buffer.write(oss);
460 //buffer_to_latex(replace_buffer);
461 replaceString = from_utf8(oss.str());
463 replaceString = from_utf8(LYX_FR_NULL_STRING);
465 FindAndReplaceOptions::SearchScope scope =
466 FindAndReplaceOptions::S_BUFFER;
467 if (CurrentDocument->isChecked())
468 scope = FindAndReplaceOptions::S_BUFFER;
469 else if (MasterDocument->isChecked())
470 scope = FindAndReplaceOptions::S_DOCUMENT;
471 else if (OpenDocuments->isChecked())
472 scope = FindAndReplaceOptions::S_OPEN_BUFFERS;
473 else if (AllManualsRB->isChecked())
474 scope = FindAndReplaceOptions::S_ALL_MANUALS;
476 LASSERT(false, /**/);
477 LYXERR(Debug::FIND, "FindAndReplaceOptions: "
478 << "searchstring=" << searchString
479 << ", casesensitiv=" << casesensitive
480 << ", matchword=" << matchword
481 << ", backwards=" << backwards
482 << ", expandmacros=" << expandmacros
483 << ", ignoreformat=" << ignoreformat
484 << ", regexp=" << regexp
485 << ", replaceString" << replaceString
486 << ", keep_case=" << keep_case
487 << ", scope=" << scope);
488 FindAndReplaceOptions opt(searchString, casesensitive, matchword,
489 !backwards, expandmacros, ignoreformat,
490 regexp, replaceString, keep_case, scope);
492 findAndReplaceScope(opt);
493 view_.setBusy(false);
497 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
499 if (! view_.currentMainWorkArea()) {
500 view_.message(_("No open document(s) in which to search"));
503 // FIXME: create a Dialog::returnFocus()
504 // or something instead of this:
505 view_.setCurrentWorkArea(view_.currentMainWorkArea());
506 findAndReplace(caseCB->isChecked(),
507 wordsCB->isChecked(),
509 expandMacrosCB->isChecked(),
510 ignoreFormatCB->isChecked(),
512 keepCaseCB->isChecked());
516 void FindAndReplaceWidget::insertRegexp(QAction * action)
518 string const regexp = fromqstr(action->data().toString());
519 LYXERR(Debug::FIND, "Regexp: " << regexp);
520 find_work_area_->setFocus();
521 Cursor & cur = find_work_area_->bufferView().cursor();
522 if (!cur.inRegexped())
523 dispatch(FuncRequest(LFUN_REGEXP_MODE));
524 dispatch(FuncRequest(LFUN_SELF_INSERT, regexp));
528 void FindAndReplaceWidget::hideDialog()
530 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
534 void FindAndReplaceWidget::on_findNextPB_clicked()
536 findAndReplace(searchbackCB->isChecked(), false);
537 find_work_area_->setFocus();
541 void FindAndReplaceWidget::on_replacePB_clicked()
543 findAndReplace(searchbackCB->isChecked(), true);
544 replace_work_area_->setFocus();
548 void FindAndReplaceWidget::on_replaceallPB_clicked()
550 replace_work_area_->setFocus();
554 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
556 view_.setCurrentWorkArea(find_work_area_);
557 LYXERR(Debug::FIND, "Selecting entire find buffer");
558 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
559 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
560 find_work_area_->installEventFilter(this);
561 replace_work_area_->installEventFilter(this);
565 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
567 replace_work_area_->removeEventFilter(this);
568 find_work_area_->removeEventFilter(this);
569 this->QWidget::hideEvent(ev);
573 bool FindAndReplaceWidget::initialiseParams(std::string const & /*params*/)
579 FindAndReplace::FindAndReplace(GuiView & parent,
580 Qt::DockWidgetArea area, Qt::WindowFlags flags)
581 : DockView(parent, "Find LyX", qt_("Advanced Find and Replace"),
584 widget_ = new FindAndReplaceWidget(parent);
586 setFocusProxy(widget_);
590 FindAndReplace::~FindAndReplace()
597 bool FindAndReplace::initialiseParams(std::string const & params)
599 return widget_->initialiseParams(params);
603 Dialog * createGuiSearchAdv(GuiView & lv)
605 FindAndReplace * gui = new FindAndReplace(lv, Qt::RightDockWidgetArea);
607 // On Mac show and floating
608 gui->setFloating(true);
614 } // namespace frontend
618 #include "moc_FindAndReplace.cpp"