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"
23 #include "FuncRequest.h"
25 #include "OutputParams.h"
26 #include "output_latex.h"
29 #include "support/convert.h"
30 #include "support/debug.h"
31 #include "support/FileName.h"
32 #include "support/gettext.h"
33 #include "support/lassert.h"
35 #include <QCloseEvent>
41 using namespace lyx::support;
46 FindAndReplace::FindAndReplace(GuiView & parent)
47 : DockView(parent, "Find LyX", "Find LyX Dialog", Qt::RightDockWidgetArea),
51 find_work_area_->setGuiView(parent_view_);
52 find_work_area_->init();
56 bool FindAndReplace::eventFilter(QObject *obj, QEvent *event)
58 LYXERR(Debug::DEBUG, "FindAndReplace::eventFilter()" << std::endl);
59 if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
60 QKeyEvent *e = static_cast<QKeyEvent *> (event);
61 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
64 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
65 if (e->modifiers() == Qt::ShiftModifier) {
66 on_findPrevPB_clicked();
68 } else if (e->modifiers() == Qt::NoModifier) {
69 on_findNextPB_clicked();
74 // standard event processing
75 return QObject::eventFilter(obj, event);
79 void FindAndReplace::selectAll()
81 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
82 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
83 find_work_area_->redraw();
87 void FindAndReplace::findAdv(bool casesensitive,
88 bool matchword, bool backwards,
89 bool expandmacros, bool ignoreformat)
91 Buffer & buffer = find_work_area_->bufferView().buffer();
92 docstring searchString;
94 OutputParams runparams(&buffer.params().encoding());
96 runparams.nice = true;
97 runparams.flavor = OutputParams::LATEX;
98 runparams.linelen = 80; //lyxrc.plaintext_linelen;
99 // No side effect of file copying and image conversion
100 runparams.dryrun = true;
101 buffer.texrow().reset();
102 // latexParagraphs(buffer, buffer.paragraphs(), os, buffer.texrow(), runparams);
103 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
104 ParagraphList::const_iterator const end = buffer.paragraphs().end();
105 for (; pit != end; ++pit) {
106 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
107 LYXERR0("searchString up to here: " << os.str());
109 searchString = os.str();
111 ParIterator it = buffer.par_iterator_begin();
112 ParIterator end = buffer.par_iterator_end();
113 for (; it != end; ++it) {
114 LYXERR0("Adding to search string: '" << it->asString(false) << "'");
115 searchString += it->asString(AS_STR_INSETS);
118 // lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
119 if (to_utf8(searchString).empty()) {
120 buffer.message(_("Nothing to search"));
123 bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
124 FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards,
125 expandmacros, ignoreformat, regexp);
126 std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
127 std::ostringstream oss;
129 std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
130 dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
132 // findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
133 // searchString, len, casesensitive, matchword, ! backwards, expandmacros);
137 void FindAndReplace::showEvent(QShowEvent *ev)
140 QWidget::showEvent(ev);
144 void FindAndReplace::hideEvent(QHideEvent *ev)
146 LYXERR(Debug::DEBUG, "FindAndReplace::hideEvent");
147 find_work_area_->removeEventFilter(this);
148 find_work_area_->disable();
149 this->QWidget::hideEvent(ev);
153 void FindAndReplace::find(bool backwards)
155 parent_view_.setCurrentWorkArea(parent_view_.currentMainWorkArea());
156 findAdv(caseCB->isChecked(),
157 wordsCB->isChecked(),
159 expandMacrosCB->isChecked(),
160 ignoreFormatCB->isChecked());
161 parent_view_.currentMainWorkArea()->redraw();
162 parent_view_.setCurrentWorkArea(find_work_area_);
163 find_work_area_->setFocus();
167 void FindAndReplace::on_regexpInsertCombo_currentIndexChanged(int index)
169 static char const * regexps[] = {
170 ".*", ".+", "[a-z]+", "[0-9]+"
172 //lyxerr << "Index: " << index << std::endl;
173 if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
174 find_work_area_->setFocus();
175 Cursor & cur = find_work_area_->bufferView().cursor();
176 if (! cur.inRegexped())
177 dispatch(FuncRequest(LFUN_REGEXP_MODE));
178 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
179 regexpInsertCombo->setCurrentIndex(0);
184 void FindAndReplace::on_closePB_clicked()
186 find_work_area_->disable();
187 LYXERR(Debug::DEBUG, "Dispatching dialog-hide findreplaceadv" << std::endl);
188 parent_view_.dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
192 void FindAndReplace::on_findNextPB_clicked() {
197 void FindAndReplace::on_findPrevPB_clicked() {
202 void FindAndReplace::on_replacePB_clicked()
207 void FindAndReplace::on_replaceallPB_clicked()
211 Dialog * createGuiSearchAdv(GuiView & lv)
213 return new FindAndReplace(lv);
217 } // namespace frontend
221 #include "moc_FindAndReplace.cpp"