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 "GuiWorkArea.h"
18 #include "qt_helpers.h"
20 #include "Application.h"
21 #include "buffer_funcs.h"
22 #include "BufferParams.h"
24 #include "FuncRequest.h"
26 #include "OutputParams.h"
27 #include "output_latex.h"
30 #include "support/FileName.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/gettext.h"
34 #include "support/lassert.h"
37 #include <QCloseEvent>
42 using namespace lyx::support;
48 FindAndReplace::FindAndReplace(GuiView & parent)
49 : DockView(parent, "Find LyX", "Find LyX Dialog", Qt::RightDockWidgetArea),
51 delayedFocusTimer_(this)
54 find_work_area_->setGuiView(parent_view_);
55 find_work_area_->init();
59 bool FindAndReplace::eventFilter(QObject *obj, QEvent *event)
61 LYXERR(Debug::DEBUG, "FindAndReplace::eventFilter()" << std::endl);
62 if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
63 QKeyEvent *e = static_cast<QKeyEvent *> (event);
64 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
67 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
68 if (e->modifiers() == Qt::ShiftModifier) {
69 on_findPrevPB_clicked();
71 } else if (e->modifiers() == Qt::NoModifier) {
72 on_findNextPB_clicked();
78 // standard event processing
79 return QObject::eventFilter(obj, event);
83 void FindAndReplace::closeEvent(QCloseEvent * close_event)
85 LYXERR(Debug::DEBUG, "FindAndReplace::closeEvent()");
86 find_work_area_->removeEventFilter(this);
87 disableSearchWorkArea();
89 DockView::closeEvent(close_event);
93 void FindAndReplace::selectAll()
95 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
96 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
97 find_work_area_->redraw();
101 void FindAndReplace::findAdv(bool casesensitive,
102 bool matchword, bool backwards,
103 bool expandmacros, bool ignoreformat)
105 Buffer & buffer = find_work_area_->bufferView().buffer();
106 docstring searchString;
107 if (! ignoreformat) {
108 OutputParams runparams(&buffer.params().encoding());
110 runparams.nice = true;
111 runparams.flavor = OutputParams::LATEX;
112 runparams.linelen = 80; //lyxrc.plaintext_linelen;
113 // No side effect of file copying and image conversion
114 runparams.dryrun = true;
115 buffer.texrow().reset();
116 // latexParagraphs(buffer, buffer.paragraphs(), os, buffer.texrow(), runparams);
117 for (ParagraphList::const_iterator pit = buffer.paragraphs().begin(); pit != buffer.paragraphs().end(); ++pit) {
118 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
119 lyxerr << "searchString up to here: " << to_utf8(os.str()) << std::endl;
121 searchString = os.str();
123 for (ParIterator it = buffer.par_iterator_begin(); it != buffer.par_iterator_end(); ++it) {
124 lyxerr << "Adding to search string: '" << to_utf8(it->asString(false)) << "'" << std::endl;
125 searchString += it->asString(AS_STR_INSETS);
128 // lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
129 if (to_utf8(searchString).empty()) {
130 buffer.message(_("Nothing to search"));
133 bool regexp = (to_utf8(searchString).find("\\regexp") != std::string::npos);
134 FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards, expandmacros, ignoreformat, regexp);
135 std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
136 std::ostringstream oss;
138 std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
139 dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
141 // findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
142 // searchString, len, casesensitive, matchword, ! backwards, expandmacros);
146 void FindAndReplace::onDelayedFocus()
148 LYXERR(Debug::DEBUG, "Delayed Focus");
149 parent_view_.setCurrentWorkArea(find_work_area_);
150 find_work_area_->setFocus();
154 void FindAndReplace::showEvent(QShowEvent *ev)
156 LYXERR(Debug::DEBUG, "FindAndReplace::showEvent");
157 parent_view_.setCurrentWorkArea(find_work_area_);
159 find_work_area_->redraw();
160 find_work_area_->setFocus();
161 find_work_area_->installEventFilter(this);
162 connect(&delayedFocusTimer_, SIGNAL(timeout()), this, SLOT(onDelayedFocus()));
163 delayedFocusTimer_.setSingleShot(true);
164 delayedFocusTimer_.start(100);
166 this->QWidget::showEvent(ev);
170 void FindAndReplace::disableSearchWorkArea()
172 LYXERR(Debug::DEBUG, "FindAndReplace::disableSearchWorkArea()");
173 // Ok, closing the window before 100ms may be impossible, however...
174 delayedFocusTimer_.stop();
175 if (parent_view_.currentWorkArea() == find_work_area_) {
176 LASSERT(parent_view_.currentMainWorkArea(), /* */);
177 parent_view_.setCurrentWorkArea(parent_view_.currentMainWorkArea());
179 find_work_area_->stopBlinkingCursor();
183 void FindAndReplace::hideEvent(QHideEvent *ev)
185 LYXERR(Debug::DEBUG, "FindAndReplace::hideEvent");
186 find_work_area_->removeEventFilter(this);
187 disableSearchWorkArea();
188 this->QWidget::hideEvent(ev);
192 void FindAndReplace::find(bool backwards)
194 parent_view_.setCurrentWorkArea(parent_view_.currentMainWorkArea());
195 findAdv(caseCB->isChecked(),
196 wordsCB->isChecked(),
198 expandMacrosCB->isChecked(),
199 ignoreFormatCB->isChecked());
200 parent_view_.currentMainWorkArea()->redraw();
201 parent_view_.setCurrentWorkArea(find_work_area_);
202 find_work_area_->setFocus();
206 void FindAndReplace::on_regexpInsertCombo_currentIndexChanged(int index)
208 static char const * regexps[] = {
209 ".*", ".+", "[a-z]+", "[0-9]+"
211 //lyxerr << "Index: " << index << std::endl;
212 if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
213 find_work_area_->setFocus();
214 Cursor & cur = find_work_area_->bufferView().cursor();
215 if (! cur.inRegexped())
216 dispatch(FuncRequest(LFUN_REGEXP_MODE));
217 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
218 regexpInsertCombo->setCurrentIndex(0);
223 void FindAndReplace::on_closePB_clicked() {
224 disableSearchWorkArea();
225 LYXERR(Debug::DEBUG, "Dispatching dialog-hide findreplaceadv" << std::endl);
226 parent_view_.dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
230 void FindAndReplace::on_findNextPB_clicked() {
235 void FindAndReplace::on_findPrevPB_clicked() {
240 void FindAndReplace::on_replacePB_clicked()
245 void FindAndReplace::on_replaceallPB_clicked()
249 Dialog * createGuiSearchAdv(GuiView & lv)
251 return new FindAndReplace(lv);
255 } // namespace frontend
259 #include "moc_FindAndReplace.cpp"