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"
16 #include "qt_helpers.h"
18 #include "GuiWorkArea.h"
20 #include "buffer_funcs.h"
21 #include "BufferParams.h"
22 #include "BufferList.h"
24 #include "FuncRequest.h"
26 #include "OutputParams.h"
27 #include "output_latex.h"
31 #include "support/debug.h"
32 #include "support/FileName.h"
33 #include "support/gettext.h"
34 #include "support/lassert.h"
36 #include <QCloseEvent>
42 using namespace lyx::support;
48 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
52 #if QT_VERSION < 0x040400
53 scrollArea->setWidget(scrollAreaWidgetContents);
55 find_work_area_->setGuiView(view_);
56 find_work_area_->init();
57 setFocusProxy(find_work_area_);
58 replace_work_area_->setGuiView(view_);
59 replace_work_area_->init();
60 // We don't want two cursors blinking.
61 replace_work_area_->stopBlinkingCursor();
65 bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event)
67 if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
68 QKeyEvent *e = static_cast<QKeyEvent *> (event);
69 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
72 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
73 if (e->modifiers() == Qt::ShiftModifier) {
74 on_findPrevPB_clicked();
76 } else if (e->modifiers() == Qt::NoModifier) {
77 on_findNextPB_clicked();
80 } else if (e->key() == Qt::Key_Tab && e->modifiers() == Qt::NoModifier) {
81 LYXERR(Debug::FIND, "Focusing replace WA");
82 replace_work_area_->setFocus();
86 if (obj == replace_work_area_ && event->type() == QEvent::KeyPress) {
87 QKeyEvent *e = static_cast<QKeyEvent *> (event);
88 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
91 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
92 if (e->modifiers() == Qt::ShiftModifier) {
93 on_replacePrevPB_clicked();
95 } else if (e->modifiers() == Qt::NoModifier) {
96 on_replaceNextPB_clicked();
99 } else if (e->key() == Qt::Key_Backtab) {
100 LYXERR(Debug::FIND, "Focusing find WA");
101 find_work_area_->setFocus();
105 // standard event processing
106 return QWidget::eventFilter(obj, event);
109 static docstring buffer_to_latex(Buffer & buffer) {
110 OutputParams runparams(&buffer.params().encoding());
112 runparams.nice = true;
113 runparams.flavor = OutputParams::LATEX;
114 runparams.linelen = 80; //lyxrc.plaintext_linelen;
115 // No side effect of file copying and image conversion
116 runparams.dryrun = true;
117 buffer.texrow().reset();
118 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
119 ParagraphList::const_iterator const end = buffer.paragraphs().end();
120 for (; pit != end; ++pit) {
121 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
122 LYXERR(Debug::FIND, "searchString up to here: " << os.str());
128 /** Switch p_buf to point to next document buffer.
130 ** Return true if restarted from master-document buffer.
133 ** Not using p_buf->allRelatives() here, because I'm not sure
134 ** whether or not the returned order is independent of p_buf.
136 static bool next_document_buffer(Buffer * & p_buf) {
137 Buffer *p_master = p_buf;
141 p_master = const_cast<Buffer *>(p_master->masterBuffer());
142 LYXERR(Debug::FIND, "p_old=" << p_old << ", p_master=" << p_master);
143 } while (p_master != p_old);
144 LASSERT(p_master != NULL, /**/);
145 vector<Buffer *> v_children;
146 /* Root master added as first buffer in the vector */
147 v_children.push_back(p_master);
148 p_master->getChildren(v_children, true);
149 LYXERR(Debug::FIND, "v_children.size()=" << v_children.size());
150 vector<Buffer *>::const_iterator it = find(v_children.begin(), v_children.end(), p_buf);
151 LASSERT(it != v_children.end(), /**/)
153 if (it == v_children.end()) {
154 p_buf = *v_children.begin();
162 /** Switch p_buf to point to previous document buffer.
164 ** Return true if restarted from last child buffer.
167 ** Not using p_buf->allRelatives() here, because I'm not sure
168 ** whether or not the returned order is independent of p_buf.
170 static bool prev_document_buffer(Buffer * & p_buf) {
171 Buffer *p_master = p_buf;
175 p_master = const_cast<Buffer *>(p_master->masterBuffer());
176 LYXERR(Debug::FIND, "p_old=" << p_old << ", p_master=" << p_master);
177 } while (p_master != p_old);
178 LASSERT(p_master != NULL, /**/);
179 vector<Buffer *> v_children;
180 /* Root master added as first buffer in the vector */
181 v_children.push_back(p_master);
182 p_master->getChildren(v_children, true);
183 LYXERR(Debug::FIND, "v_children.size()=" << v_children.size());
184 vector<Buffer *>::const_iterator it = find(v_children.begin(), v_children.end(), p_buf);
185 LASSERT(it != v_children.end(), /**/)
186 if (it == v_children.begin()) {
187 it = v_children.end();
198 /** Switch buf to point to next or previous buffer in search scope.
200 ** Return true if restarted from scratch.
202 static bool next_prev_buffer(Buffer * & buf, FindAndReplaceOptions const & opt) {
203 bool restarted = false;
205 case FindAndReplaceOptions::S_BUFFER:
208 case FindAndReplaceOptions::S_DOCUMENT:
210 restarted = next_document_buffer(buf);
212 restarted = prev_document_buffer(buf);
214 case FindAndReplaceOptions::S_OPEN_BUFFERS:
216 buf = theBufferList().next(buf);
217 restarted = buf == *theBufferList().begin();
219 buf = theBufferList().previous(buf);
220 restarted = buf == *(theBufferList().end() - 1);
228 /** Find the finest question message to post to the user */
229 docstring question_string(FindAndReplaceOptions const & opt)
231 docstring cur_pos = opt.forward ? _("End") : _("Begin");
232 docstring new_pos = opt.forward ? _("begin") : _("end");
235 case FindAndReplaceOptions::S_BUFFER:
238 case FindAndReplaceOptions::S_DOCUMENT:
239 scope = _("master document");
241 case FindAndReplaceOptions::S_OPEN_BUFFERS:
242 scope = _("open files");
245 docstring dir = opt.forward ? _("forward") : _("backwards");
246 return cur_pos + _(" of ") + scope
247 + _(" reached while searching ") + dir + ".\n"
249 + _("Continue searching from ") + new_pos + " ?";
253 void FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt) {
254 int wrap_answer = -1;
257 FuncRequest cmd(LFUN_WORD_FINDADV, from_utf8(oss.str()));
258 BufferView * bv = view_.documentBufferView();
259 Buffer * buf = &bv->buffer();
261 Buffer * buf_orig = &bv->buffer();
262 Cursor cur_orig(bv->cursor());
265 LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
267 if (bv->cursor().result().dispatched()) {
268 // Match found, selected and replaced if needed
272 // No match found in current buffer: select next buffer in scope, if any
273 bool prompt = next_prev_buffer(buf, opt);
275 if (wrap_answer != -1)
277 docstring q = question_string(opt);
278 wrap_answer = frontend::Alert::prompt(
279 _("Wrap search?"), q,
280 0, 1, _("&Yes"), _("&No"));
281 if (wrap_answer == 1)
284 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
285 buf->absFileName()));
286 bv = view_.documentBufferView();
288 bv->cursor().clear();
289 bv->cursor().push_back(CursorSlice(buf->inset()));
291 lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
292 bv->cursor().setCursor(doc_iterator_end(buf));
293 bv->cursor().backwardPos();
294 LYXERR(Debug::FIND, "findBackAdv5: cur: " << bv->cursor());
296 bv->clearSelection();
297 } while (wrap_answer != 1);
298 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
299 buf_orig->absFileName()));
300 bv = view_.documentBufferView();
301 bv->cursor() = cur_orig;
305 void FindAndReplaceWidget::findAndReplace(
306 bool casesensitive, bool matchword, bool backwards,
307 bool expandmacros, bool ignoreformat, bool replace,
310 Buffer & buffer = find_work_area_->bufferView().buffer();
311 docstring searchString;
313 searchString = buffer_to_latex(buffer);
315 ParIterator it = buffer.par_iterator_begin();
316 ParIterator end = buffer.par_iterator_end();
317 OutputParams runparams(&buffer.params().encoding());
319 runparams.nice = true;
320 runparams.flavor = OutputParams::LATEX;
321 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
322 runparams.dryrun = true;
323 for (; it != end; ++it) {
324 LYXERR(Debug::FIND, "Adding to search string: '" << it->asString(false) << "'");
325 searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
328 if (to_utf8(searchString).empty()) {
329 buffer.message(_("Nothing to search"));
332 bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
333 docstring replaceString;
335 Buffer & repl_buffer = replace_work_area_->bufferView().buffer();
337 repl_buffer.write(oss);
338 replaceString = from_utf8(oss.str()); //buffer_to_latex(replace_buffer);
340 replaceString = from_utf8(LYX_FR_NULL_STRING);
342 FindAndReplaceOptions::SearchScope scope = FindAndReplaceOptions::S_BUFFER;
343 if (CurrentDocument->isChecked())
344 scope = FindAndReplaceOptions::S_BUFFER;
345 else if (MasterDocument->isChecked())
346 scope = FindAndReplaceOptions::S_DOCUMENT;
347 else if (OpenDocuments->isChecked())
348 scope = FindAndReplaceOptions::S_OPEN_BUFFERS;
350 LASSERT(false, /**/);
351 LYXERR(Debug::FIND, "FindAndReplaceOptions: "
352 << "searchstring=" << searchString
353 << ", casesensitiv=" << casesensitive
354 << ", matchword=" << matchword
355 << ", backwards=" << backwards
356 << ", expandmacros=" << expandmacros
357 << ", ignoreformat=" << ignoreformat
358 << ", regexp=" << regexp
359 << ", replaceString" << replaceString
360 << ", keep_case=" << keep_case
361 << ", scope=" << scope);
362 FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
363 expandmacros, ignoreformat, regexp, replaceString, keep_case, scope);
364 findAndReplaceScope(opt);
368 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
370 if (! view_.currentMainWorkArea()) {
371 view_.message(_("No open document(s) in which to search"));
374 // FIXME: create a Dialog::returnFocus() or something instead of this:
375 view_.setCurrentWorkArea(view_.currentMainWorkArea());
376 findAndReplace(caseCB->isChecked(),
377 wordsCB->isChecked(),
379 expandMacrosCB->isChecked(),
380 ignoreFormatCB->isChecked(),
382 keepCaseCB->isChecked());
386 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
388 static char const * regexps[] = {
389 ".*", ".+", "[a-z]+", "[0-9]+", ""
391 LYXERR(Debug::FIND, "Index: " << index);
392 if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
393 find_work_area_->setFocus();
394 Cursor & cur = find_work_area_->bufferView().cursor();
395 if (! cur.inRegexped())
396 dispatch(FuncRequest(LFUN_REGEXP_MODE));
397 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
398 regexpInsertCombo->setCurrentIndex(0);
403 void FindAndReplaceWidget::on_closePB_clicked()
405 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
409 void FindAndReplaceWidget::on_findNextPB_clicked() {
410 findAndReplace(false, false);
411 find_work_area_->setFocus();
415 void FindAndReplaceWidget::on_findPrevPB_clicked() {
416 findAndReplace(true, false);
417 find_work_area_->setFocus();
421 void FindAndReplaceWidget::on_replaceNextPB_clicked()
423 findAndReplace(false, true);
424 replace_work_area_->setFocus();
428 void FindAndReplaceWidget::on_replacePrevPB_clicked()
430 findAndReplace(true, true);
431 replace_work_area_->setFocus();
435 void FindAndReplaceWidget::on_replaceallPB_clicked()
437 replace_work_area_->setFocus();
441 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
443 view_.setCurrentWorkArea(find_work_area_);
444 LYXERR(Debug::FIND, "Selecting entire find buffer");
445 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
446 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
447 find_work_area_->installEventFilter(this);
448 replace_work_area_->installEventFilter(this);
452 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
454 replace_work_area_->removeEventFilter(this);
455 find_work_area_->removeEventFilter(this);
456 this->QWidget::hideEvent(ev);
460 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
466 FindAndReplace::FindAndReplace(GuiView & parent,
467 Qt::DockWidgetArea area, Qt::WindowFlags flags)
468 : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
470 widget_ = new FindAndReplaceWidget(parent);
472 setFocusProxy(widget_);
476 FindAndReplace::~FindAndReplace()
483 bool FindAndReplace::initialiseParams(std::string const & params)
485 return widget_->initialiseParams(params);
489 Dialog * createGuiSearchAdv(GuiView & lv)
491 return new FindAndReplace(lv, Qt::RightDockWidgetArea);
495 } // namespace frontend
499 #include "moc_FindAndReplace.cpp"